# How to create 1 ALB external and 2 EC2 ECS-NGINX with Terraform

**URL:** <https://minica.tech/t/how-to-create-1-alb-external-and-2-ec2-ecs-nginx-with-terraform/97>\
**Category:** Cloud computing\
**Tags:** aws, ec2, alb, elb, external\
**Created:** [January 21, 2021, 5:08pm UTC](https://minica.tech/t/how-to-create-1-alb-external-and-2-ec2-ecs-nginx-with-terraform/97 "2021-01-21T17:08:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vasile.Minica](https://minica.tech/user_avatar/minica.tech/vasile.minica/32/3_2.png) [@Vasile.Minica](https://minica.tech/u/Vasile.Minica)\
**Post date:** [January 21, 2021, 5:08pm UTC](https://minica.tech/t/how-to-create-1-alb-external-and-2-ec2-ecs-nginx-with-terraform/97/1 "2021-01-21T17:08:20Z")

</div>

First let’s set up our provider:

> <https://github.com/vmbs/Creating-1-ALB-external-and-2-EC2-ECS-NGINX-with-Terraform/blob/main/provider.tf>

We creates a new VPC:

> <https://github.com/vmbs/Creating-1-ALB-external-and-2-EC2-ECS-NGINX-with-Terraform/blob/main/vpc.tf>

We have the configuration for the Application Load Balancer:

> <https://github.com/vmbs/Creating-1-ALB-external-and-2-EC2-ECS-NGINX-with-Terraform/blob/main/alb.tf>

We set the AMI that will update and launch configuration of the cluster:

> <https://github.com/vmbs/Creating-1-ALB-external-and-2-EC2-ECS-NGINX-with-Terraform/blob/main/data.tf>

Adding the configuration for our ECS cluster:

> <https://github.com/vmbs/Creating-1-ALB-external-and-2-EC2-ECS-NGINX-with-Terraform/blob/main/ecs-cluster.tf>

And the tpl file:

> <https://github.com/vmbs/Creating-1-ALB-external-and-2-EC2-ECS-NGINX-with-Terraform/blob/main/ecs-cluster.tpl>

Adding our ECS service and task and Cloud Watch configurations:

> <https://github.com/vmbs/Creating-1-ALB-external-and-2-EC2-ECS-NGINX-with-Terraform/blob/main/ecs-nginx.tf>

Adding the IAM roles for the EC2 instances so they can communicate with the ECS service:

> <https://github.com/vmbs/Creating-1-ALB-external-and-2-EC2-ECS-NGINX-with-Terraform/blob/main/iam.tf>

We can see that the load balancer is open to the world on tcp/80 and tcp/443 and the ECS EC2 instances have ports 32768 to 65535 open from the load balancer. This is because when we select the container port to 0 in the task definition AWS will randomly assign a port from this range to the container:

> <https://github.com/vmbs/Creating-1-ALB-external-and-2-EC2-ECS-NGINX-with-Terraform/blob/main/security.tf>

We have our variables file:

> <https://github.com/vmbs/Creating-1-ALB-external-and-2-EC2-ECS-NGINX-with-Terraform/blob/main/vars.tf>

Also you can go to my personal GIT on [Vasile Minica / Creating-1-ALB-external-and-2-EC2-ECS-NGINX-with-Terraform · GitLab](https://gitlab.vm4it.com/vmbs/Creating-1-ALB-external-and-2-EC2-ECS-NGINX-with-Terraform)
