Debian Codegraph @

samples/guestbook/aws/main.tf

raw
1 # Copyright 2018 The Go Cloud Development Kit Authors
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # https://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 terraform {
16 required_version = "~>0.12"
17 }
18
19 provider "aws" {
20 version = "~> 2.7"
21 region = var.region
22 }
23
24 provider "random" {
25 version = "~> 2.1"
26 }
27
28 # Firewalls
29
30 resource "aws_security_group" "guestbook" {
31 name_prefix = "guestbook"
32 description = "Sandbox for the Guestbook Go CDK sample app."
33
34 ingress {
35 from_port = 22
36 to_port = 22
37 protocol = "tcp"
38 cidr_blocks = ["0.0.0.0/0"]
39 description = "Public SSH access"
40 }
41
42 ingress {
43 from_port = 8080
44 to_port = 8080
45 protocol = "tcp"
46 cidr_blocks = ["0.0.0.0/0"]
47 description = "Public HTTP access"
48 }
49
50 ingress {
51 from_port = 3306
52 to_port = 3306
53 protocol = "tcp"
54 self = true
55 description = "MySQL within group"
56 }
57
58 egress {
59 from_port = 0
60 to_port = 0
61 protocol = "-1"
62 cidr_blocks = ["0.0.0.0/0"]
63 description = "All outgoing traffic allowed"
64 }
65 }
66
67 # SQL Database (RDS)
68
69 resource "random_string" "db_password" {
70 special = false
71 length = 20
72 }
73
74 resource "aws_db_instance" "guestbook" {
75 identifier_prefix = "guestbook"
76 engine = "mysql"
77 engine_version = "5.6.39"
78 instance_class = "db.t2.micro"
79 allocated_storage = 20
80 username = "root"
81 password = random_string.db_password.result
82 name = "guestbook"
83 publicly_accessible = true
84 vpc_security_group_ids = [aws_security_group.guestbook.id]
85 skip_final_snapshot = true
86
87 provisioner "local-exec" {
88 # TODO(light): Reuse credentials from Terraform.
89 command = "go run '${path.module}'/provision_db/main.go -host='${aws_db_instance.guestbook.address}' -region='${var.region}' -security_group='${aws_security_group.guestbook.id}' -database=guestbook -password='${random_string.db_password.result}' -schema='${path.module}'/../schema.sql"
90 }
91 }
92
93 # Blob Storage (S3)
94
95 resource "aws_s3_bucket" "guestbook" {
96 bucket_prefix = "guestbook"
97 }
98
99 resource "aws_s3_bucket_object" "aws" {
100 bucket = aws_s3_bucket.guestbook.bucket
101 key = "aws.png"
102 content_type = "image/png"
103 source = "${path.module}/../blobs/aws.png"
104 }
105
106 resource "aws_s3_bucket_object" "gcp" {
107 bucket = aws_s3_bucket.guestbook.bucket
108 key = "gcp.png"
109 content_type = "image/png"
110 source = "${path.module}/../blobs/gcp.png"
111 }
112
113 resource "aws_s3_bucket_object" "gophers" {
114 bucket = aws_s3_bucket.guestbook.bucket
115 key = "gophers.jpg"
116 content_type = "image/jpeg"
117 source = "${path.module}/../blobs/gophers.jpg"
118 }
119
120 # Paramstore (SSM)
121
122 resource "aws_ssm_parameter" "motd" {
123 name = var.paramstore_var
124 type = "String"
125 value = "ohai from AWS"
126 overwrite = "true"
127 }
128
129 # Compute (EC2)
130
131 resource "aws_iam_role" "guestbook" {
132 name_prefix = "guestbook"
133
134 assume_role_policy = <<EOF
135 {
136 "Version": "2012-10-17",
137 "Statement": {
138 "Effect": "Allow",
139 "Principal": {"Service": "ec2.amazonaws.com"},
140 "Action": "sts:AssumeRole"
141 }
142 }
143 EOF
144
145 }
146
147 resource "aws_iam_role_policy" "guestbook" {
148 name_prefix = "Guestbook-Policy"
149 role = aws_iam_role.guestbook.id
150
151 policy = <<EOF
152 {
153 "Version": "2012-10-17",
154 "Statement": {
155 "Effect": "Allow",
156 "Action": [
157 "s3:GetObject",
158 "ssm:DescribeParameters",
159 "ssm:GetParameter",
160 "ssm:GetParameters",
161 "xray:PutTraceSegments",
162 "xray:PutTelemetryRecords"
163 ],
164 "Resource": "*"
165 }
166 }
167 EOF
168
169 }
170
171 resource "aws_iam_instance_profile" "guestbook" {
172 name_prefix = "guestbook"
173 role = aws_iam_role.guestbook.name
174 }
175
176 data "aws_ami" "debian" {
177 most_recent = true
178
179 filter {
180 name = "product-code"
181 values = ["55q52qvgjfpdj2fpfy9mb1lo4"]
182 }
183
184 filter {
185 name = "product-code.type"
186 values = ["marketplace"]
187 }
188
189 filter {
190 name = "architecture"
191 values = ["x86_64"]
192 }
193
194 owners = ["679593333241"]
195 }
196
197 resource "aws_key_pair" "guestbook" {
198 key_name_prefix = "guestbook"
199 public_key = var.ssh_public_key
200 }
201
202 resource "aws_instance" "guestbook" {
203 ami = data.aws_ami.debian.id
204 instance_type = "t2.micro"
205 vpc_security_group_ids = [aws_security_group.guestbook.id]
206 iam_instance_profile = aws_iam_instance_profile.guestbook.id
207 key_name = aws_key_pair.guestbook.key_name
208
209 connection {
210 host = coalesce(self.public_ip, self.private_ip)
211 type = "ssh"
212 user = "admin"
213 }
214
215 provisioner "file" {
216 source = "${path.module}/../guestbook"
217 destination = "/home/admin/guestbook"
218 }
219
220 provisioner "remote-exec" {
221 inline = ["chmod +x /home/admin/guestbook"]
222 }
223 }
224