Open In App

How to use the AWS Teraform Provider ?

Last Updated : 26 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Using Terraform’s simple and natural linguistic structure, clients can define infrastructure resources, their conditions, and setups in a single Terraform configuration file. These files, written in HashiCorp Setup Language (HCL), portray the ideal condition of the infrastructure, permitting Terraform to productively plan and execute changes.

The AWS Terraform provider consistently integrates with AWS APIs, empowering Terraform to create make, update, and delete resources AWS on the basis of the defined configuration. This gives clients a predictable and reproducible method for overseeing the framework, working with infrastructure automation, variant control, and coordinated effort across groups.

By utilizing the AWS Terraform supplier, clients can outfit the versatility, adaptability, and unwavering quality of AWS cloud administrations while profiting from the straightforwardness and force of Terraform’s foundation-as-code approach.

Here, in this article I will direct you to the most proficient method to utilize AWS terraform supplier and by utilizing AWS supplier we will make AWS assets like vpc,ec2_instance, and so on we desire.

Understanding of primary technologies

What is terraform?

Terraform is an infrastructure as code (IaC) tool. Though It is an infrastructure-as-a-code software tool used primarily by DevOps teams to automate various infrastructure tasks, created by Hashicorp. Users define and provide data centers, and infrastructure using a declarative configuration language known as Hashicorp Configuration Language. Terraform abstracts away the complexity of manually configuring AWS resources, enabling users to define infrastructure configurations declaratively in Terraform configuration files. With the AWS provider, users can provision and manage a wide range of AWS resources, including compute instances, storage, networking components, databases, and more.

Terraform AWS Provider:

The Terraform AWS provider is a plugin that enables Terraform to interact with AWS APIs, allowing users to define and manage AWS resources such as EC2 instances, S3 buckets, VPCs, RDS databases, and IAM roles through Terraform configuration files.

How to use the AWS terraform provider

Here, I am going to implement How to use the AWS terraform provider: by launching the AWS ec2 instance.

To use the AWS Terraform provider, you’ll need to follow several steps to set up your Terraform environment, define your infrastructure in Terraform configuration files, and apply those configurations to provision and manage AWS resources. Let’s walk through these steps in detail

Step 1: Setting Up the AWS account

  • Go to AWS Management Console
  • Login by using your credentials.
  • Now you need to generate an access key to authenticate Terraform with your AWS account
  • In the AWS management console in the home screen search for IAM ( Identity and Access Management ) service. Choose Users and click on Add User.
  • Give a username and select administration access as the access type. Attach necessary permissions to the user.
  • Review the user details and create the user. Now you will see the access key ID and secret access key. Save this information securely as it will be required when configure Terraform.

Step 2: Launch EC2 instance And Install Terraform

Launch An Instance With Configuration:

  • AMI- amazon Linux 2
  • instance type- t2.micro
  • Security group- allow SSH(22),HTTP(80),HTTPS(443) traffic from anywhere
  • Configure storage – 8gb with root volume type gp2
  • Connect this instance with any CLI terminal by using SSH
ssh -i  "pemfile" ec2-user@<instance-public-ip address>compute-1.amazonaws.com

instance connection

To use the AWS Terraform provider, you’ll need to follow several steps to set up your Terraform environment, define your infrastructure in Terraform configuration files, and apply those configurations to provision and manage AWS resources. Let’s walk through these steps in detail:

Step-3: Installing terraform

  • Now install terraform by using following commands

Make sure we have to install terraform in our ec2 instance

  • For this we need to download terraform hashicorp related packages and repo.
  • I take these keys and repo from terraform official page.
  • https://developer.hashicorp.com/terraform/install#linux
  • to install terraform in our OS follow the below commands.
sudo yum install -y yum-utils shadow-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
sudo yum -y install terraform

terraform installing and setup

Step 4: Set Up AWS Credentials

  • Configure your AWS credentials either through environment variables, the AWS CLI, or a shared credentials file. This allows Terraform to authenticate with your AWS account.
