Terraform with AWS: Create AWS VPC with Private & Public Subnet with NAT Gateway & Security Group

Aniket Patel
FAUN — Developer Community 🐾
5 min readJan 29, 2022

--

Create VPC with both private and public subnet along with NAT using Terraform.

Hello geeks, In this blog we’ll learn how to configure an AWS VPC network using Terraform.

Here we’re creating 1 VPC with Private and Public subnets, 1 Internet Gateway, 1 Security Group with 1 NAT Gateway. For the demo I’ll create multiple custom route tables and associate them with subnets.

What is Terraform?

Terraform is so popular now a days. Terraform is a tool for building infrastructure with various technologies including AWS, Azure or GCP. Terraform enables you to create and manage infrastructure with code and codes can be stored in version control.

Let’s start !

Prerequisite

  1. Terraform : https://learn.hashicorp.com/terraform/getting-started/install.html
  2. AWS IAM Account API Keys (Access Key & Secret Key)
  3. AWS CLI : https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html

Let’s start with our tutorial!

1. Create “provider.tf” file

This file aims to tell Terraform about which provider you’re using. We’re using AWS over here so we’re mentioning provider "aws" {} in this file.

provider "aws" {

}

If you want to use another cloud provider such as GCP or Azure, you need to change this. I have already declared the region of AWS in this file. So now let’s first create files for variables.

2. Initialize our directory with terraform

Open up your terminal and change your directory to our tutorial directory. Now it’s time to initialize our working directory with terraform with the use of terraform init .

3. Create “terraform.tfvars” file

To persist variable values, create a “terraform.tfvars ” file, and assign variables within this file. Now add the following content into newly created file.

AWS_ACCESS_KEY = "<AWS_ACCESS_KEY>"
AWS_SECRET_KEY = "<AWS_SECRET_KEY>"

We’re adding IAM account’s keys for creating and deleting our resources on AWS and to give the permissions for that.

For all files which match terraform.tfvars or *.auto.tfvarspresent in the current directory, Terraform automatically loads them to populate variables. If the file is named something else, you can use the -var-file flag directly to specify a file.

4. Create “vars.tf” file

All variables that we are going to use in this tutorial will be added in this file.

variable "AWS_ACCESS_KEY" {}
variable "AWS_SECRET_KEY" {}
variable "AWS_REGION" {
default = "ap-south-1"
}

We’re using terraform.tfvars file here so we don’t have to specify the values of AWS_ACCESS_KEY & AWS_SECRET_KEY .

Now let’s first modify our provider file first and add the following content into provider.tf file.

provider "aws" {
region = var.AWS_REGION
access_key = var.AWS_ACCESS_KEY
secret_key = var.AWS_SECRET_KEY
}

5. Create our Networking module

Now it’s time to create our Networking module over here. So first create module folder and then inside it create Netwokring folder. Your structure should look like “module>Networking”.

A module is a container for multiple resources that are used together. Every Terraform configuration has at least one module, known as its root module, which consists of the resources defined in the .tf files in the main working directory.

6. Create “main.tf” file inside

Let’s create main.tf file inside the Networking folder.

Now again in the modules>Networking folder we’re creating vars.tf file to declare all the variables that we’re using in the main.tf file.

7. Create “production.tf” file

Let’s create production.tf file in the root directory.

You can refer https://learn.hashicorp.com/tutorials/terraform/module-use to get more details on Module configuration.

Create one more file output.tf inside Networking folder.

Now we’re ready with everything. So open up your terminal and change your directory to your working directory.

8. Run “terraform init” & “terraform validate”

Run terraform init to initialize your directory with Terraform and it’ll download all the required modules.

Now run terraform validate to validates the configuration files in your directory. You’ll get the following output if your validation process succeed.

9. Run “terraform plan”

The terraform plan is a command which is used to create an execution plan. Terraform performs a refresh, unless explicitly disabled, and then determines what actions are necessary to achieve the desired state specified in the configuration files. This command is useful to check whether your execution plan matches your expectations without making any changes to real resources.

terraform plan

You’ll get the output like above image. Finally, it’s time to deploy!

10. Run “terraform apply”

Run terraform applyto deploy the resources on AWS.

Hurray !!! Our VPC is successfully deployed on AWS.

Tip: if you are looking for deleting the resource, terraform destroy will clean up all the resources created for you.

In case if you want the whole code you can clone my repository.

Code: https://github.com/aniketpatel12/terraform_aws-vpc_setup

Thanks for taking out time to read! if you have anything to add please send a response or add a note!

If you want to discuss out something interesting then you can connect with me on LinkedIn.

LinkedIn profile: https://www.linkedin.com/in/aniket-patel-2a52851a3/

Join FAUN: Website 💻|Podcast 🎙️|Twitter 🐦|Facebook 👥|Instagram 📷|Facebook Group 🗣️|Linkedin Group 💬| Slack 📱|Cloud Native News 📰|More.

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇

--

--

AWS Community Builder | DevOps Engineer | 1xAWS | 1xOracle | Docker | Kubernetes | DevOps | Serverless