[version_1.1]

Note

The exercises in this course will have an associated charge in your AWS account. In this exercise, you create the following resources:

Familiarize yourself with AWS Cloud9 pricing, AWS CloudFormation pricing, Amazon DynamoDB pricing,and the AWS Free Tier.

Setting up

This exercise requires an IAM role that will be used with the AWS Cloud9 instance. It also requires a DynamoDB table that will be set up and then used in a later exercise. The CloudFormation stack will create these resources for you. You will also need to choose a Region where AWS App Runner is available.

  1. Download the following CloudFormation template: exercise-containers.yml. This template will set up the backend resources that are needed to complete the exercise.

    Note: If you have an existing Virtual Private Cloud (VPC) and it has a Classless Inter-Domain Routing (CIDR) block of 10.16.0.0/16, you must edit the template to change it.

  2. Sign in to the AWS Management Console as a user that has administrator permissions.

  3. Choose Services, and search for and open CloudFormation.

  4. Choose Create stack.

  5. For Specify template, choose Upload a template file.

  6. Select Choose file and browse to where you downloaded the exercise-containers template.

  7. Select the exercise-containers template and choose Open.

  8. Choose Next.

  9. For Stack name, enter exercise-containers.

  10. Choose Next, and then choose Next again.

  11. Select the acknowledgement and choose Create stack. Wait for the stack to complete.

Exercise 1: Creating the First Container

In this exercise, you create an AWS Cloud9 instance and modify some of the environment settings. You also install the prerequisites and source code that are needed to launch new container instances inside your AWS Cloud9 environment.

Task 1: Creating an AWS Cloud9 instance

In this task, you will create an AWS Cloud9 instance.

  1. In the AWS Management Console, choose Services, and then search for and open Cloud9.

  2. Choose Create environment.

  3. For Name, enter containers-cloud9 and choose Next step.

  4. Configure the following settings.
  5. Choose Create.

Task 2: Modifying and deploying source code to AWS Cloud9

In this task, you will upgrade the AWS Command Line Interface (AWS CLI) version, and deploy the source code that’s needed to complete the exercise. You will also associate the instance profile with your AWS Cloud9 instance. Finally, you will expand your environment to give it more disk space.

  1. In the AWS Cloud9 terminal, upgrade the AWS CLI version by running the following command.

  2. Verify the version.

  3. Download and extract the source code that you need for this exercise.

  4. Replace the current association with the cloud9-containers-role.

  5. Make sure that the State is listed as associated.

  6. Run the following command.

    This command tells AWS Cloud9 to specifically disable the managed rotated credentials, and instead use the cloud9-containers-role that CloudFormation created from the template.

  7. Wait for a couple of minutes, and then run the following command:

    You should see an Amazon Resource Name (ARN) that matches a role in the CloudFormation template:

  8. Expand the AWS Cloud9 disk by running the following utility:

Task 3: Building the first container

In this task, you will pull the required images and build your first Docker container.

  1. Change directory to first-container/.

  2. Inspect the Dockerfile, and build a container image.

  3. View the Docker images.

    The application being served sits on port 8080 in the container image.

  4. Publish port 8080 to your AWS Cloud9 host and make it available on port 8080.

  5. View the running Docker containers.

  6. View the logs that are being captured from the container.

    You will now view the application in a browser.

  7. At the top of your AWS Cloud9 instance, choose Preview and then choose Preview Running Application.

    You should see the running application.

  8. Launch a shell inside the container.

    You can use this shell to run commands within the container itself.

  9. In the container, look at the contents of the /app folder and view the contents of /app/input.txt.

  10. In the container, view the process list.

  11. Escape out of the container (you can escape out of the container by pressing Ctrl+D).

  12. Stop and remove the running container.

Task 4: Modifying the container with new data

In this task, you will change the input.txt file inside the container with new data. You can use a bind mount to mount a local file resource in place of /app/input.txt inside the container.

  1. Create an ~/input.txt file with five words, with each word on a new line.

  2. Launch a container with the new file mounted in place of /app/input.txt.

    You can configure an application that’s running in a container with environment variables. The containerized application takes MESSAGE_COLOR as an environment variable.

  3. Visit the updated application in a browser by choosing Preview, Preview Running Application.

  4. Force-remove the container.