User Tools

Site Tools


the_script

The Script

Aug 2017

|


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.) Once we have looked at the elements of the script, we will create our own script and test it.

<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 Key Pair

The Key Name is the name of the Key Pair you wish to use with the EC2 Instance you will create.
<sxh>

      key_name = "TestSvr"

</sxh>
This script does not create a key pair, it must already exist for the script to work.


EC2 Names

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

      Name = "terraform-instance"

</sxh>
The Name is just a label so you can identify this EC2 Instance when it is in a list with other instances, ensure this name is unique or you will cause yourself some confusion.


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.

The Security Group must already exist, the Terraform Script does not create this, it simply points to an existing one.

the_script.txt · Last modified: 2023/03/09 22:35 by 127.0.0.1