Terraform with AWS

How to use Terraform with AWS?

Terraform is an open-source tool from HashiCorp that lets you easily manage, provision, and configure the infrastructure. 

The management process and infrastructure building is typically a long and time-consuming task involving multiple IT teams. Using the Terraform platform streamlines this process, and it is also possible to adjust the infrastructure that has already been built.

You can use Terraform to manage infrastructure across multiple clouds. Terraform’s configuration language is human-readable, so it is easy to use. It’s called Hashicorp Configuration Language, or HCL.

By using APIs, or Application Programming Interfaces, the cloud provider can connect to all cloud platforms. AWS is an Infrastructure provider platform. Using Terraform and AWS, we can manage infrastructure quickly and effectively.

How to use Terraform with AWS?

How to use Terraform with AWS?

Now let us see how to build infrastructure using Terraform with AWS.

Creating a Terraform infrastructure requires a few Terraform and AWS prerequisites. First, you will need the Terraform CLI, the AWS CLI, an AWS account, and AWS credentials.

Here are the steps for creating an AWS account:

  • To get started, you will first need to create an account with the IAM section of AWS.
  • Select the programmatic access and then click next, followed by the admin group.

How to use Terraform with AWS?

  • Upon creating the user, you will receive a password ID and a secret password that will be required later to configure the system. Both keys are confidential and should be kept safe.

How to use Terraform with AWS?

How to use Terraform with AWS?

EC2 instance using Terraform.

We have created the IAM user and have Terraform already installed. Now it is time to build and destroy the EC2 instance using Terraform. Consider the following file,

Provider “aws” {

     Access_key = “ACCESS_KEY_HERE”

Secret_key= “SECRET_KEY_HERE”

REGION    

    = “us-east-1”

}

Resource “aws_instance” “forexample” {

    Ami = “ami-13be557e”

    Instance_type = “t2.micro”

}

In the above example, AWS is the provider that Terraform is using. The provider aids Terraform in creating and managing resources successfully. We have already safely located the access which we can provide here. 

AWS_instance id represents the identifier of an EC2 instance. It tells us to start supplying it with resources. The name identifier follows, which can be anything you prefer. Here is the name identifier we are using.

We then specify the AMI type on AWS and the instant_type, here we are using t2.micro., which can be found in the AWS free tier package. We are thus using it here.

Finally, we have to run the terraform init, where you created the instance.tf file. Here, we have specified the AWS provider plugin in that file. Then we must download and initialize the provider plugin that is the AWS.

As soon as the Terraform process is completed, the Terraform plan can be executed. It will enable you to view what Terraform will do before we apply it. We use the Terraform apply to create the instance. 

If you navigate to the EC2 section of the AWS console, you should see the t2.micro instance created. Terraform-based infrastructure has been created successfully using AWS and Terraform.

You can use the terraform destroy to destroy the infrastructure created, and it will also destroy all the resources created in the Terraform infrastructure.

How to use Terraform with AWS?

Final Words!

Terraform makes infrastructure management effortless and hassle-free. Now you will have a clear idea about how to create infrastructure in Terraform with AWS.

Leave a Comment

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

Scroll to Top