part2 (1)

Deploy AWS VPC , Internet Gateway & Associate Using Terraform

Welcome back to the series of Deploying On AWS Cloud Using Terraform πŸ‘¨πŸ»β€πŸ’». In this entire series, we will focus on our core concepts of Terraform by launching important basic services from scratch which will take your infra-as-code journey from beginner to advanced. This series would start from beginner to advance with real-life Usecases and Youtube Tutorials.

If you are a beginner for Terraform and want to start your journey towards infra-as-code developer as part of your devops role buckle up πŸš΄β€β™‚οΈ and lets get started and understand core Terraform concepts by implementing it…🎬

πŸ”ŽBasic Terraform ConfigurationsπŸ”

As part of the basic configuration, we are going to set up 3 terraform files

1. Providers File:- Terraform relies on plugins called “providers” to interact with cloud providers, SaaS providers, and other APIs.

Providers are distributed separately from Terraform itself, and each provider has its own release cadence and version numbers.

The Terraform Registry is the main directory of publicly available Terraform providers, and hosts providers for most major infrastructure platforms. Each provider has its documentation, describing its resource types and their arguments.

We would be using AWS Provider for our terraform series. Make sure to refer Terraform AWS documentation for up-to-date information.

Provider documentation in the Registry is versioned; you can use the version menu in the header to change which version you’re viewing.

provider "aws" 
{ 
region = "var.AWS_REGION" 
shared_credentials_file = "--Your aws credentials path--" 
}

2.Β Β Variables File:- 2 Terraform variables lets us customize aspects of Terraform modules without altering the module’s source code. This allows us to share modules across different Terraform configurations, reusing same data at multiple places.

When you declare variables in the root terraform module of your configuration, you can set their values using CLI options and environment variables. When you declare them in child modules, the calling module should pass values in the module block.

Β 

variable "AWS_REGION" 
{ 
default = "us-east-1" 
}

3.Β Versions File:- Its always a best practice to maintain a version file where you specific version based on which your stack is testing and live on production.

Β 

terraform 
{ 
required_version = ">= 0.12" 
}

Configure Virtual Private Cloud

Now that we have configured basic files for providers, variables and versions let’s move ahead and start coding our vpc file. Create a new fileΒ vpc.tfΒ and add the below code to this file.

πŸ”³Β Resources

✦ AWS_VPC:- This resource is used to launch a private VPC in the configured AWS account.

πŸ”³Β Arguments

✦ cidr_blockΒ – This IPv4 CIDR block for the VPC is an optional argument. CIDR can be explicitly set or it can be derived from IPAM using ipv4_netmask_length.
✦ enable_dns_supportΒ – This is an optional argument that denotes a boolean flag to enable/disable DNS support in the VPC with default value as true.
✦ enable_dns_hostnamesΒ – This is an optional argument which denotes a boolean flag to enable/disable DNS hostnames in the VPC with default value as true.
✦ tags:- One of the most important property used in all resources. Always make sure to attach tags for all your resources.

Β 

resource "aws_vpc" "CustomVPC" {
  cidr_block           = "10.0.0.0/16"
  enable_dns_support   = true
  enable_dns_hostnames = true

  tags = {
    Name = "CustomVPC"
           }
}

Configure Internet Gateway & Associate Them

As mentioned above we have createdΒ vpc.tfΒ file which helps us to launch our private VPC. Now lets go ahead and create a new fileΒ igw.tfΒ where we will configure our internet gateway resource and associate that internet gateway to the vpc we have created above.

πŸ”³Β Resources

✦ aws_internet_gateway:- This resource is used to launch a private VPC in the configured AWS account.

πŸ”³Β Arguments

✦ vpc_idΒ – This vpc id argument refers the id value of the vpc which has been created so the internet gateway can get associated to it.
✦ tags:- One of the most important property used in all resources. Always make sure to attach tags for all your resources.

Β 

resource "aws_internet_gateway" "igw" { 
vpc_id = aws_vpc.CustomVPC.id
tags = { Name = "IGW" } 
}

Output values make information about your infrastructure available on the command line, and can expose information for other Terraform configurations to use. Output values are similar to return values in programming languages.

Β 

output "vpc_id" { 
value = aws_instance.CustomVPC.id 
description = "This is vpc id." 
} 
output "enable_dns_support" { 
value = aws_instance.CustomVPC.enable_dns_support 
description = "Check whether dns support is enabled for VPC." 
} 
output "enable_dns_hostnames" { 
value = aws_instance.CustomVPC.enable_dns_hostnames 
description = "Check whether dns hostname is enabled for VPC." 
} 
output "aws_internet_gateway_id" { 
value = aws_internet_gateway.igw.id 
description = "Internet gateway id." 
} 
output "igw_aws_account" { 
value = aws_internet_gateway.igw.owner_id 
description = "AWS Account id to which internet gateway is associated." 
}

πŸ”ŠTo view entire github code clickΒ here

1️⃣ The terraform fmt command is used to rewrite Terraform configuration files to a canonical format and styleπŸ‘¨β€πŸ’».

Β 

 terraform fmt

2️⃣ Initialize the working directory by running the command below. The initialization includes installing the plugins and providers necessary to work with resources.Β πŸ‘¨β€πŸ’»

Β 

 terraform init

3️⃣ Create an execution plan based on your Terraform configurations.Β πŸ‘¨β€πŸ’»

Β 

 terraform plan

4️⃣ Execute the execution plan that the terraform plan command proposed.Β πŸ‘¨β€πŸ’»

Β 

terraform apply --auto-approve

❗️❗️Important Documentation To Be Viewed❗️❗️

⛔️ Hashicorp Terraform
⛔️ AWS CLI
⛔️ Hashicorp Terraform Extension Guide
⛔️ Terraform Autocomplete Extension Guide
⛔️ AWS VPC
⛔️ AWS Internet Gateway

In this blog, I have covered the deployment of the below AWS Resources.
✦ Custom VPC.
✦ Internet gateway and associate it with our custom VPC.
I have used Terraform CLI command to deploy these templates. Stay with me for the next release.

πŸ“’Β 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.