-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
207 lines (173 loc) · 4.77 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
terraform {
required_version = ">= 1.5"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
hcp = {
source = "hashicorp/hcp"
version = "~> 0.82"
}
null = {
source = "hashicorp/null"
version = "~> 3.2"
}
random = {
source = "hashicorp/random"
version = "~> 3.4"
}
tls = {
source = "hashicorp/tls"
version = "~> 4.0"
}
}
}
provider "aws" {
region = var.region
default_tags {
tags = merge(
{
environment = var.env
department = var.department
owner = var.owner
application = "HashiCafe website"
},
var.extra_tags)
}
}
resource "random_integer" "product" {
min = 0
max = length(var.hashi_products) - 1
}
data "hcp_packer_artifact" "ubuntu-webserver" {
bucket_name = var.packer_bucket
channel_name = var.packer_channel
platform = "aws"
region = var.region
}
resource "aws_vpc" "hashicafe" {
cidr_block = var.address_space
enable_dns_hostnames = true
tags = {
Name = "${var.prefix}-vpc-${var.region}"
}
lifecycle {
postcondition {
condition = self.enable_dns_hostnames == true
error_message = "VPC must have DNS hostnames enabled."
}
}
}
resource "aws_subnet" "hashicafe" {
vpc_id = aws_vpc.hashicafe.id
cidr_block = var.subnet_prefix
tags = {
Name = "${var.prefix}-subnet"
}
}
resource "aws_security_group" "hashicafe" {
name = "${var.prefix}-security-group"
vpc_id = aws_vpc.hashicafe.id
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
prefix_list_ids = []
}
tags = {
Name = "${var.prefix}-security-group"
}
}
resource "aws_security_group_rule" "ingress" {
for_each = toset(["22", "80", "443"])
security_group_id = aws_security_group.hashicafe.id
type = "ingress"
description = "Inbound port ${each.value}"
from_port = each.value
to_port = each.value
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
resource "aws_internet_gateway" "hashicafe" {
vpc_id = aws_vpc.hashicafe.id
tags = {
Name = "${var.prefix}-internet-gateway"
}
}
resource "aws_route_table" "hashicafe" {
vpc_id = aws_vpc.hashicafe.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.hashicafe.id
}
}
resource "aws_route_table_association" "hashicafe" {
subnet_id = aws_subnet.hashicafe.id
route_table_id = aws_route_table.hashicafe.id
}
resource "aws_instance" "hashicafe" {
ami = data.hcp_packer_artifact.ubuntu-webserver.external_identifier
instance_type = var.instance_type
associate_public_ip_address = true
subnet_id = aws_subnet.hashicafe.id
vpc_security_group_ids = [aws_security_group.hashicafe.id]
key_name = aws_key_pair.hashicafe.key_name
tags = {
Name = "${var.prefix}-hashicafe-instance"
}
lifecycle {
precondition {
condition = data.hcp_packer_artifact.ubuntu-webserver.region == var.region
error_message = "The selected image must be in the same region as the deployed resources."
}
postcondition {
condition = self.public_dns != ""
error_message = "EC2 instance must be in a VPC that has public DNS hostnames enabled."
}
}
}
resource "aws_eip" "hashicafe" {
domain = "vpc"
}
resource "aws_eip_association" "hashicafe" {
instance_id = aws_instance.hashicafe.id
allocation_id = aws_eip.hashicafe.id
}
# We're using a little trick here so we can run the provisioner without
# destroying the VM. Do not do this in production.
resource "null_resource" "configure-web-app" {
depends_on = [aws_eip_association.hashicafe]
connection {
type = "ssh"
user = "ubuntu"
private_key = tls_private_key.hashicafe.private_key_pem
host = aws_eip.hashicafe.public_ip
}
provisioner "remote-exec" {
inline = [
"sudo mkdir /var/www/html/img",
"sudo chown -R ubuntu:ubuntu /var/www/html"
]
}
provisioner "file" {
content = templatefile("files/index.html", {
product_name = var.hashi_products[random_integer.product.result].name
product_color = var.hashi_products[random_integer.product.result].color
product_image = var.hashi_products[random_integer.product.result].image_file
})
destination = "/var/www/html/index.html"
}
provisioner "file" {
source = "files/img/"
destination = "/var/www/html/img"
}
}
resource "tls_private_key" "hashicafe" {
algorithm = "RSA"
}
resource "aws_key_pair" "hashicafe" {
key_name = "${var.prefix}-hashicafe-sshkey"
public_key = tls_private_key.hashicafe.public_key_openssh
}