Setting up infrastructure on AWS using Terraform (Part 1)
For those new to Infrastructure-as-Code (IaC), this guide will walk you through setting up infrastructure on AWS using Terraform.
IaC empowers developers to build and provision servers efficiently. It allows for clear, repeatable steps to set up resources, ensuring readability and reducing human error. One key advantage of IaC is the ability to define resources once and create them multiple times, effortlessly generating dozens, hundreds, or even thousands of resources.
Moreover, IaC offers the benefit of version control, eliminating the reliance on potentially outdated or misplaced notes.
Introducing Terraform
As per HashiCorp’s own documentation:
“Terraform is HashiCorp’s infrastructure as code tool. It lets you define resources and infrastructure in human-readable, declarative configuration files, and manages your infrastructure’s lifecycle. Using Terraform has several advantages over manually managing your infrastructure.”
Installation
Refer to this link for Terraform installation instructions tailored to your operating system.
https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli)
Building Our First AWS Infrastructure
In this initial exercise, we’ll create an EC2 instance on AWS. An EC2 instance is a virtual machine capable of running various services, based on different operating system images. For this example, we’ll be working with an Ubuntu-based EC2 instance.
EC2 instances are virtual machines that can run various services, and for this exercise, we’ll use an Ubuntu-based one.
Before proceeding, ensure you have Terraform installed and configured AWS CLI credentials.
Verify Terraform installation by running terraform — version
in your terminal.
Now, let’s start.
- Create a folder in a memorable location and navigate to it using your preferred code editor. I recommend using VSCode and suggest installing the official Terraform plugin for a smoother experience.
- In this folder, create a file named
main.tf
.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
required_version = ">= 1.0.11"
}
provider "aws" {
region = "eu-west-2"
}
resource "aws_instance" "web_server" {
ami = "ami-04842bc62789b682e"
instance_type = "t2.micro"
tags = {
Name = "ExampleWebServer"
}
}
Explanation:
- The terraform block contains essential settings for Terraform. The crucial element here is required_providers, which specifies the AWS provider to be used for resource creation.
- The provider block designates AWS as the provider for resource management. We’re setting the region to eu-west-2.
- The resource block defines the type of resource (in this case, aws_instance), and assigns it a name (web_server). We’re using an Ubuntu-based AMI and a t2.micro instance type. Tags are applied for identification.
Initializing and Validating
Now, initialize the directory by running terraform init
from the command line. This will download and install the specified providers and plug ins.
Verify your configuration syntax with terraform validate
.
Applying the Configuration
Execute terraform apply
to see the execution plan. Confirm the changes to proceed to apply your configuration.
Reviewing the Outcome
Review the execution plan and confirm if yo`u’re satisfied. Upon successful execution, you’ll see a confirmation message along with the details of the created EC2 instance. You can check your AWS console to see the newly created EC2 instance
Important Note: Terraform created a terraform.tfstate file in your directory to track changes to your infrastructure. Ensure not to delete this file. Ideally, it’s recommended to store Terraform state remotely, such as in AWS S3 and DynamoDB, especially when collaborating in a team.
Making Configuration Changes
If you wish to make changes to the configuration, like upgrading the Ubuntu version, simply edit the main.tf file, and rerun terraform apply. Terraform will generate a new execution plan reflecting the changes.
Destroying Infrastructure
To remove the created resources, execute terraform destroy
. Terraform will present an execution plan indicating the resources to be destroyed. Upon confirmation, the EC2 instance will be removed.
You should see the execution plan and the summary:
Plan: 0 to add, 0 to change, 1 to destroy
.
Once you confirm, terraform will go ahead and remove the EC2 instance we created.
This concludes the initial steps into setting up infrastructure on AWS using Terraform. For Part 2, I will explore provisioning multiple EC2 instances with a load balancer.