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 File
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
πβπ¨πβπ¨ YouTube Tutorial π½
βοΈβοΈ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.
Author - Dheeraj Choudhary
RELATED ARTICLES
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.
Automating AWS Infrastructure with Terraform Functions
IntroductionManaging cloud infrastructure can be complex and time-consuming. Terraform, an open-source Infrastructure as Code (IaC) tool, si ...