Welcome to WordPress. This is your first post. Edit or delete it, then start writing!
Create VPC
resource “aws_vpc” “WPVPC” {
cidr_block = “10.0.0.0/16”
tags = {
Name = “WPVPC”
}
}
Create two public subnets (for high availability)
resource “aws_subnet” “WPPubsubnet1” {
vpc_id = aws_vpc.WPVPC.id
cidr_block = “10.0.1.0/24”
availability_zone = “ap-southeast-2a”
tags = {
Name = “WPPublicSubnet1”
}
}
resource “aws_subnet” “WPPubsubnet2” {
vpc_id = aws_vpc.WPVPC.id
cidr_block = “10.0.2.0/24”
availability_zone = “ap-southeast-2b”
tags = {
Name = “WPPublicSubnet2”
}
}
Create two private subnets
resource “aws_subnet” “WPPrisubnet1” {
vpc_id = aws_vpc.WPVPC.id
cidr_block = “10.0.3.0/24”
availability_zone = “ap-southeast-2a”
tags = {
Name = “WPPrivateSubnet1”
}
}
resource “aws_subnet” “WPPrisubnet2” {
vpc_id = aws_vpc.WPVPC.id
cidr_block = “10.0.4.0/24”
availability_zone = “ap-southeast-2b”
tags = {
Name = “WPPrivateSubnet2”
}
}
Create internet gateway to enable internet connection to public subnets
resource “aws_internet_gateway” “WPGW” {
vpc_id = aws_vpc.WPVPC.id
tags = {
Name = “WPGateway”
}
}
Create route table
resource “aws_route_table” “WPtable” {
vpc_id = aws_vpc.WPVPC.id
route {
cidr_block = “0.0.0.0/0”
gateway_id = aws_internet_gateway.WPGW.id
}
tags = {
Name = “WPRouteTable”
}
}
Route table association for public subnets
resource “aws_route_table_association” “PubRouT1” {
subnet_id = aws_subnet.WPPubsubnet1.id
route_table_id = aws_route_table.WPtable.id
}
resource “aws_route_table_association” “PubRouT2” {
subnet_id = aws_subnet.WPPubsubnet2.id
route_table_id = aws_route_table.WPtable.id
}
Create security group to allow HTTP and SSH access
resource “aws_security_group” “allow_WP” {
name = “wp-sg”
description = “Security group for WordPress”
vpc_id = aws_vpc.WPVPC.id
# Inbound rules
ingress {
description = “Allow SSH”
from_port = 22
to_port = 22
protocol = “tcp”
cidr_blocks = [“0.0.0.0/0”]
}
ingress {
description = “Allow HTTP”
from_port = 80
to_port = 80
protocol = “tcp”
cidr_blocks = [“0.0.0.0/0”]
}
# Outbound rules
egress {
from_port = 0
to_port = 0
protocol = “-1”
cidr_blocks = [“0.0.0.0/0”]
}
tags = {
Name = “WPSecurityGroup”
}
}
Create EC2 instance for WordPress
resource “aws_instance” “wp_instance” {
ami = “ami-000f543aeac89dd13”
instance_type = “t3.small”
availability_zone = “ap-southeast-2a”
key_name = “WP”
subnet_id = aws_subnet.WPPubsubnet1.id
security_groups = [aws_security_group.allow_WP.id]
associate_public_ip_address = true
tags = {
Name = “WPInstance”
}
}