Backend Helpers | Automation and Software Development for Cloud Applicationses

Introduction to HashiCorp Terraform

Terraform is a framework for managing infrastructure as code. It includes different providers such as Amazon Web Services , Google Cloud , Microsoft Azure , among others. In this post we will cover a brief introduction to terraform by creating and destroying an AWS S3 bucket.

Installation

You will need to download the Right version for your system. Keep in mind that you will download a zipped file. You need to unzipped and place it in a directory included in your $PATH variable. You can verify the installation with the following command:

terraform --help

Bucket Creation

In the following steps we are going to create a terraform file and we will define our first AWS resource by using Amazon Web Services (AWS) provider

Step 1: Setup your credentials

You can configure your AWS credentials by setting up the environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. In this link you will find additional information about how to setup these two variables.

Step 2: Create your first terraform resource

Create a file named example.tf . This file will include the definition for a S3 bucket:


              provider "aws" {}

              resource "aws_s3_bucket" "b" {
                bucket = "my-unique-name-bucket-maigfrga.ntweb.co"
                acl    = "private"
                region = "eu-west-1"

                tags = {
                  Name        = "MyBucket"
                  Environment = "Development"
                }
              }
            

Step 3: Initialize Your Working Directory

The command terraform init initializes your working directory:

Step 4: Create a S3 Bucket

Run terraform apply in order to create a S3 bucket:

Step 5: Destroy a Resource

The command terraform destroy will destroy the S3 you just create previously: