Terraform for AWS Infrastructure Management

Learn Terraform for AWS: Automate, scale, and optimize your cloud infrastructure.

Terraform for AWS Infrastructure Management
Page content

In the dynamic world of cloud computing, managing your Amazon Web Services (AWS) infrastructure manually is like navigating uncharted waters. The answer? Harness the power of Infrastructure as Code (IAC) using Terraform—a versatile tool tailor-made for AWS. In this blog, we’ll embark on a journey to mastering Terraform specifically for AWS environments.

Terraform, crafted by HashiCorp, is an open-source IAC tool that offers a declarative and efficient way to provision and manage infrastructure. While Terraform is cloud-agnostic, it seamlessly integrates with AWS, making it the go-to choice for AWS infrastructure orchestration.

Getting Your Hands Dirty

Installing Terraform

Start your journey by installing Terraform on your local machine. In my personal opinion, one of the most effective ways to establish a Terraform environment is by using tfenv.

For ubuntu:

git clone --depth=1 https://github.com/tfutils/tfenv.git ~/.tfenv
mkdir -p ~/.local/bin/
. ~/.profile
ln -s ~/.tfenv/bin/* ~/.local/bin
which tfenv

For mac:

brew install tfenv

Using tfenv

# To list locally installed versions
tfenv list 
# To list all available versions
tfenv list-remote
# To install a specific version
tfenv install <version>
# Make the version as default terraform version
tfenv use <version>

Setting up awscli

AWS interaction requires setting up your credentials. Easily configure your AWS access key and secret key either through the AWS CLI or by creating a dedicated IAM user via the AWS Management Console.

Note: Here i am assuming you already have an AWS account and you have created a IAM user which have access to AWS S3, AWS DynamoDB, etc.

To install/configure AWS cli, open your linux terminal follow below steps:

sudo apt update
sudo apt install python3 python3-pip -y
sudo pip3 install awscli
aws configure

Now copy paste the details promted. You will need AWS Access Key ID, AWS Secret Access Key, Default region name, Default output format.

To test our awscli, now we will try create a S3 bucket(which we will later use for other purpose i.e. keeping terraform remote state):

aws s3api create-bucket --bucket my-terraform-bucket --region us-east-1

If you can create a bucket successfully with the above command, it confirms that our awscli configuration is correct, and we can confidently proceed.

Terraform Basic Commands

  • This command prepares your workspace and downloads essential provider plugins, including AWS.
    terraform init
    
  • This command visualize the changes Terraform will make to your AWS infrastructure. This step is a crucial pre-flight check, providing insight into the impact of your configuration.
    terraform plan
    
  • To create or modify resources, use terraform apply. Terraform always seeks your confirmation before proceeding with any changes.
    terraform apply
    

State Management

Terraform maintains a state file that tracks the current status of your infrastructure. Safeguard this file securely and consider leveraging remote state storage options like AWS S3 for enhanced collaboration and reliability.

AWS-Centric Best Practices

  • Use Terraform Modules
  • Always Use Version Control
  • CI/CD Integration

Let’s dive right back in and get our hands dirty

Let’s clone a example git repo.

git clone https://github.com/devopsexplained/terraforming-aws.git 

The repo contains backend.tf, lambda.tf, provider.tf and lambda.zip. Now we will run below commands:

terraform init
terraform plan
terraform apply

After this we will observe state file in s3 and check if our lambda function is created or not.

terraform init

terraform_init

terraform plan

terraform_plan_1 terraform_plan_2 terraform_plan_3

terraform apply

terraform_apply

Now let’s see if our bucket have state file and AWS Lambda function is created or not.

aws_cli_list_s3_bucket_and_aws_lambda

Happy Terraforming!