Deploy Java microservices on Amazon ECS using AWS Fargate and Code Pipeline — Part 2

Shylesh Giri
7 min readJul 19, 2022

--

This article is a continuation of Part-1. In this we will discuss target groups, application load balancer, ECS cluster, task definition, service and at last how to set up CI/CD using AWS Code Pipeline.

Let’s begin with creating a target group and an application load balancer.

A target group tells a load balancer where to direct traffic to: EC2 instances, fixed IP addresses; or AWS Lambda functions, amongst others. When creating a load balancer, you create one or more listeners and configure listener rules to direct the traffic to one target group.

On the AWS console go to EC2 and on the left panel under load balancing select ‘Target Groups’. Then click on the create target group.

Select IP address as target type. This will register your container IPs with the target group. Next add a name for your target group and then add the port to which you are binding ECS container port.

Keep other settings as default unless you want to change it like the VPC. Add the health check endpoints which will be used by the load balancer to make decisions on traffic distribution. You can also set an additional threshold by clicking on the advanced health check settings.

If you have added the actuator dependency to your spring boot application you can use the actuator endpoint as the health check endpoint. Finally review and create the target group.

Next, we will create an application load balancer. Select load balancer from the EC2 console left panel.

A load balancer serves as the single point of contact for clients. The load balancer distributes incoming application traffic across multiple targets, such as EC2 instances, in multiple Availability Zones. This increases the availability of your application.

Click on ‘Create load balancer’ and then select application load balancer.

Next keep the default setting for scheme and IP address type.

For testing select the default VPC and select all the availability zones.

In the security group section click on create a new security group. Create a new inbound rule to access port 80 from anywhere.

Under listeners and routing select target group you created in the earlier step.

Finally create the load balancer. Post creation you will get the DNS name for your load balancer which can be accessed publicly.

Next let’s create an ECS cluster. From AWS console go to Elastic Container Service. Click on ‘Create cluster’ and then ‘Networking only’. Enter the cluster name and then click on create cluster.

An Amazon ECS cluster is a logical grouping of tasks or services. Your tasks and services are run on infrastructure that is registered to a cluster.

Next, we will create a task definition. On the left panel select the ‘Task Definition’ and then select launch type as AWS Fargate.

AWS Fargate is a technology that you can use with Amazon ECS to run containers without having to manage servers or clusters of Amazon EC2 instances.

A task definition is a set of instructions that tells Amazon ECS how to run Docker containers. Task definitions are written in JSON or manually configured through the AWS Management Console

Keep other settings as default and then select the task memory and CPU. For testing purposes, you can select the minimum value. Next click on the create container button.

Enter the name of the container and the repository URL that was created in Part-1 of this article. Make sure your container name matches with the name in the buildspec.yml. You can specify the soft limit for the container memory and then specify the port mapping for your container. The port should match the port specified in target groups. In the environment section you can specify CPU units based on how many tasks you will be creating though it is not mandatory. You can also specify the environment variables for your container e.g. DB_HOST and DB_PASSWORD. You can read more about setting environment variables for ECS task here.

Keep other settings as default and then create the task. You can create multiple versions of your task definition by updating it.

Amazon ECS service runs and maintains a specified number of instances of a task definition simultaneously in an Amazon ECS cluster.

Next, we will create a service which determines how and how many tasks should be created. Go to the cluster that was created in the previous step and then under the service tab click on create. Select the launch type as Fargate, then select the OS as Linux and then enter the task definition details that was created in the previous steps. Select the latest version of the task.

Next enter the service name, select the cluster and enter the number of tasks. Keep two as minimum but for testing you can keep it as 1. Select the deployment type as rolling update.

Next enter the network configurations. Select the default VPC for testing and select all default subnets available.

Also edit/create a security group to allow inbound traffic on port 8080. Next select the application load balancer and also enter the health check grace period. This will allow the load balancer to wait for the given time period before the health check starts.

Next select the load balancer that was created in the previous step and then click on ‘Add to load balancer’ button.

Then select the listener port and the target group name that was created earlier.

In the next step keep the default configuration for auto scaling which is not required at the moment. Finally review your configuration and then create the service. Wait for the task to be created and in running stage. If there are any issues with the tasks check the task or cloud watch logs and if necessary, adjust health check parameters. If there are no issues go to your target group to see the IP address of the container registered depending on the number of tasks created. You can then use the load balancer URL to test your service endpoints.

Now let’s set up CI/CD using the AWS Code Pipeline. From the AWS console go to the Code Pipeline.

AWS CodePipeline is a continuous delivery service you can use to model, visualize, and automate the steps required to release your software. You can quickly model and configure the different stages of a software release process. CodePipeline automates the steps required to release your software changes continuously

In step 1 enter the pipeline name under pipeline settings.

In step 2 select the source repository. This can be the git repository or any other from the drop down.

In step 3 under the build stage select the Code Build project that was created in Part-1 of this article.

In step 4 deploy stage select deploy provider as Amazon ECS and then select the cluster and the service name created in the previous steps.

Finally review and create the pipeline. Verify if all the stages have passed and check the ECR repository and also the ECS service and tasks. A new version must be added.

References

--

--

Shylesh Giri

Senior Software Engineer, Java Full Stack developer and an AWS and Google certified professional. https://in.linkedin.com/in/shyleshgiri