Transform Your AWS Infrastructure with Terraform: A Beginner’s Guide

Msweather
4 min readApr 21, 2023

Introduction:

Assume you are constructing a house. You can either assemble everything by hand or utilize plans to build your house more efficiently. Using infrastructure as code (IAC) is analogous to using blueprints. Instead of manually setting resources, you may manage your infrastructure with code. Terraform is a tool that can assist you in doing so in a more efficient and safe manner. This blog article will demonstrate how to utilize Terraform with AWS.

Step 1: Install Terraform

Installing Terraform on your PC must come first by downloading it. Consider Terraform to be your personal architect. Terraform is available for download from their website; simply install it on your PC by following the instructions.

After that, run the following command to verify that Terraform is installed:

terraform version

Step 2: Set up AWS Credentials

Setting up AWS credentials is necessary because Terraform uses the AWS API to generate and manage resources. You can accomplish this by generating an IAM user, an access key, and a secret key. You can set the access key and secret key as environment variables in your shell after you have them.

export AWS_ACCESS_KEY_ID="your-access-key-id"
export AWS_SECRET_ACCESS_KEY="your-secret-access-key"

Step 3: Create a Terraform Configuration

The following step is to generate a Terraform configuration file. This file will explain the resources you wish to generate as well as their attributes. Make a new file called main.tf and add the following code:

provider "aws" {
region = "us-east-1"
}

resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}

This configuration will launch an EC2 instance with the AMI ID `ami-0c55b159cbfafe1f0` and the instance type `t2.micro` in the `us-east-1` region.

A more detailed example is where we will use a default VPC for our EC2 and will attach an EBS to it.

# Specify the provider and region
provider "aws" {
region = "us-east-1"
}

# Create a default VPC
resource "aws_default_vpc" "def" {}

# Create a security group for the instance
resource "aws_security_group" "sg" {
name_prefix = "instance-"
vpc_id = aws_default_vpc.def.id

ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}

# Create an EBS volume
resource "aws_ebs_volume" "ebs" {
availability_zone = "us-east-1a"
size = 8
type = "gp2"
}

# Create the EC2 instance
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
key_name = "my-key"
vpc_security_group_ids = [aws_security_group.sg.id]

# Attach the EBS volume
root_block_device {
volume_type = "gp2"
volume_size = 8
delete_on_termination = true
}

# Mount the EBS volume
ebs_block_device {
device_name = "/dev/xvdf"
volume_id = aws_ebs_volume.ebs.id
}
}

In this case, we specify the AWS provider and region first. The aws_default_vpc resource is then used to establish a default VPC. The aws_security_group resource is then used to construct a security group for the instance. On port 22, this security group enables inbound SSH traffic.

The aws_ebs_volume resource is then used to construct an EBS volume. This volume is 8GB in size and employs the gp2 volume type. The availability zone is set to us-east-1a.

Finally, we use the aws_instance resource to construct the EC2 instance. The AMI is ami-0c55b159cbfafe1f0 (the Amazon Linux 2 AMI ID), the instance type is t2.micro, and the key name is my-key. We also specify the instance’s security group ID and attach the EBS as the root block device with delete_on_termination set to true. We also mount the EBS volume as an additional block device with a device name of `/dev/xvdf `.

Step 4: Initialize Terraform

After you’ve completed your blueprint, you must notify Terraform. Consider this to be similar to uploading your blueprint to a system so that it may be read and followed. To accomplish this, execute the terraform init command in the same location where you saved your main.tf file. This will install the required plugins and configure your Terraform environment.

terraform init

Step 5: Plan and Apply

You can now view what modifications Terraform will make to your infrastructure after you’ve launched it. Consider it like previewing your home before you begin construction. You can accomplish this by issuing the terraform plan command.

terraform plan

If everything seems good, use the terraform apply command to build your infrastructure. This will construct your infrastructure according to the plan you created in main.tf.

terraform apply

It will ask for a confirmation if you want to really create the infra. If you are sure you want to create and want to bypass a manual step use this.

terraform apply --auto-approve

Step 6: Destroy resources

If you no longer require your infrastructure, use the terraform destroy command to destroy it. This will erase all of the Terraform resources you’ve built.

terraform destroy

Conclusion:

In summary, Terraform is a tool for managing your infrastructure as code. It can be used to build and manage AWS resources more efficiently and safely. You can get started with Terraform and AWS by following the instructions provided in this blog post. Consider Terraform to be your blueprint creator and AWS to be your provider. Terraform allows you to easily build and manage your infrastructure.

--

--