Run Your First Terraform Program on AWS
Terraform is an open-source infrastructure as code tool that allows you to build, change, and manage infrastructure in a consistent and repeatable manner using simple configuration files.
If you’re new to Terraform and looking to leverage its power for managing infrastructure on AWS, this guide is for you. Terraform on AWS is the Infrastructure as Code (IaC) tool, simplifies cloud resource provisioning, allowing you to automate and manage infrastructure efficiently. Let’s dive into how to run your first Terraform program on AWS.
1. Prerequisites
1.1 AWS Account
- Set up an AWS account at aws.amazon.com
1.2 Terraform Installation
- Download and install Terraform
1.3 Visual Studio Code Installation
- Install Visual Studio Code (VS Code).
- Install the HashiCorp Terraform extension in VS Code for syntax support and code completion.


- After Installing the extension, Set Up the Project Directory.
1.4 Set Up the Project Directory
- Create a new folder for your Terraform project files.
- Inside this folder, create a new file named main.tf



- Now, go to AWS Terraform Provider :

- Please navigate to the Provider section to obtain the base configuration code. Copy this code and paste it into your main.tf file for use in your Terraform project.

Here is the Code, Terraform provider Copy this code and paste it in main.tf
terraform {
required_providers {
aws = {
source = “hashicorp/aws”
version = “5.75.1”
}
}
}
provider “aws” {
# Configuration options
}
- Copy this code and paste it in main.tf

- To Configure the aws into terraform, go terminal type the following commands
Type aws configure and give enter :

- After applying commands, it will ask you to enter the aws access key id
To get the Access ID, go to AWS console and go to the Security Credentials.


- Go to Create Access key and create the key

- After creating, you will get the Access key and Secret Access key

- Copy the Access key and paste it in the terminal


- After entering the access and secret keys, it will prompt you for the region and output format. Leave it as it is and press Enter.
Now we should run our code. To run the resources, type the following commands: terraform init

- The terraform init command initializes your working directory by downloading provider plugins, setting up backend configuration, loading modules, and preparing Terraform to manage infrastructure based on your configuration files.
Before we move on to next step, we will add one IAM user using terraform. Go to the aws terraform provider and search IAM. You will get the code to create IAM using terraform.

Here is the code, aws_iam_user
Copy the code and paste it in our main.tf file
provider “aws” {
region = “us-east-1” # Change to your desired AWS region
}
resource “aws_iam_user” “example_user” {
name = “example-user” # Define the IAM user name
}
Here you can add any name and any region to create IAM Users

- Now give the command called terraform plan

- The terraform plan command previews changes Terraform will make to align the actual infrastructure with your configuration, and give the blue print like structure.
Now follow the command called terraform apply

- The terraform apply command executes the planned changes, creating, updating and resources
to match your configuration
Give yes to perform actions .

- You have successfully initialized your first Terraform code configuration in AWS. To check the IAM
user, go to the aws console and search iam and go to users

- The user was successfully added
Now the last command called terraform destroy


- Give yes to make destroy all resources.


- The terraform destroy command removes all resources managed by Terraform in your configuration, effectively deleting the infrastructure. To check, go to the aws console and go to the IAM Users

- Here there is no resource to display, because the resource was destroyed using terraform
Thus the users to display.