In this article, we will get to know about the Terraform, the biggest taboo nowadays. We will understand how it helps developers to code, to manage, deploy but also saves DevOps experts a huge amount of time. The terraform script help to automate the application to manage the infra with AWS.

Terraform is an infrastructure as code tool that lets you define both cloud and on-prem resources in human-readable config files that you can version, reuse, and share. You can then use a consistent workflow to provision and manage all of your framework in your entire lifecycle. 

Terraform can manage low-level components like compute, storage, and networking resources, as well as high-level components like DNS entries and SaaS features.

Infrastructure as code is an IT practice that manages an application’s underlying IT infrastructure through programming. This approach to resource allocation allows developers to logically manage, monitor, and provision resources – as opposed to requiring that an operations team manually configure each required resource.

Let’s see how we use Terraform with AWS:


1. Stages of Terraform Workflow

terraform stages
Write:

You define resources, which may be across multiple cloud providers and services. For example, you might create a config to deploy an application on virtual machines in a Virtual Private Cloud (VPC) network with security groups and a load balancer.

Plan:

Terraform creates an execution plan describing the infrastructure. It will create, update, or destroy based on the existing infrastructure.

Apply:

On approval, Terraform performs the proposed operations in the correct order. For example, if you update the properties of a VPC and change the number of virtual machines in that VPC, Terraform will recreate the VPC before scaling the virtual machines.

2. Terraform for AWS

An advantage of using infrastructure as a code tool is that your config can be treated as your memorandum. Breaking down your infrastructure into components makes it easier to read and update your infrastructure as you grow. This, in turn, helps make knowledge sharing and bringing new team members up to speed easier.

Terraform supports powerful constructs called modules that allow you to reuse infrastructure code. This enables you to provide infrastructure as building blocks that other teams can leverage. You can use Terraform script for creating AWS EC2 instances that use only the instance types your company has standardized on.

A service team can then include your module and be in compliance. This approach creates enablement and promotes self-service.

Let’s understand a stepwise guide to set up terraform:-

Downloading Terraform

After downloading Terraform, unzip the package. Terraform runs as a single binary named terraform. Any other files in the package can be safely removed and Terraform will still function.

Finally, make sure that the terraform binary is available on your PATH. This process will differ depending on your operating system.

Verify Installation

To verify with this below command

If the result looks like the below image, then you successfully installed Terraform.

verify terraform
Configure AWS

(a) AWS CLI Installation: Install AWS CLI according to your OS flavor, you can click here to help you in the installation

(b) AWS CLI Configuration: After installation, you would be able to run the AWS command.

To configure your system you have to make sure to have the AWS account wherein you will fetch the access and secret key.

After the command AWS configure it will ask for an access key, secret key, region, and format.

aws configure
Write Configuration

The set of files used to describe infrastructure in Terraform is known as a Terraform configuration. You will write your first configuration to define a single AWS EC2 instance.

  • Create a directory for you that will help you manage things better.
  • Change into the directory
  • Create the main file of terraform main.tf
  • Edit the file in the text editor, by pasting the below text in your file. This is basic terraform script for aws ec2 instance.

Let us describe each block:-

  • Terraform block{}:- The terraform {} block contains Terraform settings, including the required providers Terraform will use to provision your infrastructure. For each provider, the source attribute defines an optional hostname, a namespace, and the provider type. 
  • Providers block:- The provider block configures the specified provider, in this case aws. A provider is a plugin that Terraform uses to create and manage your resources.
  • Resources:- Use resource blocks to define components of your infrastructure. A resource might be a physical or virtual component such as an EC2 instance, or it can be a logical resource such as a Heroku application.
Initialize the directory

When you create a new configuration — or check out an existing configuration from version control – you need to initialize the directory with terraform init.
Initializing a configuration directory downloads and installs the providers defined in the configuration, which in this case is the AWS provider.

terraform init
Format and validate the configuration

We recommend using consistent formatting in all your configuration files. The terraform fmt command automatically updates configurations in the current directory for readability and consistency.

terraform fmt

You can also make sure your configuration is syntactically valid and internally consistent by using the terraform validate command.

terraform validate
Create Infrastructure

Apply the configuration now with the terraform apply command. Terraform will print output; this can also be said as how to run terraform script with AWS provider. Before it applies any changes, Terraform prints out the execution plan which describes the actions Terraform will take in order to change your infrastructure to match the configuration.

terraform apply

It will show all the modified changes and resources being added or deleted.

After the successful creation of the resources, it will store the backup in the recovery files created in the same directory (terraform.tfstate.backup , terraform.tfstate).

terraform after apply

These files will be useful whenever the next terraform apply iteration runs. It will help terraform remember the last state of action taken.


So, this is all about the terraform to run the script for automation with AWS.

I hope you enjoyed the article and if you found this useful, then please share it with your friends and colleagues. If this post helps you, then spread this so that other people can also benefit.

If you have any queries please feel free to post them in the comments section or anything that you want to ask through mail contact.

Thank you😉

Also read,