Creating an Ubuntu EC2 instance with Terraform

We have previously created the requirements to launch a standard virtual machine on AWS EC2 (an SSH key pair and a security group). Let's now launch this virtual machine on EC2, using the specified SSH key pair to log into it and placed inside the security group, so (in our case) SSH is only available from a specific IP address.

Note

This example uses the t2.micro instance available for free in the AWS Free Tier.

Getting ready

To step through this recipe, you will need the following:

  • A working Terraform installation
  • An AWS provider, a SSH key pair, and a Security Group configured in Terraform (refer to the previous recipes)
  • An Internet connection

How to do it…

First, you need to find the correct AMI for your machine. An AMI is like a system disk image for AWS, and is referred to by its ID (that is: ami-df3bceb0 or ami-f2fc9d81). In the Ubuntu case, you can find the AMI you want by going to their Amazon EC2 AMI Locator page (https://cloud-images.ubuntu.com/locator/ec2/). In this case, I selected a Xenial release (16.04 LTS), on the eu-west-1 zone (Ireland), running on HVM virtualization and backed by SSD disks. This leaves us with one result—ami-ee6b189d:

How to do it…

Start by declaring this variable in the variables.tf file started in the first recipe, using a default value corresponding to the AMI ID we found previously:

variable "ami" {
  default = "ami-ee6b189d"
}

Now let's declare the instance type, specifying it as a default:

variable "aws_instance_type" {
  default = "t2.micro"
}

Let's use those variables to create the Terraform aws_instance resource. Locally declared variables are available using the ${var.variable_name} structure, and internal resource attributes are accessed using the ${resource_type.resource_name.attribute} structure:

resource "aws_instance" "dev" {
  ami                         = "${var.ami}"
  instance_type               = "${var.aws_instance_type}"
  key_name                    = "${aws_key_pair.admin_key.key_name}"
  security_groups             = ["${aws_security_group.base_security_group.name}"]
  associate_public_ip_address = true

  tags {
    Name = "Ubuntu launched by Terraform"
  }
}

Apply the following code:

$ terraform apply
aws_key_pair.admin_key: Creating...
[…]
aws_security_group.base_security_group: Creating...
[…]
aws_instance.dev: Creating...
[…]

Navigate to the AWS EC2 dashboard under Instances | Instances, select your instance and note the public IP:

How to do it…

Try to log into it:

$ ssh -i keys/aws_terraform [email protected]
Welcome to Ubuntu 16.04.1 LTS (GNU/Linux 4.4.0-36-generic x86_64)
ubuntu@ip-172-31-18-156:~$

You can apply and apply by refreshing its state against Amazon's as Terraform knows remote and local states are the same, and therefore it doesn't recreate endlessly new VMs each time.

You've successfully launched your first AWS EC2 instance using repeatable Terraform code!

Scaling the number of instances

What if you want to launch two similar instances, maybe for debugging purposes, or for instant action behind a load balancer? It's very easy with Terraform, just use the count option inside the aws_instance resource, and that will launch the required amount of instances:

count = 2

Next, terraform apply this and observe Terraform automatically creating a new machine according to the counter:

$ terraform apply
aws_key_pair.admin_key: Refreshing state... (ID: admin_key)
aws_security_group.base_security_group: Refreshing state... (ID: sg-d3dbd8b4)
aws_instance.dev.0: Refreshing state... (ID: i-0018b1044953371ae)
aws_instance.dev.1: Creating...
[...]
aws_instance.dev.1: Creation complete

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

The second server shows up in the AWS Console:

Scaling the number of instances

Note

Remember, the command to destroy a Terraform infrastructure is terraform destroy.

There's more…

We can achieve similar results using Ansible. Here's how it looks, using admin_key and base_security_group created in the previous recipes:

---
  - name: dev instance
    ec2:
      key_name: admin_key
      group: base_security_group
      instance_type: t2.micro
      image: ami-ee6b189d
      wait: yes
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.134.151