Day 64 - Terraform with AWS

ANSAR SHAIKANSAR SHAIK
4 min read

As we venture further into our DevOps journey, we delve deeper into the world of infrastructure as code (IaC) with Terraform. Today, we explore Terraform's integration with Amazon Web Services (AWS) and its powerful capabilities in managing cloud resources efficiently.

1. What happens when you run Terraform apply after removing an EC2 instance from the state file?

When you remove an EC2 instance from the Terraform state file and then run terraform apply, Terraform detects the disparity between the desired state (defined in your Terraform configuration) and the actual state (tracked in the state file). It recognizes that the EC2 instance no longer exists in the state file but is present in the AWS infrastructure. As a result, Terraform will attempt to reconcile this by destroying the EC2 instance in AWS to align with the desired state defined in your configuration.

2. What are some built-in provisioners in Terraform?

Terraform offers several built-in provisioners to execute actions on the resources it manages. Some of the commonly used provisioners include:

  • local-exec: Executes a command locally on the machine running Terraform.

  • remote-exec: Executes commands on a remote resource after it's created.

  • file: Uploads or downloads files to or from a resource.

  • template: Renders configuration templates to create dynamic configurations.

3. What does the salt-masterless provisioner do in Terraform?

The salt-masterless provisioner enables Terraform to configure resources using SaltStack without requiring a Salt master. It allows you to provision resources and apply Salt states directly from the Terraform configuration, simplifying the management of infrastructure using SaltStack.

4. What does the remote-exec provisioner do in Terraform?

The remote-exec provisioner executes commands on a remote resource, typically an EC2 instance, after it's created. This provisioner is useful for tasks such as installing software, configuring settings, or performing any post-deployment setup required on the instance.

5. What does the puppet provisioner do in Terraform?

The puppet provisioner integrates with Puppet, a configuration management tool, to apply Puppet manifests to resources managed by Terraform. It allows you to automate the configuration of resources using Puppet, ensuring consistency and reliability across your infrastructure.

Task-01: Provisioning an AWS EC2 instance using Terraform

Prerequisites

AWS CLI installed

The AWS Command Line Interface (AWS CLI) is a unified tool to manage your AWS services. With just one tool to download and configure, you can control multiple AWS services from the command line and automate them through scripts.

AWS IAM user

IAM (Identity Access Management) AWS Identity and Access Management (IAM) is a web service that helps you securely control access to AWS resources. You use IAM to control who is authenticated (signed in) and authorized (has permissions) to use resources.

In order to connect your AWS account and Terraform, you need the access keys and secret access keys exported to your machine.

export AWS_ACCESS_KEY_ID=<access key>
export AWS_SECRET_ACCESS_KEY=<secret access key>

Here's a step-by-step process to provision an AWS EC2 instance using Terraform:

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.16"
    }
  }
  required_version = ">= 1.2.0"
}

provider "aws" {
  region = "us-west-2"  # Specify your desired AWS region
}

resource "aws_instance" "aws_ec2_test" {
  count         = 4
  ami           = "ami-08c40ec9ead489470"  # Specify your desired AMI ID
  instance_type = "t2.micro"                # Specify your desired instance type

  tags = {
    Name = "TerraformTestServerInstance"
  }
}

Initialize terraform provider plugins and modules

Formatting the terraform code

Validating terraform configuration file

planning infrastructure

executing the plan provided in configuration file

You can see instances are created successfully

You can destroy your infrastructure using one command

Instances are terminating

This Terraform configuration creates four EC2 instances in the specified AWS region using the provided AMI and instance type. The instances are tagged with the name "TerraformTestServerInstance" for easy identification.

By following this process, you can efficiently provision AWS resources using Terraform, making infrastructure management more scalable, reproducible, and automated.

As we continue to explore Terraform's capabilities, we empower ourselves to build robust and scalable infrastructure environments seamlessly integrated with AWS.

Stay tuned for more insights on our DevOps journey!

Day 64 of 90DaysOfDevOps

0
Subscribe to my newsletter

Read articles from ANSAR SHAIK directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

ANSAR SHAIK
ANSAR SHAIK

AWS DevOps Engineer