Terraform Logo

Getting Started with Terraform

Justin

--

In this tutorial you will set up your AWS account for Terraform, install Terraform, and deploy a single server.

Set Up Your AWS Account, User and Permissions

Install Terraform

There are a few options when installing Terraform. You can open https://www.terraform.io/downloads.html, download the package for your OS, and add it to your PATH environment variable.

If you use a package manager like brew you can install Terraform by running:

brew install terraform

To ensure the installation was OK and up to date, run

terraform -v

Set Up AWS credentials

Next we will need to give Terraform access to make changes in your AWS account, you will need to set the AWS credentials, in your Terminal, run:

export AWS_ACCESS_KEY_ID=<your access key id>
export AWS_SECRET_ACCESS_KEY_ID=<your secret access key id>

Environment variables only apply to the current shell session, you’ll have to export these variables again if you are using a different window/shell session.

Writing Terraform Script

Terraform is declarative language and is written in HashiCorp Configuration Language (HCL), files have the extension .tf.

resource "<PROVIDER>_<TYPE>" "<NAME>" {  [CONFIG...]}

PROVIDER — Name of a provider (e.g., aws)

TYPE — Type of resource to create in that provider (e.g., instance)

NAME — The logical name/identifier that you will be referencing throughout your Terraform code (e.g., testInstance)

There are two required parameters, ami and instance_type, to include in order to deploy a single EC2 Instance in AWS.

provider "aws" {region = "us-west-1"}resource "aws_instance" "testInstance" {  ami           = "ami-0577b787189839998"
instance_type = "t2.micro"
}

To put it simply, you are instructing the provider, aws, to create a resource in the region, us-west-1. This resource to be created will be an AWS instance with name “testInstance” and runs on an AMI with the AMI ID, “ami-0577b787189839998". The instance type, classifies how powerful we want out instance to be, in this will be a t2.micro which is AWS free tier eligible.

AMI stands for Amazon Machine Image, it is installed onto your EC2 instance and there is an entire marketplace for AMIs. To find the AMI id, choose an AMI from https://aws.amazon.com/marketplace/search and click “Continue to Subscribe” > “Continue to Configuration”. The AMI Id is included below the region text field.

Note: AMI Ids may not the same in different regions!

Note: Some AMI will cost you money to use, however, there are free AMIs to use and you can easily filter for it in the search bar.

Deploy a Server

Go into the folder where you created main.tf .Terraform needs to install the required providers, in this case, AWS. Run:

terraform init

A hidden folder is installed and it contains the code for the provider.

user@computer terraform % tree .terraform
.terraform
└── providers
└── registry.terraform.io
└── hashicorp
└── aws
└── 3.35.0
└── darwin_amd64
└── terraform-provider-aws_v3.35.0_x5

Next we can generate an execution plan to have a bird’s eye view of the resources we want to create/delete/modify. Run:

terraform plan

Anything with a “+” will be created, “-” will be deleted, and “~” will be modified in place.

Terraform will perform the following actions:# aws_instance.testInstance will be created
+ resource "aws_instance" "testInstance" {
+ ami = "ami-0518bb0e75d3619ca"
+ arn = (known after apply)
+ associate_public_ip_address = (known after apply)
+ availability_zone = (known after apply)
+ cpu_core_count = (known after apply)
+ cpu_threads_per_core = (known after apply)
+ get_password_data = false
+ host_id = (known after apply)
+ id = (known after apply)
+ instance_state = (known after apply)
+ instance_type = "t2.micro"
+ ipv6_address_count = (known after apply)
+ ipv6_addresses = (known after apply)
+ key_name = (known after apply)
+ outpost_arn = (known after apply)
+ password_data = (known after apply)
+ placement_group = (known after apply)
+ primary_network_interface_id = (known after apply)
+ private_dns = (known after apply)
+ private_ip = (known after apply)
+ public_dns = (known after apply)
+ public_ip = (known after apply)
+ secondary_private_ips = (known after apply)
+ security_groups = (known after apply)
+ source_dest_check = true
+ subnet_id = (known after apply)
+ tenancy = (known after apply)
+ vpc_security_group_ids = (known after apply)
+ ebs_block_device {
+ delete_on_termination = (known after apply)
+ device_name = (known after apply)
+ encrypted = (known after apply)
+ iops = (known after apply)
+ kms_key_id = (known after apply)
+ snapshot_id = (known after apply)
+ tags = (known after apply)
+ throughput = (known after apply)
+ volume_id = (known after apply)
+ volume_size = (known after apply)
+ volume_type = (known after apply)
}
+ enclave_options {
+ enabled = (known after apply)
}
+ ephemeral_block_device {
+ device_name = (known after apply)
+ no_device = (known after apply)
+ virtual_name = (known after apply)
}
+ metadata_options {
+ http_endpoint = (known after apply)
+ http_put_response_hop_limit = (known after apply)
+ http_tokens = (known after apply)
}
+ network_interface {
+ delete_on_termination = (known after apply)
+ device_index = (known after apply)
+ network_interface_id = (known after apply)
}
+ root_block_device {
+ delete_on_termination = (known after apply)
+ device_name = (known after apply)
+ encrypted = (known after apply)
+ iops = (known after apply)
+ kms_key_id = (known after apply)
+ tags = (known after apply)
+ throughput = (known after apply)
+ volume_id = (known after apply)
+ volume_size = (known after apply)
+ volume_type = (known after apply)
}
}
Plan: 1 to add, 0 to change, 0 to destroy.

To execute this plan, run:

terraform apply

🎊 Congratulations! You’ve created you’ve deployed your first EC2 instance using Terraform. Head over to the AWS console and select the correct region that you deployed your EC2 instance in. You should be able to see a Virtual Machine up and running!

--

--