GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - localstack-samples/ecs-code-mounting-python-cdk: Sample application showcasing how to use the ECS Code Mounting feature with AWS CDK in Python.

BranchesTags
Open more actions menu

Latest commit

 

History

15 Commits
15 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
cdk_ecs_example
cdk_ecs_example
 
 
images
images
 
 
service
service
 
 
.gitignore
.gitignore
 
 
Makefile
Makefile
 
 
README.md
README.md
 
 
app.py
app.py
 
 
cdk.json
cdk.json
 
 
requirements.txt
requirements.txt
 
 
setup.py
setup.py
 
 
test.sh
test.sh
 
 

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

  • A valid LocalStack for AWS license. Your license provides a LOCALSTACK_AUTH_TOKEN to activate LocalStack.
  • lstk CLI to manage the LocalStack container lifecycle. The AWS CLI is required by lstk aws.
  • Cloud Development Kit used via the lstk cdk proxy.
  • Python 3.9+ & pip package manager.
  • virtualenv for creating isolated Python environments.
  • cURL or any other tool to send HTTP requests.

Start LocalStack with the LOCALSTACK_AUTH_TOKEN pre-configured:

export LOCALSTACK_AUTH_TOKEN=<your-auth-token>
lstk 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:

lstk cdk bootstrap

To deploy the infrastructure, run the following command:

lstk cdk 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:

lstk 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

awscdkcdk-pythonecsecs-code-mountinglocalstack

Resources

Readme

Code of conduct

Code of conduct

Contributing

Contributing
Activity
Custom properties

Stars

1 star

Watchers

3 watching

Forks

0 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.