Skip to content

Instantly share code, notes, and snippets.

#provider
provider "aws" {
profile = "root"
region = "ap-south-1"
}
# vpc
resource "aws_vpc" "tf_vpc" {
cidr_block = "192.168.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags= {
Name = "tf-vpc"
}
}
# public subnet
resource "aws_subnet" "pub_subnet1" {
vpc_id = aws_vpc.tf_vpc.id
availability_zone = "ap-south-1a"
cidr_block = "192.168.1.0/24"
map_public_ip_on_launch = true
tags= {
Name = "pub-subnet1"
}
resource "aws_security_group" "tf_sql_sg" {
depends_on = [
aws_route_table_association.tf_ng_assoc
]
name = "tf_sql_sg"
description = "mysql inbound"
vpc_id = aws_vpc.tf_vpc.id
ingress {
description = "mysql"
# Internet Gateway
resource "aws_internet_gateway" "tf_ig" {
vpc_id = aws_vpc.tf_vpc.id
tags = {
Name = "tf-ig"
}
}
# IGW Route Table
# EIP
resource "aws_eip" "tf-eip" {
tags = {
"Name" = "vpc-eip"
}
}
# NAT Gateway
resource "aws_nat_gateway" "tf_ngw" {
resource "aws_instance" "wordpress" {
ami = "ami-0732b62d310b80e97"
instance_type = "t2.micro"
subnet_id = aws_subnet.pub_subnet1.id
security_groups = [aws_security_group.tf_wp_sg.id]
key_name = "key1"
tags = {
Name = "wordpress"
}
resource "aws_instance" "wordpress" {
ami = "ami-0732b62d310b80e97"
instance_type = "t2.micro"
subnet_id = aws_subnet.pub_subnet1.id
security_groups = [aws_security_group.tf_wp_sg.id]
key_name = "key1"
tags = {
Name = "wordpress"
}
resource "aws_instance" "mysql" {
depends_on = [
aws_instance.wordpress
]
ami = "ami-0732b62d310b80e97"
instance_type = "t2.micro"
subnet_id = aws_subnet.pvt_subnet2.id
security_groups = [aws_security_group.tf_sql_sg.id]
key_name = "key1"
user_data = <<END
# security group for wordpress
resource "aws_security_group" "tf_wp_sg" {
name = "tf_wp_sg"
description = "wordpress inbound"
vpc_id = aws_vpc.tf_vpc.id
ingress {
description = "http"
from_port = 80
to_port = 80