Container Services
BACK

Container Services

Services used: ECR, ECS, Fargate, Cloud9, VPC

Languages used: Python

Objective: Use Amazon ECS, Amazon ECR, and AWS Fargate to deploy a sample application. Create a docker image for an application. Create an ECR repository, and then push the docker image to it. Deploy an application with ECS and Fargate by using an image from ECR. Deploy a second application in Fargate by using the image from ECR. Validate access to the second application.

Search for and open Cloud9.

Click My environments and Open the Cloud9 IDE.

Click the Welcome tab and select a Main Theme.

Click the File dropdown menu and select Upload Local Files.

Choose Select files and upload the sample file. Close the pop-up box.

Open a new tab and select New Terminal.

Run the command prompt: unzip lab_code.zip.

Install and start the docker in the environment by running: ./install_scripts/install_docker.sh

Go to Environments and expand the first_app folder. Open the Dockerfile in a new tab. Review the Python code and return to the Bash tab.

Get the value of the region by running region=$(aws configure get region) and then region=${region:-us-east-1}. Create the repository variables by running the following in order: repo_name="my_app" then account=$(aws sts get-caller-identity --query Account --output text) and then fullname="${account}.dkr.ecr.${region}.amazonaws.com/${repo_name}:latest".

Create an ECR repository by running aws ecr create-repository --repository-name "${repo_name}" then retrieve an authentication token by running aws ecr get-login-password --region ${region}|docker login --username AWS --password-stdin ${fullname}

Create, build, and tag docker images by running cd ~/environment/first_app then docker build -t ${repo_name}

Verify the docker images by running docker images --filter reference=my_app and ensure that our repository my_app is present.

Now push a Docker image to ECR by running docker tag ${repo_name} ${fullname} then docker push ${fullname}

Compile and push the second_app image to ECR by running cd ~/environment/install_scripts/ then ./push_second_app.sh and then review the logs.

Return to the console and go to ECR.

Select Repositories then click the my_app name.

Copy the Image URL.

Search for and go to VPC.

Select Your VPCs and copy the Lab VPC ID.

Go to Security Groups and select the VPC ID that we copied in the previous step. Select the Inbound rules tab and click Edit inbound rules.

Delete the All traffic rule and click Add rule. Choose Custom TCP, Port range 8443 from anywhere. Click Save rules.

Search for and go to ECS.

Ensure that the Lab_cluster is present.

Click Task definitions and Create new task definition.

Name it first_App and ensure Fargate and Linux are selected.

Scroll down and choose .5 vCPU and 1 GB.

Scroll down to Container - 1 and name it my_app. Paste the my_app image URL copied earlier. Type 8443 for Container Port and ensure the protocol is TCP.

Scroll down and unselect Use log collection. Scroll down and click Create.

Wait for the Status to become Active then Click Deploy and select Run task.

Choose Lab_cluster with the Fargate Launch type.

Scroll down and review our configuration.

Go to the Networking section and select the Lab_VPC. Select only the public_subnetSubnet1.

Go to Task overrides and select the ecsTaskExecutionRole for both roles. Click Create.

Scroll down and click refresh until the status is Running. Click the task ID.

Copy the Public IP address of the server.

Paste in a new window replacing Public IP in: http://Public IP:8443 and ensure we see the message.

Return to ECR. Select Repositories and click my_second_app.

Copy the Image URL.

Return to ECS. Click Task definitions and Create new task definition.

Name it my_second_App and ensure Fargate and Linux are selected.

Scroll down and choose .5 vCPU and 1 GB.

Scroll down to Container - 1 and name it my_second_app. Paste the my_second_app image URL copied earlier. Type 8443 for Container Port and ensure the protocol is TCP.

Scroll down and unselect Use log collection. Scroll down and click Create.

Wait for the Status to become Active then Click Deploy and select Run task.

Choose Lab_cluster with the Fargate Launch type.

Scroll down to the Networking section and select the Lab_VPC. Select only the public_subnetSubnet2.

Go to Task overrides and select the ecsTaskExecutionRole for both roles. Click Create.

Scroll down and click refresh until the status is Running. Click the task ID.

Copy the Public IP address of the server.

Paste in a new window replacing Public IP in: http://Public IP:8443 and ensure we see the message. Success!