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

Advertisement

Ad-1.jpg

Comments are closed.