Terraform AWS EC2 Deployment - Part 3

2 minute read

In Part II we continued from single web EC2 instance, to an Auto-Scaling Load Balanced cluster of EC2 instances.

And now finally, let’s see some LIVE…. actually not really - I’ve got some video output below of things in action. I’ve had to make a few adjustments to the previous parts config’s but I’ll detail them as I talk you through the videos.

note: if you want to follow along you’ll need to do some setup from Part I

Terraform files

this is what we’re working with:


Launching the Terraform Apply

The Plan is good, it’s time to launch our plan into the Cloud/AWS. The bottom screen shows a watch aws ec2 describe-instance-status command which shows the moment an EC2 instance in born:

note: our ‘output.tf’ config had the ELB’s public DNS outputted so we can go and connect to it.

EC2 Cluster of Web Servers

check everything’s launched as we needed and the web servers are serving our web page:

Test Auto Scaling Setting

Let’s test the Autoscaling Group by killing one instance, and watch ASG launch another instance (remember the minimum acceptable instances is 2):

What was new/different from the previous configs? I’ve noted some of the changes below.

Added healthchecks

These are cool. It tells the Load Balancer what’s a “healthy” number of EC2 instances we want running(2 is the magic number here), sets timeout values and intervals.

resource "aws_elb" "ElasticLoadBalancer" {
  name = "ELBAutoScalingGroup"
  security_groups = ["${aws_security_group.ELBSecurityGroup.id}"]
  availability_zones = ["${data.aws_availability_zones.available.names}"]

  health_check {
    healthy_threshold = 2
    unhealthy_threshold = 2
    timeout = 3
    interval = 30
    target = "HTTP:${var.inbound_port}/"

  listener {
    lb_port = 80
    lb_protocol = "http"
    instance_port = "${var.inbound_port}"
    instance_protocol = "http"

ELB: Healthchecks Targeting EC2’s

AWS healthchecks are essentially “pings” to the EC2 endpoints defined by the ‘target’ parameter.

  • ‘HTTP’ is the ping protocol
  • ‘var.inbound_port’ is the ping port
  • the ping PATH the ‘/’ on the end.

Interpolating count for EC2 instance names (failed)

Well this failed miserably. I wanted to have each EC2 fire up with a unique name. Need to look into this more, but from the little I’ve read up on this approach it might not be the way to go.

Anyway, the new changes:


variable "count" {
  default = 1

auto.scaling.group.tf (note ‘tags’ not ‘tag’)

tags {
    Name = "${format("ASG-%03d", count.index + 1)}"
    propagate_at_launch = true


Overall this was a really good basic steps exercise in getting familiar with the building blocks of Terraform. It’s a really great infrastructure management platform and I think one that I’m going to get properly familiar with and I definitely recommend it as part of your DevOps toolchain.