Provisioning of Instances in Different Subnets(i.e. Private & Public) using Terraform


Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions.

Configuration files describe to Terraform the components needed to run a single application or your entire datacenter. Terraform generates an execution plan describing what it will do to reach the desired state, and then executes it to build the described infrastructure. As the configuration changes, Terraform is able to determine what changed and create incremental execution plans which can be applied.

The infrastructure Terraform can manage includes low-level components such as compute instances, storage, and networking, as well as high-level components such as DNS entries, SaaS features, etc.


Amazon Web Services (AWS) is a subsidiary of Amazon that provides on-demand cloud computing platforms and APIs to individuals, companies, and governments, on a metered pay-as-you-go basis. In aggregate, these cloud computing web services provide a set of primitive abstract technical infrastructure and distributed computing building blocks and tools. One of these services is Amazon Elastic Compute Cloud (EC2), which allows users to have at their disposal a virtual cluster of computers, available all the time, through the Internet.

Problem Statement / Task :

We have to create a web portal for our company with all the security as much as possible.

So, we use Wordpress software with dedicated database server.

Database should not be accessible from the outside world for security purposes.

We only need to public the WordPress to clients.

So here are the steps for proper understanding!


Write a Infrastructure as code using terraform for AWS.


Now we have to create a VPC


In these VPC we have to create 2 subnets listed below:

public subnet [ Accessible for Public World !]

private subnet [ Restricted for Public World! ]


Create a public facing internet gateway for connect our VPC/Network to the internet world and attach this gateway to our VPC

Internet Gateway

Create a routing table for Internet gateway so that instance can connect to outside world, update and associate it with public subnet

Routing Table

Create a key pair which we use to login into instances

Key Pair

Launch an ec2 instance which has WordPress setup already having the security group allowing port 80 so that our client can connect to our WordPress site.

Also attach the key to instance for further login into it.

Security Group for WordPress
WordPress Instances

Launch an EC2instance which has MySQL setup already with security group allowing port 3306 in private subnet so that our WordPress instances can connect with the same.

Also attach the key with the same.

Security Group for MySQL
MySQL Instances

Print IP of both (WordPress & My SQL Instances)

Print IP

Save WordPress Instance IP into a text file

Save IP

Now execute some terraform commands to run these code

terraform init


terraform validate

Validation of Terraform Code

terraform apply — auto-approve


Now we can verify our service status from AWS Console.

Internet Gateway
Routing Table
EC2 Instances

Final Output:

Wordpress Site
CMD Output

For Complete code of this task : Click Here

Thank You




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

First blog post for Launch School

Swift / Java

Failure is a part of the game

View logs from server/local machine with simple web application asp.net C#

How to get government employees to use GitHub

Top 3 Computer Science University Courses for Getting a Software Engineering Job or Internship

Auth using AWS Amplify In Flutter

FlutterForce — #Week 100

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ashwani Singh

Ashwani Singh

More from Medium

[GCP] Restore Cloud SQL to instance in another project

Generate Kubernetes Manifest Files Using Move2Kube

Metricbeat for MySQL

How to SSH to your VM on GCP from Windows