-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathterraform-bootstrap.yaml
241 lines (213 loc) · 7.12 KB
/
terraform-bootstrap.yaml
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
---
AWSTemplateFormatVersion: "2010-09-09"
Description: "Bootstrap the Terraform S3 Backend"
#
# This CloudFormation stack template initializes all of the resources necessary
# to utilize the Terraform S3 backend, see;
# https://www.terraform.io/language/settings/backends/s3
#
# These resources are meant to reside in what the Terraform documentation refers
# to as an "administrative AWS account". See;
# https://developer.hashicorp.com/terraform/language/settings/backends/s3#multi-account-aws-architecture
#
# Note that should this CloudFormation stack be deleted the S3 bucket and
# DynamoDB table it defines will be retained for safety's sake.
#
Parameters:
#
# If the `S3BucketName` parameter is left empty then a random name will be
# generated that uses this CloudFormation stack's name as a prefix.
#
S3BucketName:
Type: String
Description: >-
Name for the S3 bucket created to store Terraform state.
Default: ""
S3StatePrefix:
Type: String
Description: >-
Prefix within the S3 bucket to store Terraform state.
Default: "terraform-state"
DynamoDbTableName:
Type: String
Description: >-
Name for the DynamocDB table created to lock Terraform state.
Default: "terraform-locking"
PolicyName:
Type: String
Description: >-
Nmae for the IAM policy that enables access to backend resources.
Default: "Terraform-S3-Backend"
#
# To ensure that the S3 bucket and DynamoDB table are discoverable by scripts
# and other forms of automation we put their names into well known locations in
# Parameter Store.
#
ParameterPrefix:
Type: String
Description: >-
Prefix (without a leading '/'') for Parameters Store values that can be used
to discover the resources in this stack for the purpose of HCL generation, etc.
Default: "terraform"
Rules:
StatePrefixConstraint:
Assertions:
- Assert: !Not [ !Equals [ !Ref S3StatePrefix, "" ] ]
AssertDescription: >-
The S3 prefix for state objects may not be blank.
LockingTableNameConstraint:
Assertions:
- Assert: !Not [ !Equals [ !Ref DynamoDbTableName, "" ] ]
AssertDescription: >-
The name for the DynamoDB locking table may not be blank.
Conditions:
HasSpecifiedS3BucketName: !Not [ !Equals [ !Ref S3BucketName, "" ] ]
Resources:
#
# Stores the Terraform state. This bucket may be used as the backend for more
# than one remote state instance.
#
Bucket:
Type: AWS::S3::Bucket
UpdateReplacePolicy: Retain
DeletionPolicy: Retain
Properties:
BucketName: !If [ HasSpecifiedS3BucketName, !Ref S3BucketName, !Ref "AWS::NoValue" ]
AccessControl: Private
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
VersioningConfiguration:
Status: Enabled
Tags:
- Key: application
Value: "Terraform S3 Backend"
#
# The backend can use a DynamoDB table for locking to prevent races between
# different instances of Terraform.
#
# The layout of this table is buried in the S3 backend documentation, see;
# https://developer.hashicorp.com/terraform/language/settings/backends/s3#dynamodb-state-locking
#
# TLDR; "The table must have a partition key named LockID with type of String."
#
DynamoTable:
Type: AWS::DynamoDB::Table
UpdateReplacePolicy: Retain
DeletionPolicy: Retain
Properties:
TableName: !Ref DynamoDbTableName
PointInTimeRecoverySpecification:
PointInTimeRecoveryEnabled: true
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: LockID
AttributeType: S
KeySchema:
- AttributeName: LockID
KeyType: HASH
SSESpecification:
SSEEnabled: true
Tags:
- Key: application
Value: "Terraform S3 Backend"
#
# Attach this policy to an IAM user, group, or role to enable access to the S3
# backend, see;
# https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_manage-attach-detach.html
#
Policy:
Type: AWS::IAM::ManagedPolicy
Properties:
Description: "Terraform S3 backend access."
ManagedPolicyName: !Ref PolicyName
PolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: "Bucket"
Effect: Allow
Action:
- "s3:ListBucket"
Resource: !GetAtt Bucket.Arn
- Sid: "StateAccess"
Effect: Allow
Action:
- "s3:GetObject"
- "s3:PutObject"
- "s3:DeleteObject"
Resource: !Sub "${Bucket.Arn}/${S3StatePrefix}/*"
- Sid: "Locking"
Effect: Allow
Action:
- "dynamodb:GetItem"
- "dynamodb:PutItem"
- "dynamodb:DeleteItem"
Resource: !GetAtt DynamoTable.Arn
- Sid: "Parameters"
Effect: Allow
Action:
- "ssm:DescribeParameters"
- "ssm:GetParameter"
- "ssm:GetParameterHistory"
- "ssm:GetParameters"
- "ssm:GetParametersByPath"
Resource: !Sub "arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/${ParameterPrefix}/*"
# Side note:
# Parameter Store parameters have a different tag format than most
# other AWS resources created via CloudFormation. See;
# https://stackoverflow.com/questions/69718237/property-validation-failure-value-of-property-tags-does-not-match-type-map
S3BucketParameter:
Type: AWS::SSM::Parameter
Properties:
Description: >-
Bucket used for Terraform S3 backend deployment(s).
Name: !Sub "/${ParameterPrefix}/s3-backend-bucket"
Tags:
application: "Terraform S3 Backend"
Tier: Standard
Type: "String"
Value: !Ref Bucket
S3KeyPrefixParameter:
Type: AWS::SSM::Parameter
Properties:
Description: >-
Bucket used for Terraform S3 backend deployment(s).
Name: !Sub "/${ParameterPrefix}/s3-backend-prefix"
Tags:
application: "Terraform S3 Backend"
Tier: Standard
Type: "String"
Value: !Ref S3StatePrefix
DynamoTableParameter:
Type: AWS::SSM::Parameter
Properties:
Description: >-
DynamoDB locking table used for Terraform S3 backend deployment(s).
Name: !Sub "/${ParameterPrefix}/s3-backend-lock-table"
Tags:
application: "Terraform S3 Backend"
Tier: Standard
Type: "String"
Value: !Ref DynamoTable
Outputs:
Bucket:
Description: Terrafom state S3 bucket
Value: !Ref Bucket
Export:
Name: !Sub "${AWS::StackName}:bucket-name"
LockTable:
Description: Terraform state lock table
Value: !GetAtt DynamoTable.Arn
Export:
Name: !Sub "${AWS::StackName}:dynamo-table-arn"
Policy:
Description: IAM policy used to access Terraform state
Value: !Ref Policy
Export:
Name: !Sub "${AWS::StackName}:policy-arn"