You now have a container that runs on your local machine. The next step is to get this container uploaded off your local machine, into your AWS account, and launch it there.

Pushing your image to a private container registry

The way you get your container image into your AWS account is by creating a container registry and pushing your container image to it. Just like a git repository is a place where you can push your code and it keeps track of each revision to the code, a container registry is a place where you can push your built images, and it keeps track of each revision of your application as a whole.

The AWS service for storing your container images is Elastic Container Registry (ECR). You can create a new private registry for your application by navigating to ECR in the AWS dashboard and clicking the “Create repository” button:

After you click the button and enter a name for the repository you are given a series of commands you can use to push to the repository. The first step is to login to your private registry:

`aws ecr get-login --no-include-email --region us-east-1`

Then you need to retag the local image you built in the previous step so it can be uploaded to the new repository:

docker tag chat:latest 209640446841.dkr.ecr.us-east-1.amazonaws.com/chat:v1

And then push the image:

docker push 209640446841.dkr.ecr.us-east-1.amazonaws.com/chat:v1

Once the image has uploaded you can see it show up in the AWS console when you view the details of the chat repository:

The docker image is now uploaded to a private registry in your own AWS account. The next step is to run it!

Running the application from your docker registry

To launch the docker image on your account under AWS Fargate let’s use some CloudFormation templates from AWS. CloudFormation is an AWS tool which allows you to describe the resources you want to launch on your account as metadata. Then CloudFormation reads this file and automatically creates, modifies, or deletes resources on your behalf. This approach is called “infrastructure as code” and it allows you to quickly and automatically configure your AWS resources without making mistakes by mistyping something or skipping a step.

I’ve added the templates that are needed to deploy the application in a branch of the git repo:

git checkout 3-deployment

The templates you need to use are located at recipes/ inside the project repo. You can use the AWS CLI to quickly deploy a Fargate cluster using the template at recipes/public-vpc.yml

aws cloudformation deploy --stack-name=production --template-file=recipes/public-vpc.yml --capabilities=CAPABILITY_IAM

This command may take a few minutes while it sets up a dedicated VPC for the application, a load balancer, and all the resources you need to launch a docker container as a service in AWS Fargate.

Once it completes you can launch the template at recipes/public-service.yml to get your container running in the cluster. This time let’s use the console so its easier to enter all the parameters.

You can navigate to the CloudFormation console and click the “Create Stack” button, then choose a file to upload. You want to upload the file at services/public-service.yml

After upload you are greeted by a screen where you can customize the parameters:

There are a number of parameters here but the only ones we need to worry about right now are:

Stack Name

Let’s name the CloudFormation stack itself chat

ImageUrl

This is the image you uploaded earlier, the same value from the docker push command, something like:

209640446841.dkr.ecr.us-east-1.amazonaws.com/chat:v1

DesiredCount

This is how many copies of the container to run. The default in this template is 2 but we have not yet configured this app to be horizontally scalable, so you need to change this value to 1 (we will extend the application to be horizontally scalable in a follow-up article).

ServiceName

This is a name for the service itself. Once again you can call it chat

ContainerPort

This is the port number that the application inside the container needs to receive traffic on. You need to change it to 3000 since this is the default port that the Node.js app receives traffic on.