Terraform Install & AWS Setup on Ubuntu

What is Terraform

Terraform is an open-source infrastructure-as-code (IaC) tool created by HashiCorp. It enables developers and operators to define and manage their infrastructure resources in a declarative and version-controlled manner. With Terraform, you can codify your infrastructure requirements using a simple and human-readable configuration language.

In essence, Terraform allows you to treat your infrastructure as code, enabling you to define, provision, and manage resources across various cloud providers (such as AWS, Azure, Google Cloud, etc.) and other infrastructure components (like networking, storage, and databases). This approach brings numerous benefits, including automation, reproducibility, scalability, and ease of collaboration.

By writing Terraform configuration files, commonly known as “Terraform scripts” or “Terraform code,” you define the desired state of your infrastructure. Terraform then analyzes these files, compares the defined state with the current state of your infrastructure, and automatically determines the actions required to reach the desired state. It orchestrates the creation, modification, and deletion of resources as needed, ensuring that your infrastructure matches the configuration you’ve defined.

Terraform also provides features such as dependency management, resource provisioning, and plan execution. It supports a wide range of resource types and offers extensive documentation, a vibrant community, and a growing ecosystem of plugins and modules that extend its functionality.

In summary, Terraform simplifies the process of managing infrastructure by allowing you to define and automate your infrastructure resources using code. It helps streamline deployments, reduces manual effort, and provides a reliable and consistent way to manage infrastructure across different environments. With Terraform, you can harness the power of infrastructure-as-code, making your infrastructure more efficient, scalable, and resilient.

Terraform Install

Open the termminal and run these steps

sudo apt-get update && sudo apt-get install -y gnupg software-properties-common
wget -O- https://apt.releases.hashicorp.com/gpg | \
gpg --dearmor | \
sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
gpg --no-default-keyring \
--keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg \
--fingerprint
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \
https://apt.releases.hashicorp.com $(lsb_release -cs) main" | \
sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update
sudo apt-get install terraform

Create Iam User

Go to AWS Console and type “IAM” on search bar.Then Create new user with administrator access.

after created the new admin user you need to create access id and secrete key. for that you have to click on new user and go to security credential tab.

in that tab you can find Access key option and create access key for cli.

Note : Make sure do not share the id and key with anyone and store it safely.

AWS IAM User Setup

First of all you have to install aws cli. you can find the install documentation here.

After installation done open the termminal and type aws configure and please add access id and secrete key and region on it.

Terraform Setup Test

create a new folder and type code . to open VS Code.

Note : You have to install VS Code before run code .

Install Terraform extention

create new file call main.tf

provider "aws" {
    region = "your region"
}

resource "aws_vpc" "terraform" {
    cidr_block = "10.0.0.0/16"
}

after run these commands,

terraform init

The “terraform init” command is used to initialize a Terraform working directory

terraform plan

The “terraform plan” command is used to preview the changes that Terraform will make to the infrastructure, providing insights into what resources will be created, modified, or deleted without actually applying those changes.

terraform apply

The “terraform apply” command is used to apply the changes defined in the Terraform configuration and provision or modify the infrastructure resources accordingly.

Now go to the AWS Console and type “VPC” on search bar. In there you can find terraform vpc that we created.

Leave a Comment

Your email address will not be published. Required fields are marked *