Nginx on ECS EC2 type in CloudFormation

Myron Zaiets
CloudFormation hacks
2 min readNov 23, 2022

--

Hi,

I was doing some testing with ECS and here I wanted to share with you some insights regarding this matter. As we all know there are 2 ways to implement ECS:

  1. ECS FARGATE — you pay for what you use, you run containers without EC2 instances.
  2. ECS EC2 — you pay for EC2 capacity, you are deploying EC2 instances for that.

A comparison can be found here: https://containersonaws.com/introduction/ec2-or-aws-fargate/

Here I want to show you how to imlement a simple Nginx on EC2 type of EC2. For that we need:

  • ECS cluster
  • ECS task definition — configuration for your containers (Docker image, cpu and so on…)
  • ECS service — run a required number of tasks, you can look at it as a deployment kind of type.
  • Autoscaling group
  • Load Balancer
  • All needed roles for each of the services.

Once we have all of that we can create our CloudFormation template. Here I am using a simple Nginx image, and I am running it in my default VPC and subnets. So once you want to use this template, you need to:

  • change capacity number of instances.
  • use the correct subnets.
  • use the correct VPC.

Once we deployed it, our task is running well:

--

--

Myron Zaiets
CloudFormation hacks

AWS Cloud Engineer/DevOps/✔6x AWS Certified/✔2x AWS Community Builder