Building Infrastructure as Code with Terraform

Aman AwasthiAman Awasthi
4 min read

In the fast-paced world of DevOps, automating infrastructure provisioning and management is crucial for efficient and reliable software development. Terraform empowers teams to define, deploy, and manage infrastructure in a declarative manner. In this blog post, we'll walk through a comprehensive example of creating an AWS infrastructure using Terraform, focusing on best practices and variable usage.

Introduction to the Infrastructure

Imagine you're tasked with setting up an AWS infrastructure for a development environment. You want to create an EC2 instance, manage two S3 buckets (one for general usage and another for Terraform state), and set up a DynamoDB table to track the state-file locking activity logs. To keep the configuration modular and maintainable, you decide to use variables and organize your resources across different .tf files.

Terraform State Management: Secure and Collaborative Practices

When using Terraform to build infrastructure, the state is captured in the terraform.tfstate file is crucial for tracking resources and their configurations. However, sharing this file on platforms like GitHub poses risks due to security and collaboration issues.

To address this, Terraform offers a solution: managing state using Amazon S3 and DynamoDB. By storing the state in a secure S3 bucket and using DynamoDB for locking sensitive data exposure and conflicts are minimized. This approach enhances security, resolves collaboration conflicts, and enables effective versioning. Configure the backend block in your Terraform configuration to implement this state management strategy.

Steps -

Step 1: Preparing AWS CLI and Privileges - Ensure AWS CLI is configured with the necessary privileges for seamless interaction with AWS resources.

Step 2: Defining Provider Details - In the terraform.tf file, create a terraform block specifying the required providers. Example: AWS provider details.

Step 3: Configuring Provider - Generate a provider.tf file. Utilize variables like my_region from variable.tf to dynamically set the AWS region.

Step 4: Setting Up Variables Invariables.tf, define variables to be utilized across all .tf files, streamlining customization.

Step 5: Creating EC2 Instance and S3 Bucket - Create a separate .tf file to establish an EC2 instance and an S3 bucket. The initial setup involves the instance and a single S3 bucket.

Step 6: Initializing the Configuration - Initialize the configuration using the command terraform init.

Step 7: Planning and Applying Infrastructure - Execute terraform plan followed by terraform apply constructing the infrastructure as designed.

Step 8: Success in Infrastructure Creation - Upon successful execution, the infrastructure is ready. To display the instance's public IP, create an output.tf file.

Initiating State Management - For efficient state management, initiate a new S3 bucket for storing the terraform.tfstate file, a DynamoDB table for lock tracking, and a backend configuration.

Step 9: Defining Variables - Establish variables for the DynamoDB table and state bucket within the variables.tf file.

Step 10: Creating DynamoDB Table - Generate a new .tf file for setting up the DynamoDB table, ensuring proper log tracking.

Step 11: Building State File Storage Bucket - Create resources to set up the S3 bucket designated for storing state files.

Preparing for Backend Configuration - Before creating the backend configuration, ensure the table and bucket exist within the infrastructure.

Step 12: Initializing and Applying Changes - Run terraform init and terraform apply to reflect the creation of the state file storage bucket. (Attaching two screenshots as I missed creating the bucket)

Step 13: Establishing Backend Configuration - Incorporate the backend configuration into the terraform block.

Note - The variable values can't be directly applied here.

Step 14: Finalizing Initialization - Re-run terraform init to complete the setup. A success message indicates the successful configuration of the "s3" backend.

Here, we will get a message - Successfully configured the backend "s3"! Terraform will automatically use this backend unless the backend configuration changes.

Now, the changes made in the state will be directly saved in the designated S3 bucket.

Step 15: Validation through AWS Console Validate changes by logging into the AWS console. Confirm instances, buckets, and DynamoDB tables to ensure accurate implementation.

Make sure to destroy the infrastructure after your learning.

Thank you :)

3
Subscribe to my newsletter

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

Written by

Aman Awasthi
Aman Awasthi