-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.infrastructure.tf
186 lines (155 loc) · 4.59 KB
/
init.infrastructure.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
/*
Description: Provision resources that help us support the infrastructure
Maintainer: Benjamin Krueger
Maintainer_email: [email protected]
Copyright 2017 Benjamin Krueger
*/
provider "aws" {
version = "~> 1.8"
profile = "${var.profile}"
region = "${var.infrastructure["region"]}"
alias = "init.infrastructure"
}
//
// Infrastructure File Storage
// Here we set up two S3 buckets:
// one to to store infrastructure related files
// one to store access logs for the infrastructure storage bucket
resource "aws_s3_bucket" "site-infrastructure-log" {
provider = "aws.init.infrastructure"
count = "${var.infrastructure["enable_storage_log"]}"
bucket = "${var.site}-infrastructure-log"
acl = "log-delivery-write"
tags {
"Description" = "Infrastructure Storage Access Logs"
"x:managed:by" = "Terraform"
"x:service:type" = "infrastructure"
}
}
resource "aws_s3_bucket" "site-infrastructure" {
provider = "aws.init.infrastructure"
count = "${var.infrastructure["create_storage"]}"
bucket = "${var.site}-infrastructure"
acl = "private"
versioning {
enabled = true
}
logging {
target_bucket = "${aws_s3_bucket.site-infrastructure-log.id}"
target_prefix = "log/"
}
tags {
"Name" = "Infrastructure Storage"
"x:managed:by" = "Terraform"
"x:service:type" = "infrastructure"
}
}
//
// Terraform State Locking
// Here we set up a DynamoDB table to store our state lock.
// https://www.terraform.io/docs/state/locking.html
resource "aws_dynamodb_table" "site-infrastructure-tf-locks" {
provider = "aws.init.infrastructure"
count = "${var.infrastructure["use_tf_dynamo_lock"]}"
name = "${var.site}-infrastructure-tf-locks"
read_capacity = 1
write_capacity = 1
hash_key = "LockID"
ttl {
attribute_name = "TimeToExist"
enabled = false
}
attribute {
name = "LockID"
type = "S"
}
tags {
"Description" = "Terraform State Locks"
"x:managed:by" = "Terraform"
"x:service:type" = "infrastructure"
}
}
//
// Terraform State Files
// Here we create a folder to store our Terraform state files
// https://www.terraform.io/docs/state/remote.html
resource "aws_s3_bucket_object" "terraform-state-files" {
provider = "aws.init.infrastructure"
count = "${var.infrastructure["use_tf_remote_state"]}"
bucket = "${aws_s3_bucket.site-infrastructure.id}"
acl = "private"
key = "terraform/"
// We use a different source on Windows vs. UNIX-like systems
source = "${var.client_os == "windows" ? "nul": "/dev/null"}"
tags {
"Description" = "Terraform State Files"
"x:managed:by" = "Terraform"
"x:service:type" = "infrastructure"
}
}
// Set up the Instance Role
data "aws_iam_policy_document" "instance_assume_role" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
resource "aws_iam_role" "instance_role" {
name = "instance-role"
path = "/"
assume_role_policy = "${data.aws_iam_policy_document.instance_assume_role.json}"
force_detach_policies = true
}
resource "aws_iam_instance_profile" "instance_role" {
name = "instance-role"
role = "${aws_iam_role.instance_role.name}"
}
// Configure what access instances get
data "aws_iam_policy_document" "instance_access" {
statement {
sid = ""
actions = [
"ec2:DescribeInstances",
"ec2:CreateTags",
"ec2:DescribeTags",
]
resources = ["*"]
}
statement {
sid = ""
actions = [
"ec2:DescribeVpcs",
"ec2:DescribeSecurityGroups",
"ec2:AuthorizeSecurityGroupEgress",
"ec2:AuthorizeSecurityGroupIngress",
"ec2:RevokeSecurityGroupEgress",
"ec2:RevokeSecurityGroupIngress",
]
resources = ["*"]
}
statement {
sid = ""
actions = ["autoscaling:*"]
resources = ["*"]
}
statement {
sid = ""
actions = ["s3:*"]
resources = [
"arn:aws:s3:::backups",
"arn:aws:s3:::backups/*"
]
}
}
resource "aws_iam_policy" "instance_access" {
name = "instance-access"
path = "/"
policy = "${data.aws_iam_policy_document.instance_access.json}"
}
resource "aws_iam_role_policy_attachment" "attach" {
role = "${aws_iam_role.instance_role.name}"
policy_arn = "${aws_iam_policy.instance_access.arn}"
}