|
| 1 | +# ECS Code Mounting with AWS CDK in Python |
| 2 | + |
| 3 | +| Key | Value | |
| 4 | +| ------------ | ------------------------------------------------------------------------------------- | |
| 5 | +| Environment | <img src="https://img.shields.io/badge/LocalStack-deploys-4D29B4.svg?logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAKgAAACoABZrFArwAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAALbSURBVHic7ZpNaxNRFIafczNTGIq0G2M7pXWRlRv3Lusf8AMFEQT3guDWhX9BcC/uFAr1B4igLgSF4EYDtsuQ3M5GYrTaj3Tmui2SpMnM3PlK3m1uzjnPw8xw50MoaNrttl+r1e4CNRv1jTG/+v3+c8dG8TSilHoAPLZVX0RYWlraUbYaJI2IuLZ7KKUWCisgq8wF5D1A3rF+EQyCYPHo6Ghh3BrP8wb1en3f9izDYlVAp9O5EkXRB8dxxl7QBoNBpLW+7fv+a5vzDIvVU0BELhpjJrmaK2NMw+YsIxunUaTZbLrdbveZ1vpmGvWyTOJToNlsuqurq1vAdWPMeSDzwzhJEh0Bp+FTmifzxBZQBXiIKaAq8BBDQJXgYUoBVYOHKQRUER4mFFBVeJhAQJXh4QwBVYeHMQJmAR5GCJgVeBgiYJbg4T8BswYPp+4GW63WwvLy8hZwLcd5TudvBj3+OFBIeA4PD596nvc1iiIrD21qtdr+ysrKR8cY42itCwUP0Gg0+sC27T5qb2/vMunB/0ipTmZxfN//orW+BCwmrGV6vd63BP9P2j9WxGbxbrd7B3g14fLfwFsROUlzBmNM33XdR6Meuxfp5eg54IYxJvXCx8fHL4F3w36blTdDI4/0WREwMnMBeQ+Qd+YC8h4g78wF5D1A3rEqwBiT6q4ubpRSI+ewuhP0PO/NwcHBExHJZZ8PICI/e73ep7z6zzNPwWP1djhuOp3OfRG5kLROFEXv19fXP49bU6TbYQDa7XZDRF6kUUtEtoFb49YUbh/gOM7YbwqnyG4URQ/PWlQ4ASllNwzDzY2NDX3WwioKmBgeqidgKnioloCp4aE6AmLBQzUExIaH8gtIBA/lFrCTFB7KK2AnDMOrSeGhnAJSg4fyCUgVHsolIHV4KI8AK/BQDgHW4KH4AqzCQwEfiIRheKKUAvjuuu7m2tpakPdMmcYYI1rre0EQ1LPo9w82qyNziMdZ3AAAAABJRU5ErkJggg=="> <img src="https://img.shields.io/badge/AWS-deploys-F29100.svg?logo=amazon"> | |
| 6 | +| Services | Elastic Container Service, Elastic Container Registry | |
| 7 | +| Integrations | CDK | |
| 8 | +| Categories | Containers | |
| 9 | +| Level | Beginner | |
| 10 | +| GitHub | [Repository link](https://github.com/localstack-samples/ecs-code-mounting-python-cdk) | |
| 11 | + |
| 12 | +## Introduction |
| 13 | + |
| 14 | +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](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/bind-mounts.html) to implement the ECS Code Mounting feature. |
| 15 | + |
| 16 | +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. |
| 17 | + |
| 18 | +## Prerequisites |
| 19 | + |
| 20 | +- LocalStack Pro with the [`localstack` CLI](https://docs.localstack.cloud/getting-started/installation/#localstack-cli). |
| 21 | +- [Cloud Development Kit](https://docs.localstack.cloud/user-guide/integrations/aws-cdk/) with the [`cdklocal`](https://www.npmjs.com/package/aws-cdk-local) installed. |
| 22 | +- [Python 3.9+](https://www.python.org/downloads/) & `pip` package manager. |
| 23 | +- [`virtualenv`](https://pypi.org/project/virtualenv/) for creating isolated Python environments. |
| 24 | +- `cURL` or any other tool to send HTTP requests. |
| 25 | + |
| 26 | +Start LocalStack Pro with the `LOCALSTACK_AUTH_TOKEN` pre-configured: |
| 27 | + |
| 28 | +```shell |
| 29 | +export LOCALSTACK_AUTH_TOKEN=<your-auth-token> |
| 30 | +localstack start |
| 31 | +``` |
| 32 | + |
| 33 | +## Instructions |
| 34 | + |
| 35 | +In this section, you'll learn how to deploy the CDK stack to LocalStack and test the ECS Code Mounting feature. |
| 36 | + |
| 37 | +### Installing dependencies |
| 38 | + |
| 39 | +To install the dependencies, run the following command: |
| 40 | + |
| 41 | +```bash |
| 42 | +virtualenv env |
| 43 | +source env/bin/activate # On Windows, use `env\Scripts\activate` |
| 44 | +pip install -r requirements.txt |
| 45 | +``` |
| 46 | + |
| 47 | +### Deploying the CDK stack |
| 48 | + |
| 49 | +To bootstrap the CDK, run the following command: |
| 50 | + |
| 51 | +```shell |
| 52 | +cdklocal bootstrap |
| 53 | +``` |
| 54 | + |
| 55 | +To deploy the infrastructure, run the following command: |
| 56 | + |
| 57 | +```shell |
| 58 | +cdklocal deploy |
| 59 | +``` |
| 60 | + |
| 61 | +You are expected to see the following output: |
| 62 | + |
| 63 | +```bash |
| 64 | + ✅ CdkEcsExample |
| 65 | + |
| 66 | +✨ Deployment time: 16.8s |
| 67 | + |
| 68 | +Outputs: |
| 69 | +CdkEcsExample.DemoServiceLoadBalancerDNS00F01F2F = lb-2cf5d58c.elb.localhost.localstack.cloud |
| 70 | +CdkEcsExample.DemoServiceServiceURL823541D5 = http://lb-2cf5d58c.elb.localhost.localstack.cloud |
| 71 | +Stack ARN: |
| 72 | +arn:aws:cloudformation:us-east-1:000000000000:stack/CdkEcsExample/c8aa4bea |
| 73 | + |
| 74 | +✨ Total time: 18.08s |
| 75 | +``` |
| 76 | + |
| 77 | +### Testing the ECS deployment |
| 78 | + |
| 79 | +Navigate to the LocalStack logs and you would be able to see the following logs: |
| 80 | + |
| 81 | +```bash |
| 82 | +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 |
| 83 | +``` |
| 84 | + |
| 85 | +To test the ECS deployment, run the following command: |
| 86 | + |
| 87 | +```shell |
| 88 | +curl localhost:28099 |
| 89 | +``` |
| 90 | + |
| 91 | +You are expected to see the following output: |
| 92 | + |
| 93 | +```bash |
| 94 | +Hello, LocalStack! |
| 95 | +``` |
| 96 | + |
| 97 | +### Testing the ECS Code Mounting feature |
| 98 | + |
| 99 | +Go to the `service/main.py` file and change the message on line 8 to `Hello, ECS Code Mounting!`. Save the file. |
| 100 | + |
| 101 | +```python3 |
| 102 | +@app.route("/") |
| 103 | +def hello_world(): |
| 104 | + return "Hello, ECS Code Mounting!" |
| 105 | +``` |
| 106 | + |
| 107 | +Save the file and run the following command to test the code mounting feature: |
| 108 | + |
| 109 | +```shell |
| 110 | +curl localhost:28099 |
| 111 | +``` |
| 112 | + |
| 113 | +You are expected to see the following output: |
| 114 | + |
| 115 | +```bash |
| 116 | +Hello, ECS Code Mounting! |
| 117 | +``` |
| 118 | + |
| 119 | +### Cleaning up |
| 120 | + |
| 121 | +To clean up the resources, run the following command: |
| 122 | + |
| 123 | +```shell |
| 124 | +localstack stop |
| 125 | +``` |
| 126 | + |
| 127 | +## How do I set up the ECS Code Mounting feature? |
| 128 | + |
| 129 | +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: |
| 130 | + |
| 131 | +```python |
| 132 | +task_definition = ecs.FargateTaskDefinition( |
| 133 | + self, |
| 134 | + "DemoServiceTask", |
| 135 | + family="DemoServiceTask", |
| 136 | + volumes=[ |
| 137 | + ecs.Volume( |
| 138 | + name="test-volume", |
| 139 | + host=ecs.Host( |
| 140 | + source_path=os.path.join(os.getcwd(), "service") |
| 141 | + ), |
| 142 | + ) |
| 143 | + ], |
| 144 | +) |
| 145 | +... |
| 146 | +container.add_mount_points( |
| 147 | + ecs.MountPoint( |
| 148 | + container_path="/app", |
| 149 | + source_volume="test-volume", |
| 150 | + read_only=True |
| 151 | + ), |
| 152 | +) |
| 153 | +``` |
| 154 | + |
| 155 | +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). |
| 156 | + |
| 157 | +## License |
| 158 | + |
| 159 | +This library is licensed under the Apache 2.0 License. |
0 commit comments