-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
123 lines (104 loc) · 2.83 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# AWS Provider
provider "aws" {
region = "eu-west-2"
}
# VPC
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "sajid-vpc"
}
}
# Subnet
resource "aws_subnet" "subnet" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
tags = {
Name = "sajid-subnet"
}
}
# Internet Gateway
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.main.id
}
# Route Table
resource "aws_route_table" "rt" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
}
# Route Table Association with Subnet
resource "aws_route_table_association" "rta" {
subnet_id = aws_subnet.subnet.id
route_table_id = aws_route_table.rt.id
}
# Security Group
resource "aws_security_group" "sg" {
vpc_id = aws_vpc.main.id
# Ingress rule to allow HTTP traffic on port 80 from anywhere
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# Ingress rule to allow SSH traffic on port 22 from anywhere
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# Egress rule to allow all outbound traffic
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "sajid-security-group"
}
}
# Allocate Elastic IP
resource "aws_eip" "eip" {
domain = "vpc"
}
# Associate Elastic IP
resource "aws_eip_association" "eip_assoc" {
instance_id = aws_instance.web.id
allocation_id = aws_eip.eip.id
}
# Launch the EC2 instance, configure to install and run WordPress
resource "aws_instance" "web" {
ami = "ami-079bd1a083298389f" # Amazon Linux 2 AMI
instance_type = "t2.micro"
subnet_id = aws_subnet.subnet.id
#security_groups = [aws_security_group.sg.name]
vpc_security_group_ids = [aws_security_group.sg.id] # security group IDs
key_name = "ssh_key"
user_data = <<-EOF
#!/bin/bash
yum update -y
amazon-linux-extras enable php7.4
yum clean metadata
yum install -y httpd php php-mysqlnd mariadb-server
systemctl start httpd
systemctl enable httpd
systemctl start mariadb
systemctl enable mariadb
cd /var/www/html
sudo wget http://wordpress.org/latest.tar.gz
sudo tar -xvzf latest.tar.gz
sudo cp -r wordpress/* /var/www/html/
sudo rm -rf wordpress latest.tar.gz
sudo chown -R apache:apache /var/www/html/
sudo chown -R apache:apache /var/www/html/
sudo systemctl restart httpd
EOF
tags = {
Name = "wordpress-server"
}
}