Important Note: You will create AWS resources during the exercise which will incur cost in your AWS account. It is recommended to clean-up the resources as soon as you finish the exercise to minimize the cost.

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.

  1. Login to the AWS Console and select Ireland as the region.

  2. Goto IAM Management Console, click on the Users menu in the left and then click on the Add user button.

    App2Container

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

    App2Container

  4. On the next screen, select Attach existing policies directly option. Select AdministratorAccess as the policy and click on the Next: Tags button.

    App2Container

  5. On the next screen, click on the Next: Review button.

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

    App2Container

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

  1. Goto the AWS Cloud9 console and click on the Create environment button.

    AWS CoPilot

  2. On the next screen, enter dojoenvironment as the name and click on the Next step button.

    AWS CoPilot

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

    AWS CoPilot

  4. On the next screen, click on the Create environment button.

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

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

    `

  2. Run docker –version command to verify the docker installation.

    AWS CoPilot

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

    `

  4. Next run the command copilot –help to verify if Copilot CLI is deployed.

    AWS CoPilot

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

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

    AWS CoPilot

    <!doctype html>
    <html lang="en">
    <head>
      <title>Docker Sample Application</title>
    </head>
    <body>
      <h2>Docker Sample Application</h2>
    </body>
    </html>
    

    `

  2. Next you create another file Dockerfile with the following code.

    AWS CoPilot

    FROM nginx:latest
    EXPOSE 80
    COPY ./index.html /usr/share/nginx/html/index.html 
    

    `

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

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

    `

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

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

    AWS CoPilot

  4. It will start creating the deployment environment.

    AWS CoPilot

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

    AWS CoPilot

  6. You can access the URL to verify the application deployed.

    AWS CoPilot

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

AWS CoPilot

ECS Cluster and Service:

AWS CoPilot

Load Balancer:

AWS CoPilot

ECR Image:

AWS CoPilot

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.

AWS CoPilot

Delete the dojoenvironment Cloud9 Environment.

Delete dojouser IAM User in the IAM Management console.

Thanks and hope you enjoyed the exercise.


Back to the Exercises