How to deploy your containers on AWS Fargate
AWS Fargate is a compute engine for Amazon ECS that allows you to run containers without having to manage servers or clusters. With AWS Fargate, you no longer have to provision, configure, and scale clusters of virtual machines to run containers. Let’s deploy
Step 1 Creating a docker image repository
First you need to:
- sign in to your AWS management console
- elastic container repository
- create a new repository. This is where you will store your docker images that will run in your Fargate cluster.
- By pressing the button named “View push commands” you will see all the commands needed to log in, build your image, and push it to the repository.
Step 2 Create a cluster
The next step is to create a cluster to run your applications in. Open the menu and search for ECS. Or if you are in your repository, select “Clusters” from the left menu.
- Click on getting started -> select cluster template powered by AWS Fargate -> next step
- Select new VPC or you can configure for your existing VPC later
- Configure cluster (network configuration) and click on create
Step 3 Create a Task Definition
The next thing we need to do is to create a task-definition. The task definition defines what docker image to run and how to run it. When creating a new Task Definition you will get the choice of launch compatibility.
- Choose Fargate and click next.
- Give your task a name.
- Select a task I AM role.
- Add the container(scroll down).
- Enter a name for the container and the path in your repository. Add the port mapping and set the port to the same port that your docker container exposes.
- Click add container.
- Set your preferred task memory and task CPU. Then Click on create.
Now go back to task definition and select the newly created task -> click on actions -> run task -> launch type: fargate -> select default subnet -> Click on run task. From the cluster click on the running task -> click on the eni id -> from details click on the security groups -> click on inbound -> click on edit and add a Type of custom TCP Rule for your_port source Custom and allow all 0:0:0:0/0 then click on save Then get the public ip from the running task and try http://<your_public_ip>:<your_port>
Step 4 Create a service
ECS-console -> enter your Cluster -> Click “Create” under the Services tab -> select Fargate -> enter the number of tasks-> choose a VPC and subnets -> security groups click on edit -> set a custom TCP-port if your container uses anything other than port 80 (later adjust the source when we have a load balancer set up)-> Name it Next up we need to define a load balancer. go to the EC2 Console -> Choose “application load balancer” -> select the load balancer protocol -> select the vpc and subnets -> Name the new load balancer-> Click next -> Configure Security Groups-> create another security group -> Configure routing -> Configure routing, we just need to define a Target for the load balancer ->review and create the load balancer add the load balancer to your service and select the container to load balance -> Select the target group and disable service discovery -> Click on next step and do not select any auto scaling features now -> Review and create the service Now test the task using the Public IP and the port. Go to EC2 Console -> select Security Groups -> Find the security group already created and edit -> inbound rules, add the load balancers Group ID to the source field of port This makes sure that only the load balancer can access the ECS-service on that port.
Step 5 Point a domain name to the application load balancer
go to Route 53 -> select the hosted zone your domain-> create a new record set for it to put it on a subdomain -> select an A-record from the list of types -> Select yes on the Alias option and select your load balancer from the list -> press create