Hybrid cloud task 3 Automate AWS VPC with Terraform

EC2:-

Security Groups:-

VPC:-

Subnet:-

Internet Gateway:-

Routing Table:-

Terraform:-

  • VPC
  • 2 Subnets
  • A security group for WordPress instance
  • A security group for Mysql database instance
  • Internet Gateway associated with VPC
  • Route Table inside VPC with a route that directs internet-bound traffic to the internet gateway
  • Route table associated with a subnet to make it public subnet.
  • WordPress instance launched in the public subnet with an associated security group and a key-pair to make user access WordPress blog website.
  • MySQL instance in our private subnet with an associated security group and a key-pair.
resource "aws_vpc" "tf_VPC" {
cidr_block = "192.169.0.0/16"
instance_tenancy = "default"
enable_dns_hostnames = true
tags = {
Name = "tf_VPC"
}
}
resource "aws_subnet" "sam-subnet-public" {
vpc_id = "${aws_vpc.tf_VPC.id}"
cidr_block = "192.169.0.0/24"
availability_zone = "ap-south-1a"
map_public_ip_on_launch = "true"
tags = {
Name = "aws-subnet"
}
}
resource "aws_subnet" "tf-subnet-private" {
vpc_id = "${aws_vpc.tf_VPC.id}"
cidr_block = "192.169.1.0/24"
availability_zone = "ap-south-1b"
map_public_ip_on_launch = "true"
tags = {
Name = "aws-subnet"
}
}
resource "aws_internet_gateway" "aws-igw" {
vpc_id = "${aws_vpc.tf_VPC.id}"
tags = {
Name = "aws-igw"
}
}
resource "aws_route_table" "aws-route" {
vpc_id = "${aws_vpc.tf_VPC.id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.aws-igw.id}"
}
tags = {
Name = "aws-route"
}

}
resource "aws_route_table_association" "rta" {
subnet_id = "${aws_subnet.aws-subnet-public.id}"
route_table_id = "${aws_route_table.aws-route.id}"
}
resource "aws_security_group" "sg_wp" {
name = "sg_wp"
vpc_id = "${aws_vpc.tf_VPC.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"]
}
ingress {
description = "TCP"
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_wp"
}
}
resource "aws_security_group" "sg_mysql" {
name = "sg_mysql"
vpc_id = "${aws_vpc.tf_VPC.id}"
ingress {
protocol = "tcp"
from_port = 3306
to_port = 3306
security_groups = ["${aws_security_group.sg_wp.id}"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags ={
Name= "sg_mysql"
}
}
resource "aws_instance" "wp-instance" {
ami = "ami-000cbce3e1b899ebd"
instance_type = "t2.micro"
subnet_id = "${aws_subnet.aws-subnet-public.id}"
vpc_security_group_ids = ["${aws_security_group.sg_wp.id}"]
key_name = "newaaccount"
tags ={
Name= "wp-instance"
}
}
resource "aws_instance" "mysql-instance" {
ami = "ami-08706cb5f68222d09"
instance_type = "t2.micro"
subnet_id = "${aws_subnet.aws-subnet-private.id}"
vpc_security_group_ids = ["${aws_security_group.sg_mysql.id}"]
key_name = "newaaccount"
tags ={
Name= "mysql-instance"
}
}
$ terraform apply

--

--

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