Terraform from the beginning: notes from my first AWS EC2 deployment - Part 3

Let’s Recap! In Part I we learn why I found myself wanting to learn a few basics of Terraform.

Then 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:

auto.scaling.group.tf
elastic.load.balancer.tf
launch.configuration.tf
output.tf
provider.tf
security.groups.tf
variables.tf

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.

elastic.load.balancer.tf
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:

variables.tf

variable "count" {
  default = 1
}

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

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

Conclusion

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.

References

Leave a Comment