aws configure

aws configuration

Terraform Scripts

In Terraform, the terraform block is utilized to arrange the settings related to the Terraform execution environment itself. This block allows you to define various options and configurations that affect how Terraform behaves when executing your infrastructure code.

Step 5: Make the directory for to configure terraform scripts

  • Make a new directory for your Terraform configuration files and navigate into it.
  • Inside this directory, create a terraform files with .tf extension to write the terraform scripts.
mkdir terraform
cd terraform

mkdir

Provider Block

In Terraform, the “provider” block is a fundamental construct used to define and configure the provider responsible for managing the resources in a specific cloud or infrastructure environment. Providers are plugins in Terraform that interface with APIs of various services or platforms to create, read, update, and delete resources.

Step 6: terraform script for aws provider

inside this created terraform directory , create a new file to write the terraform scripts for Route table and define the Terraform aws configuration for your route table.

vi provider.tf

#provider

provider “aws” {

region = “us-east-1” # Specify your desired AWS region

}

provider block

  • so,we mentioned the provider block in provider.tf file

lets’ look at how to use the AWS terraform provider by launching the aws_ec2 instance.

Step 6: terraform script for aws ec2_instance

  • In same terraform directory create a file ec2_instance.tf,in this file configure the terraform script to launch an aws ec2_instance
sudo vi ec2_instance.tf

provider “aws” {

region = “eu-north-1”

}

resource “aws_instance” “example” {

ami = “ami-0f0ec0d37d04440e3”

instance_type = “t3.micro”

key_name = “11”

}

terraform script for ec2_instance

Step 8: Execute terraform files i.e. provider.tf,ec2_instance.tf

  • we should initialize the terraform in backend.
  • firstly, we make ensure the terraform files shoube in declarative manner.
  • secondly, we have to check the validation of terraform code doesn’t have any syntax and resources errors.
  • then plan these terraform code and terraform files i mean check the cloud resources we are going to create.
  • finally,apply the the terraform code. this is the most important step we are going to execute because this is the step terraform will create the cloud resources we want
terraform init

Terraform init

  • execute the below commands to format,validate and plan the terraform scripts
terraform validate
terraform plan

Terraform validate

  • now,execute these below command to apply terraform scripts with auto approve.
  • When we execute this command then automatically our infrastructure will build.
terraform apply --auto-approve

terraform apply --auto-approveresources are created and added

  • see the terraform apply is complete Resources: 1 added,0 changed,0 destroyed

Resource Block

In Terraform, the “resources” block is not a specific construct like the provider or terraform blocks. Instead, it’s a common terminology used to refer to the section of a Terraform configuration where you define the infrastructure resources what you want to desire.

ec2_instance created

AWS Terraform Provider – FAQ’s

can we use terraform to build and manage multiple AWS accounts?

yes, we can use terraform supports to manage multiple AWS accounts to create,build,update,modify and delete AWS resources.users can define multiple provider configuration in provider block.

suggest any best practices to implement using AWS terraform?

Suggesting best practices to implement to use AWS terraform maintain the provider block with valid regions,and specify the cloud provider and main security for aws configuration credentials.include organizing Terraform configurations using indentation,modules and workspaces, following infrastructure as code principles, using version control (e.g., Git) for configuration files.

Which AWS resources we can manage with terraform?

AWS terraform provides wide range of aws resources.the resources we can manage are AWS ec2 instance,VPC (virtual private cloud),RDS(relational databases),security groups,route tables,internet gateways,virtual private networks,route53, IAM users and so on…..

What are ways or methods to authenticate Terraform with AWS?

There are different methods to authenticate terraform with AWS including aws access keys,aws credentials file and attach IAM roles.Users typically AWS configure credentials using environment variables (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY), AWS CLI configuration, or IAM roles assigned to EC2 instances.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads