terraformfuntion_blog

Automating AWS Infrastructure with Terraform Functions

Introduction

Managing cloud infrastructure can be complex and time-consuming. Terraform, an open-source Infrastructure as Code (IaC) tool, simplifies this process by allowing you to define, provision, and manage your infrastructure in a declarative way. In this blog, we will explore how to use Terraform functions to automate the creation of an AWS VPC, public subnets, and EC2 instances. By leveraging Terraform functions, we can dynamically generate and manage resources efficiently.

Prerequisites

Before we dive into the code, ensure you have the following prerequisites in place:

  1. AWS Account: You need an AWS account to create and manage resources.

  2. Terraform Installed: Install Terraform on your local machine. You can download it from Terraform’s official site.

  3. AWS CLI Configured: Configure the AWS CLI with your AWS credentials to allow Terraform to communicate with AWS.

Diagrammatic Representation

Understanding the Terraform Code

Let’s break down the provided Terraform code to understand how we automate AWS infrastructure.

1. Data Source for AWS Account ID

data "aws_caller_identity" "current" {}

This data source fetches the current AWS account ID. It’s useful for tagging and managing resources based on the account details.

2. Data Source for Latest Amazon Linux 2 AMI

data "aws_ami" "latest_amazon_linux" {
  most_recent = true
  filter {
    name   = "name"
    values = ["amzn2-ami-hvm-*-x86_64-gp2"]
  }
  owners = ["amazon"]
}

This data source retrieves the latest Amazon Linux 2 AMI, ensuring that we use the most up-to-date image for our EC2 instances.

3. Data Source for Available AWS Availability Zones

data "aws_availability_zones" "available" {
  state = "available"
}

This data source fetches a list of available AWS availability zones, which helps in distributing resources across multiple zones for high availability.

4. Creating a VPC

resource "aws_vpc" "customvpc" {
  cidr_block       = "10.0.0.0/16"
  instance_tenancy = "default"

  tags = {
    Name = "main"
  }
}

This resource block creates a Virtual Private Cloud (VPC) with a CIDR block ofĀ 10.0.0.0/16. The VPC provides isolated networking in the AWS cloud.

5. Creating Public Subnets

resource "aws_subnet" "customsub" {
  count  = length(data.aws_availability_zones.available.names)
  vpc_id = aws_vpc.customvpc.id

  cidr_block        = "10.0.${count.index}.0/24"
  availability_zone = element(data.aws_availability_zones.available.names, count.index)

  map_public_ip_on_launch = true

  tags = {
    Name = "public-${element(data.aws_availability_zones.available.names, count.index)}"
    Type = "Public"
  }
}

This resource block creates public subnets in each availability zone. By usingĀ countĀ andĀ elementĀ functions, we dynamically create subnets and assign them to different availability zones, ensuring high availability and fault tolerance.

6. Creating EC2 Instances

resource "aws_instance" "custom_instances" {
  count         = length(aws_subnet.customsub)
  ami           = data.aws_ami.latest_amazon_linux.id
  instance_type = "t2.micro"
  subnet_id     = element(aws_subnet.customsub[*].id, count.index)

  tags = {
    Name = "ServerNo-${count.index}"
    Env  = "Dev"
  }
}

This resource block creates EC2 instances in the public subnets. TheĀ countĀ andĀ elementĀ functions ensure that instances are evenly distributed across the subnets. Each instance is tagged for easy identification and management.

šŸ‘ā€šŸ—ØšŸ‘ā€šŸ—Ø YouTube Tutorial šŸ“½

Ā šŸ„šŸ„ Conclusion šŸ„šŸ„

By using Terraform functions, we can automate the creation and management of AWS resources efficiently. TheĀ count,Ā element, and data sources allow us to dynamically generate and distribute resources, ensuring scalability and high availability. This approach not only saves time but also reduces the risk of manual errors.

Terraform’s powerful capabilities make it an essential tool for modern cloud infrastructure management. By following this guide, you can start automating your AWS infrastructure and leverage the full potential of Terraform.

Happy automating!

šŸ“¢Ā Stay tuned for my next blog…..

So, did you find my content helpful? If you did or like my other content, feel free to buy me a coffee. Thanks.

Dheeraj_Pic1 (2)

Author - Dheeraj Choudhary

I am an IT Professional with 11+ years of experience specializing in DevOps & Build and Release Engineering, Software configuration management in automating, build, deploy and release. I blog about AWS and DevOps on my YouTube channel, which focuses on content such as, AWS, DevOps, open source, AI-ML and AWS community activities.

RELATED ARTICLES

AWS Glue Presentation (1)

Automate S3 Data ETL Pipelines With AWS Glue Using Terraform

Discover how to automate your S3 data ETL pipelines using AWS Glue and Terraform in this step-by-step tutorial. Learn to efficiently manage and process your data, leveraging the power of AWS Glue for seamless data transformation. Follow along as we demonstrate how to set up Terraform scripts, configure AWS Glue, and automate data workflows.

Comments are closed.