-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
104 lines (82 loc) · 2.74 KB
/
Copy pathmain.tf
File metadata and controls
104 lines (82 loc) · 2.74 KB
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
##################################################################################
# PROVIDERS
##################################################################################
provider "aws" {
access_key = var.aws_access_key
secret_key = var.aws_secret_key
region = var.aws_region
}
##################################################################################
# DATA
##################################################################################
data "aws_ssm_parameter" "ami" {
name = "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2"
}
##################################################################################
# RESOURCES
##################################################################################
# NETWORKING #
resource "aws_vpc" "vpc" {
cidr_block = var.vpc_cidr_block
enable_dns_hostnames = var.enable_dns_hostnames
tags = local.common_tags
}
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.vpc.id
tags = local.common_tags
}
resource "aws_subnet" "subnet1" {
cidr_block = var.vpc_subnet1_cidr_block
vpc_id = aws_vpc.vpc.id
map_public_ip_on_launch = var.map_public_ip_on_launch
tags = local.common_tags
}
# ROUTING #
resource "aws_route_table" "rtb" {
vpc_id = aws_vpc.vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
tags = local.common_tags
}
resource "aws_route_table_association" "rta-subnet1" {
subnet_id = aws_subnet.subnet1.id
route_table_id = aws_route_table.rtb.id
}
# SECURITY GROUPS #
# Nginx security group
resource "aws_security_group" "nginx-sg" {
name = "nginx_sg"
vpc_id = aws_vpc.vpc.id
# HTTP access from anywhere
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# outbound internet access
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = local.common_tags
}
# INSTANCES #
resource "aws_instance" "nginx1" {
ami = nonsensitive(data.aws_ssm_parameter.ami.value)
instance_type = var.instance_type
subnet_id = aws_subnet.subnet1.id
vpc_security_group_ids = [aws_security_group.nginx-sg.id]
user_data = <<EOF
#! /bin/bash
sudo amazon-linux-extras install -y nginx1
sudo service nginx start
sudo rm /usr/share/nginx/html/index.html
echo '<html><head><title>Taco Team Server</title></head><body style=\"background-color:#1F778D\"><p style=\"text-align: center;\"><span style=\"color:#FFFFFF;\"><span style=\"font-size:28px;\">You did it! Your reward is this amazing taco: 🌮</span></span></p></body></html>' | sudo tee /usr/share/nginx/html/index.html
EOF
tags = local.common_tags
}