How to deploy containers on AWS

Want to deploy a container-based micro-service on the cloud? AWS provides an easy way to get started. Here are some quick instructions.

Once you have an account created with AWS, the first step is getting your Mac/PC ready.

Setup Mac/PC

Installing ECS CLI

Install CLI tools by running the following commands:
sudo curl -o /usr/local/bin/ecs-cli https://s3.amazonaws.com/amazon-ecs-cli/ecs-cli-darwin-amd64-latest
sudo chmod +x /usr/local/bin/ecs-cli

Configure CLI

Run the following command with your access key and secret key

ecs-cli configure --region us-west-2 --access-key $AWS_ACCESS_KEY_ID --secret-key $AWS_SECRET_ACCESS_KEY --cluster ecs-cli-demo

Deploy Containers

Before deploying the containers, you need to create a cluster with 1 or more EC2 instances.  Follow the steps below to get the cluster up as well as deploy the first set of containers.

Create a key pair

EC2 requires key pairs to login to instances. Run the command below to create a key pair.
aws ec2 create-key-pair --key-name MyKey --query 'KeyMaterial' --output text > MyKey.pem

Display the keypair
aws ec2 describe-key-pairs --key-name MyKey

Start the cluster

The following command will start a cluster with 2 machines of type t2.medium
ecs-cli up --keypair MyKey --capability-iam --size 2 --instance-type t2.medium

These two instances will show up on the AWS console under EC2 instances. In order to login to the instances, do the following.
  1. Go to the security group associated with the instance and edit it to add ssh port
  2. Find the public IP of the instance and do "ssh -i MyKey.pem ec2-user@ip-address"

Create a compose file

The compose allows defining the properties of containers that are going to be deployed. The sample compose file below, defines a wordpress and mysql containers. The host port of 80 is mapped to the internal port 80 of the wordpress container to allow external web access. The wordpress is automatically linked to the mysql for database access.

Save the following file to wordpress.yml file.

version: '2' services: wordpress: image: wordpress cpu_shares: 100 mem_limit: 524288000 ports: - "80:80" links: - mysql mysql: image: mysql cpu_shares: 100 mem_limit: 524288000 environment: MYSQL_ROOT_PASSWORD: password

Deploy containers

The following command deploys the containers to the cluster that we previously created.
ecs-cli compose --file wordpress.yml  up

Check to see if the containers are by running:
ecs-cli ps

You can scale the container by running:
ecs-cli compose --file wordpress.yml scale 2

Cleanup

You can remove just the containers by running:
ecs-cli compose --file wordpress.yml down

If you want to bring down the entire cluster, run
ecs-cli down --force




Comments