Web Portal Using Personal VPC
Task-3
Write a Infrastructure as code using terraform, which automatically create a VPC.
resource “aws_vpc” “main” {
cidr_block = “190.168.0.0/16”
instance_tenancy = “default”
enable_dns_hostnames = “true”
tags = {
Name = “mytfoffice”
}
}
In that VPC we have to create 2 subnets:
a) public subnet [ Accessible for Public World! ]
resource “aws_subnet” “mainsubneta” {
depends_on = [ aws_vpc.main,]
vpc_id = “${aws_vpc.main.id}”
cidr_block = “190.168.0.0/24”
availability_zone = “ap-south-1a”
map_public_ip_on_launch = “true”
tags = {
Name = “public-1a”
}
}
b) private subnet [ Restricted for Public World! ]
resource “aws_subnet” “mainsubnetb” {
depends_on = [ aws_vpc.main,]
vpc_id = “${aws_vpc.main.id}”
cidr_block = “190.168.1.0/24”
availability_zone = “ap-south-1b”
tags = {
Name = “private-1b”
}
}
BOTH PUBLIC AND PRIVATE SUBNETS.
Create a public facing internet gateway for connect our VPC/Network to the internet world and attach this gateway to our VPC.
resource “aws_internet_gateway” “gw” {
depends_on = [aws_vpc.main,]
vpc_id = “${aws_vpc.main.id}”
tags = {
Name = “tfgateway”
}
}
Create a routing table for Internet gateway so that instance can connect to outside world, update and associate it with public subnet.
resource “aws_route_table” “r” {
depends_on = [ aws_vpc.main,]
vpc_id = “${aws_vpc.main.id}”
route {
cidr_block = “0.0.0.0/0”
gateway_id = “${aws_internet_gateway.gw.id}”
}
tags = {
Name = “tfrouterule”
}
}
resource “aws_route_table_association” “rtastn” {
subnet_id = aws_subnet.mainsubneta.id
route_table_id = aws_route_table.r.id
}
creating key pairs before attaching the key
module “key_pair”{
source = “terraform-aws-modules/key-pair/aws”
key_name = “vpc-key”
public_key = tls_private_key.this.public_key_openssh
}
Creating security group for both wordpress and mysql
WORDPRESS
resource “aws_security_group” “sgwp” {
name = “sg_wordpress”
description = “Allow TLS inbound traffic”
vpc_id = “${aws_vpc.main.id}”
ingress {
description = “http”
from_port = 80
to_port = 80
protocol = “tcp”
cidr_blocks = [“0.0.0.0/0”]
}
ingress {
description = “ssh”
from_port = 22
to_port = 22
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 = “sg_wordpress”
}
}
MySql
resource “aws_security_group” “sgsql” {
name = “sg_mysql”
description = “Allow TLS inbound traffic”
vpc_id = “${aws_vpc.main.id}”
ingress {
description = “http”
from_port = 3306
to_port = 3306
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 = “sg_MySQL”
}
}
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.
resource “aws_instance” “wpinstance” {
depends_on = [ tls_private_key.this,aws_security_group.sgwp,]
ami = “ami-ff82f990”
instance_type = “t2.micro”
key_name = “vpc-key”
vpc_security_group_ids = [ aws_security_group.sgwp.id ]
subnet_id = “${aws_subnet.mainsubneta.id}”
tags = {
Name = “WordPressInstance”
}
}
Launch an ec2 instance which has MYSQL setup already with security group allowing port 3306 in private subnet so that our wordpress vm can connect with the same.Also attach the key with the same.
resource “aws_instance” “sqlinstances” {
depends_on = [ tls_private_key.this,aws_security_group.sgsql,]
ami = “ami-08706cb5f68222d09”
instance_type = “t2.micro”
key_name = “vpc-key”
vpc_security_group_ids = [ aws_security_group.sgsql.id ]
subnet_id = “${aws_subnet.mainsubnetb.id}”
tags = {
Name = “MySQL-Instance”
}
}
TASK 3 COMPLETED…..
LETS DESTROY THIS SETUP
terraform destroy -auto-approve