Auto Creating Web portal on Cloud by creating VPC and configuring it for better security.

What is EC2?

What is VPC?

Problem Statement…

Here the Task starts..

  1. First we have to make one folder in which we have to init the terraform.

Open a notepad with file extension .tf

  1. Inside the file first we have to write..

2. Creating the VPC

resource "aws_vpc" "myvpc" {
cidr_block = "192.168.0.0/16"
instance_tenancy = "default"
enable_dns_hostnames = "true"
tags = {
Name = "abhivpc"
}
}

3. Creating the subnets..

resource "aws_subnet" "publicSubnet" {
vpc_id = aws_vpc.myvpc.id
cidr_block = "192.168.0.0/24"
availability_zone = "ap-south-1a"
map_public_ip_on_launch = "true"tags = {
Name = "subnet1"
}
}
resource "aws_subnet" "privateSubnet" {
vpc_id = aws_vpc.myvpc.id
cidr_block = "192.168.1.0/24"
availability_zone = "ap-south-1b"tags = {
Name = "subnet2"
}
}

4. Creation of internet gateway…

resource "aws_internet_gateway" "internetGateway" {
vpc_id = aws_vpc.myvpc.idtags = {
Name = "my_internetgateway"
}
}

5. Creation of Routing Tables….

resource "aws_route_table" "routingTable" {
vpc_id = aws_vpc.myvpc.idroute {

gateway_id = aws_internet_gateway.internetGateway.id
cidr_block = "0.0.0.0/0"
}tags = {
Name = "myRoutingTable"
}
}
resource "aws_route_table_association" "association" {
subnet_id = aws_subnet.publicSubnet.id
route_table_id = aws_route_table.routingTable.id
}

6. Creation of security groups…

This is for wordpress

resource "aws_security_group" "wordpress_sg" {
depends_on = [ aws_vpc.myvpc ]
name = "wordpress_sg"
vpc_id = aws_vpc.myvpc.idingress {
description = "SSH"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [ "0.0.0.0/0"]
}ingress {
description = "HTTP"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = [ "0.0.0.0/0" ]
}egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}tags = {
Name = "wordpress_sg"
}
}

And this is for mysql

resource "aws_security_group" "mysql_sg" {
depends_on = [ aws_vpc.myvpc ]
name = "mysql_sg"
vpc_id = aws_vpc.myvpc.idingress {
description = "MYSQL"
from_port = 3306
to_port = 3306
protocol = "tcp"
security_groups = [ aws_security_group.wordpress_sg.id ]
}egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}tags = {
Name = "mysql_sg"
}
}

7. Creation of instances for wordpress and mysql and final open in chrome browser.

resource "aws_instance" "wordpress_os" {
ami= "ami-7e257211"
instance_type = "t2.micro"
subnet_id = aws_subnet.publicSubnet.id
vpc_security_group_ids = [ aws_security_group.wordpress_sg.id ]
key_name = "mykeys"
tags = {
Name = "wordpress"
}
}
resource "aws_instance" "database" {
ami= "ami-0447a12f28fddb066"
instance_type = "t2.micro"
subnet_id = aws_subnet.privateSubnet.id
vpc_security_group_ids = [ aws_security_group.mysql_sg.id ]
key_name = "mykeys"
tags = {
Name = "database"
}
}
resource "null_resource" "nulllocal1" {
depends_on = [
aws_instance.wordpress_os ,
aws_instance.database ,
]
provisioner "local-exec" {
command = "start chrome ${aws_instance.wordpress_os.public_ip}"
}
}

After saving this file run this cmd.

Output of the code…

For destroy all the setup run this

Here I finish my task

Thank you for reading…

--

--

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