Terraform: Enabling developer to create and manage deployment through code

In this blog post, We will walk you through Terraform which is a tool for building, changing, and versioning infrastructure safely and efficiently.

Terraform enables the developers to properly manage the infrastructure through code.

The set of files used to describe infrastructure in Terraform is simply known as a Terraform configuration. These files have extension .tf.

Configuration files describe to Terraform the components needed to run a single application or your entire datacenter. Terraform generates an execution plan describing what it will do to reach the desired state, and then executes it to build the described infrastructure. As the configuration changes, Terraform is able to determine what changed and create incremental execution plans which can be applied.

Here, we will show you how to build architecture on AWS, but Terraform can also manage other providers including Azure, DataDog, Bitbucket, DNS, Google Cloud and many more.

provider "aws" {
  access_key = "${var.access_key}"
  secret_key = "${var.secret_key}"
  region     = "${var.region}"

The provider block is used to configure the named provider, in our case “aws”. A provider is responsible for creating and managing resources.

resource "aws_dynamodb_table" "my-demo-table" {
 name = "demo-table"
 hash_key = "id"
 range_key = "shopId"
 read_capacity = 10
 write_capacity = 10

 attribute {
   name = "id"
   type = "S"

 attribute {
   name = "shopId"
   type = "S"

The resource block defines a resource that exists within the infrastructure. A resource might be a physical component such as an EC2 instance, or it can be a logical resource such as a Heroku application. In this example, we have created a table in dynamoDB named as my-demo-table.

Now, our next step is to run this code to create this infrastructure on AWS. To run terraform configuration files, please follow below steps –

Step 1: terraform init

This command initializes the working directory containing the terraform configuration files. During init, it finds the backend configuration (if provided any) in the root directory, and initializes the chosen backend, and maintains a state file (.tfstate) in that backend.

Note: If no backend configuration is provided, by default terraform will create one in your local system. 

Step 2: terraform plan (optional)

This command explains its execution plan, i.e. what all will be created, modified and destroyed if you do run apply for this terraform scheme.

Step 3: terraform apply

This command first explains its execution plan, (basically it does terraform plan internally) and asks if that plan is ok for you.

If you type “yes”, it will execute that plan.

Now, the desired structure will be built for you in AWS. 

If you will execute the above configuration files, a dynamoDb instance named as my-demo-table will be created for you in AWS in your provided region.

Hope you liked the blog. Please feel free to comment down for doubts, we will be happy to help you.

Happy Blogging !!



This entry was posted in Amazon, Architecture, AWS, Cloud, Devops, knoldus and tagged , . Bookmark the permalink.

One Response to Terraform: Enabling developer to create and manage deployment through code

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s