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.
To step through this recipe, you will need the following:
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
:
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:
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!
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:
3.133.134.151