Provisioning of Instances in Different Subnets(i.e. Private & Public) in AWS (implementing VPC,EC2,Bastion Host) 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.

Bastion Host

A bastion host is a special-purpose computer on a network specifically designed and configured to withstand attacks. The computer generally hosts a single application, for example a proxy server , and all other services are removed or limited to reduce the threat to the computer. It is hardened in this manner primarily due to its location and purpose, which is either on the outside of a firewall or in a demilitarized zone (DMZ) and usually involves access from untrusted networks or computers.Bastion hosts are related to multi-homed hosts and screened hosts.

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.

We apply internet connection in database server by using bastion host security group in that instance

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 NAT gateway for connect our VPC /Network to the internet world and attach this gateway to our VPC in the public network


Update the routing table of the private subnet, so that to access the internet it uses the nat gateway created in the public subnet

Routing Table

Create a key pair which we use to login into instances

Key Pair

Now we create some security groups for our instances & bastion host

Security Group for WordPress
Security Group for Bastion Host
Allow Bastion Host
Security Group for WordPress

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.

WordPress Instance

Launch an EC2 instance 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.

MySQL Instances

Add Elastic IP

Elastic IP

For complete code Go to my Github Repo : Click Here

Now Run some terraform commands :

At first initialize the terraform

terraform init


Now validate the code

terraform validate


Now Run the code

terraform apply — auto-approve


Output Images

Elastic IP
Internet Gateway
NAT Gateway
Web Page
Routing Table
Security Group / Firewall

Destroy the complete infrastructure after solving use case

terraform destroy


Thank You !!!!!!




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

Recommended from Medium

How to Access Data From the Beta Channel of Graph API

Time to Effect — Operations of large number of Kubernetes Clusters

Making the Business Case for Low-Code/No-Code Platform Adoption

A basic introduction to Unity for newcomers(2D edition).

Fall of Lion Team : Code Standard Part 1

Checking Cache to Catch the Chunking Chafed

Fun and Meaningful Retrospectives

NETCAT-A Hacking Tool

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

Autoscaling Unattended RPA with Power Automate

FinOps part 2-AWS-Instance-type

Cloud Computing Solutions For Sustainability: Achieve Net-Zero