top of page
  • Writer's pictureSuraj Dhakre

Mastering Terraform for AWS Infrastructure Management

Updated: Oct 16, 2023

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 for aws

Unveiling Terraform for AWS

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.



Navigating 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

Harness Terraform Modules

Terraform modules enable you to encapsulate and reuse configurations. Leverage modules to establish standardized AWS infrastructure components for your projects.


Always Use Version Control

Keep your Terraform configurations under version control, preferably in a Git repository. This practice facilitates collaboration, code reviews, and historical tracking.


CI/CD Integration

Integrate Terraform into your AWS-centric continuous integration and continuous deployment (CI/CD) pipeline for automated infrastructure updates.



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

terraform plan

terraform plan

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


Conclusion

Becoming a Terraform virtuoso in the realm of AWS empowers you to navigate your cloud infrastructure with precision, foster seamless collaboration within your team, and ensure infrastructure uniformity. Begin by installing Terraform, crafting your configurations, and gradually embracing AWS-centric best practices. With Terraform's prowess and AWS's flexibility, you're poised to master the art of Infrastructure as Code, tailored for the AWS universe. Happy Terraforming!



Comments


bottom of page