How to deploy Docker Hub-hosted microservices in AWS ECS
It's surprisingly easy to deploy microservices in the AWS cloud. AWS Elastic Container Service (ECS) can pull images from Docker Hub and manage container workload with low-cost AWS Fargate clusters. In the accompanying video, we configured the AWS ECS and subsequently deployed RESTful Spring Boot microservices in AWS. It took a little over five minutes. That's how easy and straightforward deploying microservices to AWS ECS can be.
AWS ECS tutorial prerequisites
To start, you'll need an image hosted on Docker Hub. If you don't have your own, you can use the publicly available one that is used in this example.
AWS ECS quick start
The first step to deploy microservices in AWS is to log in to the Amazon cloud dashboard and select AWS ECS. Three links appear for configuring ECS -- namely, Repositories, Task Definitions and Clusters.
The Repositories section describes an AWS-hosted storage area in which you can upload Docker images of your own, eliminating the need for Docker Hub. In my experience, however, the process for uploading Docker images to AWS ECS is a bit cumbersome. Since this tutorial uses Docker Hub, we can skip the topic of repositories and move right into the creation of task definitions.
AWS ECS task definitions
You use an AWS ECS task definition to specify the memory and CPU resources that will be assigned to the container image at runtime. The AWS ECS task definition also requires a reference to the actual Docker image to run, which, in this case, will simply be a reference to the one hosed on Docker Hub.
For this AWS ECS tutorial, create a new task definition named rps-task. Set the memory to 1,024 mebibytes and the CPU to 512 shared units. For the container image, use the name rps-image-on-docker-hub, and specify the image location as docker.io/jheguevara/pl-rps-service.
When configuring the container, ensure that mappings for ports 80 and 8080 exist.
With those settings specified, the ECS task definition is complete.
AWS ECS cluster configuration
An ECS task needs a cluster upon which to run. Select the Clusters link, and choose to create a new cluster. Select an AWS Fargate cluster template, and assign it the name aws-fargate-cluster. Once these options are specified, click Create.
To run the AWS ECS task, select aws-fargate-cluster, and in the Tasks tab, choose the Run New Task option. Specify a Fargate launch type, and then choose the first valid option in the Cluster VPC and Subnets drop-down boxes.
For the container to be visible on the public internet, inbound rules for security groups need to be specified. To do this, choose the option to edit the security groups, and add All TCP and All traffic to the inbound rules.
After the AWS ECS security groups are configured, click the Run Task button. This will start the Docker container, subsequently completing the process of deploying a container hosting Spring Boot-based microservices in AWS.
Microservices in AWS ECS
By inspecting the task running on the cluster, you can find the public IP address that ECS has pinned to the container. By using this IP address and specifying port 8080, you can access the Spring Boot app deployed as a microservice in AWS. It's a simple rock-paper-scissors application coded using Spring Web model-view-controller and Spring REST APIs. When the application runs, you can pat yourself on the back for having successfully deployed the microservice to AWS.