What we will need for this exercise:
Text editor (VS Code):
I recommend using Visual Studio Code (VS Code) for writing and managing your Terraform configuration files. You can install VS code here
AWS Account:
To follow along with this tutorial, you'll need an AWS account. If you don’t have one yet, you can create it here.
AWS CLI:
Before we get started, ensure you have the AWS CLI installed on your machine. This tutorial will be demonstrated on a Mac, but if you're using Windows, you can follow along by using PowerShell instead of the macOS Terminal. The commands and setup steps will be similar, with only minor differences in syntax or execution. You can follow the steps here if not yet installed.
Once installed, you can log into your AWS account through the CLI using the steps outlined here
Let’s dive in!
What is Terraform?
Terraform is an Infrastructure as Code (IaC) tool developed by HashiCorp that allows you to define, provision, and manage cloud resources in a declarative manner. Instead of manually configuring infrastructure through cloud provider consoles, Terraform enables you to define your infrastructure using simple configuration files written in HashiCorp Configuration Language (HCL) or JSON.
Why Use Terraform?
- Multi-Cloud Support – Works with AWS, Azure, Google Cloud, and many other providers.
- Declarative Configuration – Define what you need, and Terraform figures out how to create it.
- Version Control – Infrastructure configurations can be stored in Git for tracking changes.
- Automated Deployment – Easily create, update, and destroy infrastructure with simple commands.
- State Management – Keeps track of deployed resources to prevent conflicts and drift.
How Terraform Works
-
Write Configuration: Define infrastructure in
.tf
files. -
Initialize Terraform: Run
terraform init
to set up the working directory. -
Plan Changes: Use
terraform plan
to preview what Terraform will do. -
Apply Configuration: Run
terraform apply
to create or update resources. -
Destroy Infrastructure: Use
terraform destroy
to remove resources when they’re no longer needed.
Step 1: Installing terraform:
On Windows:
You can install Terraform on Windows using Chocolatey or manual installation.
Using Chocolatey (Recommended)
If you have Chocolatey installed, run the following command in PowerShell (Admin):
choco install terraform -y
On Mac:
You can install Terraform on macOS using Homebrew or by manual installation.
Using Homebrew (Recommended)
If you have Homebrew installed, simply run:
brew tap hashicorp/tap
brew install hashicorp/tap/terraform
Manual Installation:
Alternatively, you can follow this link to manually install Terraform on your windows or Mac machine. Scroll down to where it says "Install Terraform" and select your Operating System.
Step 2: Configuring your Project:
Link IAM credentials to Terraform AWS provider:
To use your AWS IAM credentials to authenticate the Terraform AWS provider, be sure to set the environment variables for your AWS access keys by running these commands.
export AWS_ACCESS_KEY_ID=<your-access-key-id>
export AWS_SECRET_ACCESS_KEY=<your-secret-access-key>
Write File Configurations
In the terminal (Mac) or Powershell (Windows), Run this command to create and navigate to a new folder (titled terraform-aws-practice). Here we will set up the files needed to deploy our demo EC2 instance
mkdir terraform-aws-practice && cd terraform-aws-practice
Open the folder in an editor (VS code in this example)
- Create a file
Paste the following code into main.tf and save:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
required_version = ">= 1.2.0"
}
provider "aws" {
region = var.aws_region
}
resource "aws_instance" "app_server" {
ami = var.instance_ami
instance_type = var.instance_type
tags = {
Name = var.instance_name
}
}
Explanation of the main.tf Configuration:
- Terraform Block: Specifies the required AWS provider version.
- Provider Block: Defines AWS as the cloud provider and sets the region using a variable (to be defined shortly).
- Resource Block: Creates an EC2 instance with an Amazon Machine Image (AMI) ID, instance type, and a name tag.
- Values beginning with "var." are to be referenced in a file called "variables.tf" that we will create next
- Create a new file
Copy and paste this code in variables.tf and save the file:
variable "aws_region" {
description = "The AWS region to deploy resources"
type = string
default = "us-west-2"
}
variable "instance_ami" {
description = "The AMI ID for the EC2 instance"
type = string
default = "ami-830c94e3"
}
variable "instance_type" {
description = "The instance type for the EC2 instance"
type = string
default = "t2.small"
}
variable "instance_name" {
description = "The name of the EC2 instance"
type = string
default = "ExampleAppServerInstance"
}
Explanation of the variables.tf configuration:
The variables.tf
file in Terraform is used to define input variables for your Terraform configuration. These variables allow you to customize your infrastructure without modifying the core configuration files, making your setup more flexible and reusable. In this example, we have:
- aws_region: The AWS region to deploy resources, with a default of us-west-2.
- instance_ami: The Amazon Machine Image (AMI) ID for the EC2 instance, with a default value.
- instance_type: The type of EC2 instance to launch, defaulted to t2.small.
- instance_name: The name of the EC2 instance, set to ExampleAppServerInstance by default.
- Copy and paste the following code in a new file called "terraform.tfvars":
aws_region = "us-west-2"
instance_ami = "ami-830c94e3"
instance_type = "t2.micro"
instance_name = "ExampleAppServerInstance"
Explanation of the terraform.tfvars file:
The terraform.tfvars
file is used to define values for the variables declared in your Terraform configuration, typically in the variables.tf
file. This allows you to separate the variable definitions from their actual values, providing flexibility and simplifying the management of different environments or configurations. In this example, we have:
aws_region: Specifies the AWS region to deploy resources (in this case, us-west-2).
instance_ami: Provides the Amazon Machine Image (AMI) ID for the EC2 instance.
instance_type: Defines the type of EC2 instance (e.g., t2.micro).
instance_name: Sets the name of the EC2 instance.
Step 3: Configuring your Project and Creating Infrastructure:
Now that you have your configuration files set up, it's time to configure your project and prepare it for deployment.
1. Initialize the Terraform
In the terminal (Mac) or PowerShell (Windows), navigate to your project directory (terraform-aws-practice
) and run the following command to initialize the Terraform project. This will download the required provider plugins and set up your project for Terraform execution:
terraform init
You should get a message like this:
2. Verify the Configuration Files
Before deploying, you can validate your Terraform configuration files to ensure they are properly structured and free from errors. Run the following command:
terraform validate
If your configuration files are correct, you should see a message indicating that the configuration is valid, similar to this:
3. Plan the Infrastructure Deployment
Next, use the terraform plan command to preview the infrastructure changes Terraform will make based on your configuration. This step helps you ensure that the plan matches your expectations:
terraform plan
The output will show you a summary of the resources Terraform will create, modify, or destroy. Review this output carefully.
4. Apply the Configuration
If everything looks good in the terraform plan output, you can apply the configuration and deploy your resources. Run the following command:
terraform apply
5. Check Your AWS Console
Once the deployment is complete, you can log in to the AWS Console and navigate to the EC2 Dashboard to verify that the EC2 instance has been created according to the configuration.
By following these steps, your Terraform configuration should successfully deploy an EC2 instance to AWS.
6. Terminate the instance with Terraform
When you're done with your AWS resources and no longer need them, it's important to clean up to avoid unnecessary costs. Terraform makes this easy with the terraform destroy command. This command will destroy all resources defined in your Terraform configuration, ensuring you're not charged for resources you don't need.
In the terminal, still in the project root folder, enter:
terraform destroy
This will terminate the instance. Check your AWS console to confirm that the instance has been removed
In this tutorial, we've learned how to use Terraform to provision and manage AWS resources. By following the steps outlined, we were able to:
- Set up a development environment with Terraform and AWS CLI.
- Configure a simple AWS EC2 instance deployment with Terraform, including setting up main.tf, variables.tf, and terraform.tfvars.
- Authenticate Terraform with your AWS credentials and deploy resources.
- Finally, clean up by using terraform destroy to terminate your resources and avoid unnecessary charges.
Top comments (0)