Launch a Simple AWS EC2 Instance

Aug 2017

Introduction

By Simple I mean the very basic EC2 instance that you can have, a single EC2 Instance with basic storage and a DHCP IP Address (so no Elastic IP), and no S3 Buckets etc.
The idea is that using a free AWS account, you should be able to play with the information on this page and not incur any costs (I'm not guaranteeing that by the way). I am only concerned at this point in creating a Terroform script that will launch the EC2 Instance, I am not going to be installing any software on my EC2 Instance at this point.
If you have not created an EC2 Instance manually, then you possibly will not understand this process, so I suggest you check this page out first Creating an Amazon AWS EC2 Instance

The Script

Below is the script we will be using, I want to discuss a few of the elements first so that we understand a little of what changes to the script you might need to make to suit your purposes. (Ignore the line numbers)
<sxh> provider “aws” {

access_key = "JLJBK63HMPBC7DC150PA"
secret_key = "K7H2g33xPj6F7zBNcFGTeL5SlbjTsDNA/9nA2caa"
region = "eu-west-2"

}

resource “aws_instance” “example” {

ami = "ami-40a8bf24"
instance_type = "t2.micro"
key_name = "TestWebSvr"
security_groups= ["launch-wizard-1"]
tags {
Name = "terraform-instance"
}

} </sxh>

Access Key

This is not the same as your key pair, if you have created an EC2 instance manually, you will have created a key pair for SSH access, but the credentials here are not the same.
<sxh> provider “aws” {

access_key = "JLJBK63HMPBC7DC150PA"
secret_key = "K7H2g33xPj6F7zBNcFGTeL5SlbjTsDNA/9nA2caa"

</sxh>
We will look at how to create the 'access' and 'secret' keys further down. Please note that these keys in the above example are complete fabrications, please do not try to use them, you will totally waste your time.

Region

This really caught me out to begin with. If I look at the region I am running my instance in then it is reported as eu-west-2a. The region I am using (London) has two availability zones, eu-west-2a and eu-west-2b However you don't chose which one of these you use, it's automatic, so when you pick your region, you only specify the main region like this eu-west-2
<sxh>

region = "eu-west-2"

</sxh>

Instance AMI

The next section is where we setup our AMI (Amazon Machine Image) The AMI is what OS you wish to use (Linux, Winodws etc) and if Linux what version of Linux (Red Had, Debian etc).
<sxh>

ami = "ami-40a8bf24"

</sxh>
In the above example we have specified ami = “ami-40a8bf24”. To find this you need to log in to your AWS account and select 'Launch Instance'. You will see the page of AMI options and their associated AMI names.
Please be aware that AMI names vary across AZ availability zones. So the AMI name of Red Hat will differ between London and US.

Instance Type

The Instance Type is really the type of Virtual Server. This dictates how many vCPUs you have, the amount of RAM etc.
<sxh>

instance_type = "t2.micro"

</sxh>
In this example we are using the t2.micro, as this falls in to the free tier category (as long as you are still in your free tier period.)

EC2 Names

Key name is simply a name to identify the EC2 Instance as.
<sxh>

key_name = "TestWebSvr"
Name = "terraform-instance"

</sxh>
The Name = terraform-instance is another label to identify this by.

Security Groups

There are security groups that can be set in the Terraform script. You can create your own security groups or use the default one (default one that is created with a new EC2 Instance.)
<sxh>

security_groups= ["launch-wizard-1"]

</sxh>
The security groups “launch-wizard-1” is the default security group and has the default inbound settings of SSH, TCP, 22, 0.0.0.0/0 which allows for SSH access.