-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
208 lines (178 loc) · 5.22 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
208
##############################################################################
# HashiCorp Terraform and Vault Workshop
#
# This Terraform configuration will create the following:
#
# AWS VPC with a subnet
# A Linux server running HashiCorp Vault and a simple application
# A hosted RDS MySQL database server
/* This is the provider block. We recommend pinning the provider version to
a known working version. If you leave this out you'll get the latest
version. */
provider "aws" {
version = "= 2.17.0"
region = "${var.region}"
}
resource "aws_vpc" "workshop" {
cidr_block = "${var.address_space}"
tags = {
Name = "${var.prefix}-workshop"
}
}
resource "aws_subnet" "subnet" {
vpc_id = "${aws_vpc.workshop.id}"
availability_zone = "${var.region}b"
cidr_block = "${var.subnet_prefix}"
tags = {
Name = "${var.prefix}-workshop-subnet"
}
}
resource "aws_subnet" "subnet2" {
vpc_id = "${aws_vpc.workshop.id}"
availability_zone = "${var.region}c"
cidr_block = "10.0.11.0/24"
tags = {
Name = "${var.prefix}-workshop-subnet"
}
}
resource "aws_internet_gateway" "main-gw" {
vpc_id = "${aws_vpc.workshop.id}"
}
resource "aws_route_table" "main-public" {
vpc_id = "${aws_vpc.workshop.id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.main-gw.id}"
}
}
resource "aws_route_table_association" "main-public-1-a" {
subnet_id = "${aws_subnet.subnet.id}"
route_table_id = "${aws_route_table.main-public.id}"
}
resource "aws_security_group" "vault-sg" {
name = "${var.prefix}-sg"
description = "Vault Security Group"
vpc_id = "${aws_vpc.workshop.id}"
ingress {
from_port = 8200
to_port = 8200
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 5000
to_port = 5000
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_security_group" "mysql-workshop-sg" {
name = "${var.prefix}-mysql-sg"
description = "Mysql Security Group"
vpc_id = "${aws_vpc.workshop.id}"
ingress {
from_port = 3306
to_port = 3306
protocol = "tcp"
cidr_blocks = ["10.0.0.0/8"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
module "ssh-keypair-aws" {
source = "github.com/scarolan/ssh-keypair-aws"
name = "${var.prefix}-workshop"
}
resource "aws_instance" "vault-server" {
ami = "${data.aws_ami.ubuntu.id}"
instance_type = "${var.vm_size}"
subnet_id = "${aws_subnet.subnet.id}"
vpc_security_group_ids = ["${aws_security_group.vault-sg.id}"]
associate_public_ip_address = "true"
key_name = "${module.ssh-keypair-aws.name}"
tags = {
Name = "${var.prefix}-tf-workshop"
TTL = "72"
owner = "[email protected]"
}
connection {
type = "ssh"
user = "ubuntu"
private_key = "${module.ssh-keypair-aws.private_key_pem}"
host = "${aws_instance.vault-server.public_ip}"
}
provisioner "file" {
source = "files/"
destination = "/home/ubuntu/"
}
# Put a copy of the ssh key onto the local workstation
#provisioner "local-exec" {
/* interpreter = ["PowerShell", "-Command"]
command = <<-EOF
New-Item -ItemType Directory -Force -Path $${env:HOMEPATH}\.ssh
Write-Output @"
${module.ssh-keypair-aws.private_key_pem}
"@ | Out-File $${env:HOMEPATH}\.ssh\id_rsa
((Get-Content $${env:HOMEPATH}\.ssh\id_rsa) -join "`n") + "`n" | Set-Content -NoNewline $${env:HOMEPATH}\.ssh\id_rsa
EOF
}
*/
provisioner "remote-exec" {
inline = [
"chmod -R +x /home/ubuntu/",
"sleep 30",
"MYSQL_ENDPOINT=${aws_db_instance.vault-demo.endpoint} MYSQL_HOST=${aws_db_instance.vault-demo.address} MYSQL_PORT=${aws_db_instance.vault-demo.port} /home/ubuntu/setup.sh"
]
}
}
resource "aws_db_subnet_group" "default" {
name = "${var.prefix}-subnet-group"
subnet_ids = ["${aws_subnet.subnet.id}", "${aws_subnet.subnet2.id}"]
tags = {
Name = "tf-workshop-subnet"
}
}
resource "aws_db_instance" "vault-demo" {
allocated_storage = 20
identifier = "${var.prefix}-tf-workshop-rds"
db_subnet_group_name = "${aws_db_subnet_group.default.id}"
storage_type = "gp2"
engine = "mysql"
engine_version = "5.7"
instance_class = "db.t2.micro"
final_snapshot_identifier = "foo"
skip_final_snapshot = true
name = "wsmysqldatabase"
username = "hashicorp"
password = "Password123!"
parameter_group_name = "default.mysql5.7"
vpc_security_group_ids = ["${aws_security_group.mysql-workshop-sg.id}"]
}