Skip to content

Sample application showcasing how to use the ECS Code Mounting feature with AWS CDK in Python.

Notifications You must be signed in to change notification settings

localstack-samples/ecs-code-mounting-python-cdk

Repository files navigation

ECS Code Mounting with AWS CDK in Python

Key Value
Environment
Services Elastic Container Service, Elastic Container Registry
Integrations CDK
Categories Containers
Level Beginner
GitHub Repository link

Introduction

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.

image

Prerequisites

Start LocalStack Pro with the LOCALSTACK_AUTH_TOKEN pre-configured:

export LOCALSTACK_AUTH_TOKEN=<your-auth-token>
localstack start

Instructions

In this section, you'll learn how to deploy the CDK stack to LocalStack and test the ECS Code Mounting feature.

Installing dependencies

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

Deploying the CDK stack

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

Testing the ECS deployment

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!

Testing the ECS Code Mounting feature

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!

Cleaning up

To clean up the resources, run the following command:

localstack stop

How do I set up the ECS Code Mounting feature?

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).

License

This library is licensed under the Apache 2.0 License.

About

Sample application showcasing how to use the ECS Code Mounting feature with AWS CDK in Python.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published