Quick Container Deployment with AWS CoPilot
AWS Copilot is a command line interface (CLI) that enables quick launch and easy manageability of containerized applications on AWS. Copilot automates each step in the application deployment lifecycle including pushing to a registry, creating a task definition, and creating a cluster. In this exercise, you learn to launch a containerized application using AWS CoPilot.
Step1: Pre-Requisite
You need to have an AWS account with administrative access to complete the exercise. If you don’t have an AWS account, kindly use the link to create free trial account for AWS.
Step2: Create IAM User
Let’s create an IAM user which is used to containerize the application using Amazon CoPilot in Amazon Cloud9. When building the exercise, we tried to use Cloud9 temporary managed credentials but we were getting credentials reset error; hence we used IAM user credentials in Cloud9 for the exercise.
-
Login to the AWS Console and select Ireland as the region.
-
Goto IAM Management Console, click on the Users menu in the left and then click on the Add user button.
-
On the next screen, type in dojouser as the user name and select Programmatic access as the option for the access type. Click on the Next: Permissions button.
-
On the next screen, select Attach existing policies directly option. Select AdministratorAccess as the policy and click on the Next: Tags button.
-
On the next screen, click on the Next: Review button.
-
On the next Review screen, click on the Create user button. It creates the user and it also generates Access key ID and Secret access key. Click on the Download.csv link to download the keys. Note: please make sure you make note of the keys as you need them in the later steps.
-
In the next step, you create a Cloud9 environment which is used to run Amazon CoPilot commands.
Step3: Create Cloud9 Environment
You use an Amazon Cloud9 environment to create container application and then deploy it using AWS CoPilot commands.
-
Goto the AWS Cloud9 console and click on the Create environment button.
-
On the next screen, enter dojoenvironment as the name and click on the Next step button.
-
On the next screen, select Environment type as Create a new EC2 instance for environment (direct access). Select Instance type as m5.large (8 GiB RAM + 2 vCPU). Select Amazon Linux for the Platform. Keep rest of the fields with the default values and click on the Next step button.
-
On the next screen, click on the Create environment button.
-
It will take couple of minutes to create the environment. In the next step, you will deploy AWS CoPilot in the environment.
Step4: Configure Cloud9 Environment
In the Cloud9 environment, you deploy Docker and AWS CoPilot CLI.
-
In the AWS Cloud9 console, run the following commands one by one to install docker.
sudo yum update -y sudo yum install docker sudo service docker start
`
-
Run docker –version command to verify the docker installation.
-
Run the following commands one by one to install AWS Copilot.
sudo curl -Lo /usr/local/bin/copilot https://github.com/aws/copilot-cli/releases/download/v0.4.0/copilot-linux-v0.4.0 sudo chmod +x /usr/local/bin/copilot
`
-
Next run the command copilot –help to verify if Copilot CLI is deployed.
-
The Cloud9 Environment is ready. You create your container application in the next step.
Step5: Create Container Application
In this step, you create a sample container application and later use AWS Copilot CLI to deploy it.
-
In Cloud9 environment, use New File menu option under File to create a new file index.html. Copy-paste the following HTML code into it.
<!doctype html> <html lang="en"> <head> <title>Docker Sample Application</title> </head> <body> <h2>Docker Sample Application</h2> </body> </html>
`
-
Next you create another file Dockerfile with the following code.
FROM nginx:latest EXPOSE 80 COPY ./index.html /usr/share/nginx/html/index.html
`
-
The sample application is ready. It is time to deploy it using AWS CoPilot CLI commands.
Step6: Deploy Container Application
In this step, you deploy the sample container application using AWS CoPilot CLI commands.
-
You will use the IAM user credentials created in the previous steps to run the AWS CoPilot CLI commands. Run the following commands one by one. Replace «Access-Id» with access id of the dojouser IAM User. Replace «Secret-Key» with secret key of the dojouser IAM User. You made note of these credentials in the previous steps.
export AWS_ACCESS_KEY_ID=<<Access-Id>> export AWS_SECRET_ACCESS_KEY=<<Secret-Key>> export AWS_DEFAULT_REGION=eu-west-1
`
-
In Cloud9 environment, run copilot init command. It will ask series of question which you need to answer to configure the deployment. The questions are as the following and you provide response as mentioned below.
What would you like to name your application? [? for help]
Enter - dojoapp
Which service type best represents your service’s architecture? [Use arrows to move, type to filter, ? for more help]
> Load Balanced Web Service Backend Service
Select Load Balanced Web Service and enter.
What do you want to name this Load Balanced Web Service? [? for help]
Enter - dojoapi
Which Dockerfile would you like to use for dojoapi? [Use arrows to move, type to filter, ? for more help]
> ./Dockerfile
Select./Dockerfile and enter.
-
It will take some time to configure the manifest file for the application deploy. It then asks - Would you like to deploy a test environment?. Type in y and enter.
-
It will start creating the deployment environment.
-
It will ask to tag the container image (Input an image tag value:). Type in dojo and enter. It will take some time to complete the deployment. When the deployment finishes, you can see the URL for the application deployed.
-
You can access the URL to verify the application deployed.
-
The application has been deployed. There are many other AWS CoPilot commands to list the applications, show environment details, show logs etc. It is recommended to refer AWS Copilot documentation for that.
Step7: Review the Resources Created
Before you close the exercise. It is worth going through the key resources AWS Copilot created in order to deploy the application.
ECS Task Definition:
ECS Cluster and Service:
Load Balancer:
ECR Image:
It finishes the exercise. Goto the next step to clean up the resources so that you don’t incur any cost post the exercise.
Step8: Clean up
Run the command copilot app delete –env-profiles test=default in the Cloud9 environment to delete the deployment of the application. Alternative - you can delete the following CloudFormation stacks.
Delete the dojoenvironment Cloud9 Environment.
Delete dojouser IAM User in the IAM Management console.
Thanks and hope you enjoyed the exercise.