Key | Value |
---|---|
Environment | |
Services | Elastic Container Service, Elastic Container Registry |
Integrations | CDK |
Categories | Containers |
Level | Beginner |
GitHub | Repository link |
The ECS Code Mounting feature allows you to mount code from your host filesystem into the ECS container. This enables a quick debugging loop where you can test changes without having to build and redeploy the ECS task’s Docker image and push it to ECR each time. Internally, LocalStack uses the AWS Bind Mounts to implement the ECS Code Mounting feature.
The sample code in this repository demonstrates how to use the ECS Code Mounting feature with AWS CDK in Python. The ECS task uses a simple Flask application that returns a simple message. The code is mounted from the host filesystem into the ECS container, and the ECS task is deployed to a LocalStack environment.
- LocalStack Pro with the
localstack
CLI. - Cloud Development Kit with the
cdklocal
installed. - Python 3.9+ &
pip
package manager. virtualenv
for creating isolated Python environments.cURL
or any other tool to send HTTP requests.
Start LocalStack Pro with the LOCALSTACK_AUTH_TOKEN
pre-configured:
export LOCALSTACK_AUTH_TOKEN=<your-auth-token>
localstack start
In this section, you'll learn how to deploy the CDK stack to LocalStack and test the ECS Code Mounting feature.
To install the dependencies, run the following command:
virtualenv env
source env/bin/activate # On Windows, use `env\Scripts\activate`
pip install -r requirements.txt
To bootstrap the CDK, run the following command:
cdklocal bootstrap
To deploy the infrastructure, run the following command:
cdklocal deploy
You are expected to see the following output:
✅ CdkEcsExample
✨ Deployment time: 16.8s
Outputs:
CdkEcsExample.DemoServiceLoadBalancerDNS00F01F2F = lb-2cf5d58c.elb.localhost.localstack.cloud
CdkEcsExample.DemoServiceServiceURL823541D5 = http://lb-2cf5d58c.elb.localhost.localstack.cloud
Stack ARN:
arn:aws:cloudformation:us-east-1:000000000000:stack/CdkEcsExample/c8aa4bea
✨ Total time: 18.08s
Navigate to the LocalStack logs and you would be able to see the following logs:
2024-04-10T15:32:47.025 WARN --- [ asgi_gw_1] l.s.e.t.docker : Updating hostPort for ECS task to 19344, as requested host port 28099 seems unavailable
To test the ECS deployment, run the following command:
curl localhost:28099
You are expected to see the following output:
Hello, LocalStack!
Go to the service/main.py
file and change the message on line 8 to Hello, ECS Code Mounting!
. Save the file.
@app.route("/")
def hello_world():
return "Hello, ECS Code Mounting!"
Save the file and run the following command to test the code mounting feature:
curl localhost:28099
You are expected to see the following output:
Hello, ECS Code Mounting!
To clean up the resources, run the following command:
localstack stop
To use this example, you need to set up the ECS Code Mounting feature in the ECS task definition. The following code snippet demonstrates how to set up the ECS Code Mounting feature in the CDK stack:
task_definition = ecs.FargateTaskDefinition(
self,
"DemoServiceTask",
family="DemoServiceTask",
volumes=[
ecs.Volume(
name="test-volume",
host=ecs.Host(
source_path=os.path.join(os.getcwd(), "service")
),
)
],
)
...
container.add_mount_points(
ecs.MountPoint(
container_path="/app",
source_volume="test-volume",
read_only=True
),
)
In the above snippet, you need to create a volume with the source_path
pointing to the directory containing the code that you want to mount (in this case, the service
directory). Then, you need to add a mount point to the container with the container_path
pointing to the directory inside the container where you want to mount the code (in this case, the /app
directory).
This library is licensed under the Apache 2.0 License.