My Devops Journey

Menu
  • AWS
  • Terraform
  • kubernetes
Menu

Creating a AWS VPC and subnet using terraform with simple example

Posted on December 16, 2019 by taragurung

Terraform is an open source infrastructure as code tool. You are here because you know what terraform is and what it can do for you. Here, the primary goal is to understand the basics of terraform and write a script that will support the theory and make things clearer.

Terraform installation guide on Mac os:

Before we begin, let’s make our system ready by installing the terraform.

  1. Open terminal (press command⌘ + space and type terminal)
  2. brew install terraform
  3. Type terraform and if you see some message popping out regarding terraform then the installation was successful.

Quick start example:

provider "aws" {
 profile = "testfm"
  region  = "us-east-2"
}

resource "aws_instance" "terraform_ec2" {
  ami       = "ami-0d5d9d301c853a04a"
  instance_type = "t2.micro"
}

The example above creates a EC2 instance of type t2 micro in us-east-2 region.

Some useful Terraform command before we begin

terraform init: to initialize the terraform

terraform plan: This is like dry run, will show the details what will terraform do and what are the settings it will apply.

terraform apply: The main command to start terraform 

terraform fmt: will auto format the file in proper standard formatting

terraform validate : will look for some errors in the file

terraform plan: this command will show in detail what it is really going to do. Ie what it will add what will not and many more

terraform destroy: destroy the infrastructure created by terraform apply

Trying to create AWS VPC and Subnet using the Terraform:

We all know VPC is the building block of everything. If someone starts to learn AWS they will obviously go through it and is one of the important things to must know and implement in AWS cloud. So why not start with it. We will also learn to attach Subnet to VPC.

Terraform basic template:

Before diving into writing the terraform script, lets see, how the basic terraform script format looks alike.

                                            Fig: Basic format of terraform script

As seen in the figure, we have two main parts in terraform: a part to set the cloud provider and a part to define the resources to provision.

Terraform script (main.tf):

provider "aws" {
 profile = "testfm"
  region  = "us-east-2"
}

resource "aws_vpc" "terraform_vpc" {
    cidr_block = "10.0.0.0/16"
    enable_dns_support = "true" #gives you an internal domain name
    enable_dns_hostnames = "true" #gives you an internal host name
    enable_classiclink = "false"
    instance_tenancy = "default"   
   
    tags = {
        Name = "test-vpc"
        Location = "Ohio"
    }
}

resource "aws_subnet" "subnet1" {
  vpc_id     = aws_vpc.terraform_vpc.id #value from another resource (resource.logicalname.param)
  cidr_block = "10.0.1.0/24"

  tags = {
    Name = "terraform_subnet1"
  }
}

Understanding the terraform script:

provider "aws" {
 profile = "testfm"
  region  = "us-east-2"
}

The terraform will do the provisioning on aws cloud provider and the region will be us-east-2. We must be familiar with all the AWS terminology to get used to with region and upcoming scripts as the terraform, here is meant to work on AWS cloud provider.

resource "aws_vpc" "terraform_vpc" {
    cidr_block = "10.0.0.0/16"
    enable_dns_support = "true" #gives you an internal domain name
    enable_dns_hostnames = "true" #gives you an internal host name
    enable_classiclink = "false"
    instance_tenancy = "default"   
   
    tags = {
        Name = "test-vpc"
        Location = "Ohio"
    }
}

Now, the second section is related to resources. The resources refer to any services provided by the cloud. As our goal was creating VPC and Subnet using terraform. In our case VPC and Subnet is the resource for the terraform.

The resource takes 2 parameters: 

  1. The first parameter is the resource name (eg: aws_ec2, aws_vpc, aws_subnet). Check terraform docs for more.
  2. The second parameter is the logical name given to the resource. This is used to give identification to the resources. If we have a multiple resources and need to refer any resource then we can use the logical name preceded by the resource name.

The above script will create a VPC with the properties defines inside the curly braces. Please, check the official terraform docs regarding the purpose of each key being used.

Output: it will create a VPC with defined keys and the tags. The Name parameter will also be used to name a resource.

resource "aws_subnet" "subnet1" {
  vpc_id = aws_vpc.terraform_vpc.id #GETvalue from another resource
  cidr_block = "10.0.1.0/24"

  tags = {
    Name = "terraform_subnet1"
  }
}

This sections is another resource associated with subnet creation. It creates a subnet and is associated with with VPC resource. The association is done by the line:


vpc_id     = aws_vpc.terraform_vpc.id

Here, we used the following syntax to link the resource:

Resourcename.resource_name.parameter

Now, let’s hit the terraform command  that will create our resources defined in Amazon.

$ terraform apply

Type yes, for confirmation

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Creating a AWS VPC and subnet using terraform with simple example
  • Kubernetes deployment, service and ingress how they are inter linked with simple example
  • Creating a Private and Public subnet network in AWS
©2021 My Devops Journey | Built using WordPress and Responsive Blogily theme by Superb