Terraform Basics - EC2 instance

January 28, 2020 ยท View on GitHub

Goal

Create EC2 instance running Amazon Linux AMI in the public subnet (default VPC) using aws_instance resource and aws_ami data-source.

Tasks

  1. Make sure to install Terraform 0.12
  2. Start with updating content of main.tf in this directory (see sub-tasks in that file)
  3. Optionally, split content into main.tf, variables.tf and outputs.tf for better readability

Solutions

Solutions are inside solutions/0-basic directory.

Extra tasks

  1. Verify that EC2 instance has been launched (using aws_instance data source, AWS CLI or AWS Console).

  2. Launch 2 similar EC2 instances (using count meta-argument on aws_instance resource) and output their public IPs.

  3. Explore various Terraform built-in functions and learn the difference between accessing element in a list using short form ([0]) and element() function.

  4. Connect to EC2 instance by SSH (eg, using EC2 keypair).

  5. Using terraform-aws-elb module create a public-facing Elastic Load Balancer which will stay in front of EC2 instances launched by an autoscaling group.