Deploy a Container Web App on Amazon ECS Using Amazon CodeCatalyst
Learn to build and deploy a container-based web application using ECS and CodeCatalyst.
What Are the Components of ECS?
Compute Capacity Planning, and Options
Create the Code for the Resource Stack
Create CodeCatalyst CodeCatalystPreviewDevelopmentAdministrator Role
Create CodeCatalyst Repository
Create CodeCatalyst Dev Environment
Create a CodeCatalyst CI/CD Environment
Create a CodeCatalyst Workflow
Add a Render Amazon ECS Task Definition Action
Add a Deploy To Amazon ECS Action
Cleaning Up Your AWS Environment
- Create the infrastructure to run your container with Amazon ECS
- Deploy a containerized application to Amazon ECS using Amazon CodeCatalyst
About | |
---|---|
✅ AWS Level | 200 - Intermediate |
⏱ Time to complete | 15-20 minutes |
💰 Cost to complete | Less than $0.02 USD if completed in under an hour. |
🧩 Prerequisites | - AWS Account with administrator-level access* - CodeCatalyst Account [*]Accounts created within the past 24 hours might not yet have access to the services required for this tutorial. |
💻 Code Sample | Code sample used in tutorial on GitHub |
📢 Feedback | Any feedback, issues, or just a 👍 / 👎 ? |
⏰ Last Updated | 2023-04-26 |
- An AWS Account (if you don't yet have one, you can create one and set up your environment here).
- CDK installed: Visit the Get Started with AWS CDK guide to learn more.
- The example project code downloaded to extract the SampleApp.
- Docker installed and running.
- CodeCatalyst Account and Space setup with Space administrator role assigned to you (if you don't have a CodeCatalyst setup already, you can follow the Amazon CodeCatalyst setting up guide).
lib/cdk-ecs-infra-stack.ts
. This is where you will write the code for the resource stack you are going to create.- IAM role: This role will be assigned to the container to allow it to call other AWS services.
- ECS Task definition: The specific parameters to use when launching the container.
- ECS Pattern for Fargate load balanced service: This abstracts away the complexity of all the components required to create the cluster, load balancer, and service, and configures everything to work together.
aws-ec2
, aws-ecs
, aws-ecr
, and aws-cdk-lib/aws-ecs-patterns
ones. To add them, edit the lib/cdk-ecs-infra-stack.ts
file to add the dependency at the top of the file:FargateTaskDefintion
, and by using ContainerImage.fromRegistry. CDK will use the Nginx container image from DockerHub.desiredCount
indicates how many copies you want, serviceName
designates what you want to call the service, and publicLoadBalancer
is set to true so that you can access it over the internet. The line setting the assignPublicIp
to true is important in this example because we are using the default VPC that does not have private subnets. Best practice recommends launching services in private subnets, but that is outside the scope of this guide.ecs_pattern.ApplicationLoadBalancedFargateService
block:bin/cdk-ecs-infra.ts
file, and uncomment line 14:cdk deploy
to deploy the container, cluster, and all the other infrastructure required. Please note that Docker must be running to build the containerized application. You should see output similar to the following:y
and then Enter
to deploy. CDK will now set up all the infrastructure you defined, and it will take a few minutes to complete.CodeCatalystPreviewDevelopmentAdministrator
role. The CodeCatalystPreviewDevelopmentAdministrator
role has broad permissions which may pose a security risk, so we recommend that you only use this role in tutorials and scenarios where security is less of a concern.CodeCatalystPreviewDevelopmentAdministrator
role.zip
file, uncompress it, and move it into our project folder with the following commands (the adb68cd
part of the directory name may be different for you, please confirm the value, and replace as needed):task.json
in the SampleApp
folder, and replace the <account ID>
with your account ID.SampleApp
directory, run the following:main
branch, and will have the following content:BuildAndDeployToECS
by changing the first line to Name: BuildAndDeployToECS
. You can configure a workflow using either yaml
, or the visual editor. Let's define the steps we need to create, and then add them using the yml
editor. We will need to create the following:- Build the application to generate a Docker image using a Build action in the workflow and define a set of input variables.
- Region is used to configure the AWS region in which you would like the application to be deployed. Provide the same region where your ECS cluster infrastructure is provisioned.
- Registry is used to configure the Docker ECR endpoints, which will be used in the further build steps.
- Image is the name of your ECR repository, which is created within the CDK project, and should be
my-respository-cdkecsinfrastack
if you used the default values provided.
- Tag the Docker image
- Push the tagged Docker image to our ECR repository.
- Export the ECR URl for the image we just created to pass it to the deploy step
- Deploy the container image to the ECS cluster.
<Account-Id>
with your account ID, and <CodeCatalystPreviewDevelopmentAdministrator role>
(if you used the default, it should be CodeCatalystWorkflowDevelopmentRole-AWS
):Validate
button - it should add a green banner with The workflow definition is valid.
at the top. You can see the details of any errors by clicking on Warnings: 1
at the bottom of the page - the 1
indicates how many errors were detected. You can also now click on the Visual
button to inspect the workflow using the Visual editor.task.json
) we created earlier with the Docker image URI that we exported in the Build action as the IMAGE
output. This value will change every time we make changes to our sample application's source code, and we need a step in our workflow to handle this update to task.json
for us.task.json
file in the SampleApp repository. This task will output an updated task definition file at the runtime.RenderAmazonECStaskdefinition
line should be indented with 2 spaces at the start:<ECS cluster ARN>
with your ECS cluster ARN - this was part of the output after running cdk deploy
, and should be something like arn:aws:ecs:us-west-2:<account Id>:CdkEcsInfraStack-EcsDefaultClusterMnL3mNNYNVPC9C1EC7A3-UdnuAwVtK2q2
. Configure service to the ECS Service's name, which we defined in the CDK project. Alternatively, you can find the ECS service name in the AWS Console - Amazon Elastic Container Service - Clusters. Region is used to configure the AWS Region in which you would like your application to be deployed. Provide the same region where your ECS cluster infrastructure is provisioned. Provide the environment connection and CI/CD environment information under Environments.DeploytoAmazonECS
:<account ID>
, <CodeCatalystPreviewDevelopmentAdministrator role>
, and <ECS cluster ARN>
values replaces with appropriate values):Commit
button to add the workflow to the repository - it will be added in the .codecatalyst/workflows
directory as BuildAndDeployToECS.yaml
. After it is committed, it should start running automatically. You can click on the Run-xxxxx
link under Recent runs
to view the execution. Once completed, it should show all the step as successful with a green checkmark:- Delete CodeCatalyst dev environment
- Delete CodeCatalyst source-repository
- Delete CodeCatalyst project
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.