VPC Build with Terraform
This is a simple run down of how a basic vpc can be created. I haven’t used any of the advanced concepts used in the creation process i.e. terraform workspaces or modules… will be explained later on.
If you go back to the 2 tier architecture I posted this basically is similar to that difference is I’ll be implementing it with the use of terraform.
Highly recommend for you to watch the video in my previous Terraform post before coming here though. Other than that lets get to it.
Here’s the link to the code in my repo https://github.com/Xevlyn/Terraform-Projects.git
Starting with the main.tf file
This is just the code snippet for the VPC we intend to create.
- “aws_vpc” — resource to be created which has been stated in this case is the vpc.
- “GrayyMainNet” — unique identifier used in the terraform code which if not stated, a name will be given to it.
- “cidr_block” — Ip address we choose to give for our network.
- “instance_tenancy” — A tenancy option is for instances launched into the VPC. Default , which ensures that EC2 instances launched in this VPC use the EC2 instance tenancy attribute specified when the EC2 instance is launched.
- tags — This is just the name attached to the VPC once created
Anything a var.<example> is a reference to the variables file where by when ever the the code built, it refers to the varibales.tf file for the details
The varibles.tf file contains all the details of the resources to be created as seen below in the snippets. For the VPC, Subnets, internet gateway, route table and NAT gateway
- Provider — Refers to the cloud provider being used which is aws in this case specifying the region in which it should be created in.
- Profile — define the settings for a particular provider in this case AWS and each profile typically corresponds to a set of authentication credentials for a specific environment, allowing Terraform to interact with the associated cloud provider.
Here are the other snippets
Now another means of securing the terraform code you write creating an S3 backend which serves as a remote backend for storing and retrieving Terraform state files.
State files to keep track of the resources it manages, their current state, and the relationships between them. Storing the state remotely allows for collaboration, consistency, and better management of infrastructure.
- The “bucket” parameter specifies the name of the S3 bucket where Terraform state files will be stored.
- The “key” parameter is the name of the state file within the S3 bucket. In this example, it’s set to “terraform-statefiles/vpc.tfstate.” which is the folder and file respectively.
Once everything has been put in place, run the terraform init, terraform plan and terraform apply in order to build up the infrastructure.
Here’s a run through video on how to do it. Until the next one
GrayyFoxx