Running a node application on ECS over ALB
Create Application Load Balancer
First, we need an application load balancer to get a DNS, As we can’t get an elastic IP of task created on Fargate, It’s IP get changing whenever a container dies and regenerate. So, to use the container service we need an ALB, that will provide an endpoint, which we can use to access our application.
- First go to EC2, page. Click on load balancing. Create a new load balancer.
- Select Application load balancer.
- Provide a name to your ALB.
- Enter the port number at which you want the traffic for load balancer.
In my case I used port 80 to access over browser.
- Provide a network on which you want to set your ALB.
Next step, if you're using a 443 port for the load balancer, it’ll ask for an SSL certificate. If ACM (AWS Certificate Manager) is available in your region you can provide ACM key or you can generate your own. If it doesn’t exist, you can provide it to IAM.
And if you’re using port 80, it will not ask for a certificate. You can pass this stage.
- Select the security group.
I would prefer to open the same port, that you have used for load balancer.
- Provide routing data.
This is the step that makes your container run perfectly. Here you can mention on which port of your container you want to forward the traffic.
- Select the target type to IP.
It’ll work for fargate because fargate is not your instance. You can not access it with ssh. For EC2, you can use the target type as an instance.
- Provide port, on which you want to get all the traffic for your container. In my case, I was running my node application on port 3000, so I specified 3000 port.
- Register target
Provide a list of IP’s that you want to allow for your load balancer if you creating for fargate. Leave it blank. It will fetch automatically, once you configure your Service in ECS.
Review your changes and create your Application Load Balancer.
Creating a container for ECS
First, you need an image to upload on ECR, that will be used to create your environment on the container. So just follow some steps, and your cluster is up.
- Create a docker file.
- On the same folder take git pull of your git repository, that we’ll mount on the container side.
- Mount <application> folder of host server with /var/www/html/ of container server.
- Create an image from the docker file.
docker build -t image-name — no-cache.
Here I have used — no-cache to build an image, as for my node application I want to run npm install
each time my code changes take place.
- Now create a Repository for ECR image.
- Push your Docker image to ECR
docker tag hello-world aws_account_id.dkr.ecr.region.amazonaws.com/hello-repository
aws ecr get-login — no-include-email — region region
docker push aws_account_id.dkr.ecr.region.amazonaws.com/hello-repository.
Creating a Cluster
- Go to ECS, click create a cluster.
- Select fargate(Networking Only).
- Provide a Cluster Name.
- Click on create cluster, and your cluster is ready to use.
Create Task Definition.
- Click on the Create new Task Definition
- Next, select fargate and proceed
- Provide Task Definition name and Task role.
- Provide Task Execution Role
- Fill up your Task memory and Task CPU size
- Add container for your Task Definition
- Provide container name and container image from ECR, that you had uploaded earlier.
- Provide a soft limit/hard limit for your container. It is basically ‘memory’ and ‘memoryReservation’ parameters, respectively, in task definitions.
- Provide port mapping, on which port your container works. In my case, my node application needs port 3000 to run.
No need to do extra config, until and unless you need something extra.
- Click on add to add a container.
- Click on create to create Task Definition.
Create Service
- Go to the cluster, that you have created and click on create service to create a new service.
- Select launch type to Fargate.
- Provide service name and number of Tasks you want to run at a single time.
- Next, select the VPC, on which you have created your ALB.
- Provide subnet and security group (the port you want to open for your container.).
- Select the Load Balance that you have created earlier.
- Add load balance to the container. Click on Add to Load Balancer
- Fill the port number, for your container. Whether you want to open your application as HTTP or HTTPS.
Provide the target type for your application, that you have created earlier with the load balancer.
- Next, add auto-scaling config to your container, if you want to auto-scale. It will auto-scale if the load increases on your container.
- Finally, review your changes, and create your service.
You can see your container running at the Task tab in particular services.
Test your Application
- Open a running task, you’ll get an IP address over there
- Open that IP address with the port in browser, ip_address:port
But we don’t really want people to access our task-instances directly.
So go to EC2, security group and edit the security group that you have selected for your service.
Now, go to load balance, copy the Group Id of the load balancer that you have selected and copy it to the Security group.
Finally, your DNS is ready to use. Enjoy.