-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.yml
More file actions
208 lines (207 loc) · 6.28 KB
/
template.yml
File metadata and controls
208 lines (207 loc) · 6.28 KB
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
Conditions:
IsDestinationRegion: !Equals
- !Ref 'ReplicaRegion'
- !Ref 'AWS::Region'
IsSourceRegion: !Equals
- !Ref 'SourceRegion'
- !Ref 'AWS::Region'
Description: Template to create Replicated Bucket
Metadata:
Author: https://github.com/johnpreston
Date: '2019-06-26T13:28:56.466145'
Outputs:
RoleArn:
Value: !If
- IsSourceRegion
- !GetAtt 'ReplicationRole.Arn'
- NoWhatWeNeedHere
Parameters:
BucketName:
AllowedPattern: '[a-z0-9-]+'
Type: String
ReplicaRegion:
AllowedValues:
- eu-north-1
- ap-south-1
- eu-west-3
- eu-west-2
- eu-west-1
- ap-northeast-2
- ap-northeast-1
- sa-east-1
- ca-central-1
- ap-southeast-1
- ap-southeast-2
- eu-central-1
- us-east-1
- us-east-2
- us-west-1
- us-west-2
Type: String
SourceRegion:
AllowedValues:
- eu-north-1
- ap-south-1
- eu-west-3
- eu-west-2
- eu-west-1
- ap-northeast-2
- ap-northeast-1
- sa-east-1
- ca-central-1
- ap-southeast-1
- ap-southeast-2
- eu-central-1
- us-east-1
- us-east-2
- us-west-1
- us-west-2
Type: String
Resources:
BucketEncryptionKey:
Properties:
KeyPolicy:
Statement:
- Action:
- kms:*
Effect: Allow
Principal:
AWS:
- !Sub 'arn:aws:iam::${AWS::AccountId}:root'
Resource:
- '*'
Sid: Allow administration of the key
Version: '2012-10-17'
Type: AWS::KMS::Key
BucketEncryptionKeyAlias:
Properties:
AliasName: !Sub 'alias/${AWS::Region}/${BucketName}'
TargetKeyId: !GetAtt 'BucketEncryptionKey.Arn'
Type: AWS::KMS::Alias
ReplicaBucketPolicy:
Condition: IsDestinationRegion
Properties:
Bucket: !Ref 'ReplicatedBucket'
PolicyDocument:
Id: S3ReplicationPolicy
Statement:
- Action:
- s3:GetBucketVersioning
- s3:PutBucketVersioning
- s3:ReplicateObject
- s3:ReplicateDelete
Effect: Allow
Principal:
AWS: !Sub 'arn:aws:iam::${AWS::AccountId}:root'
Resource:
- !Sub 'arn:aws:s3:::${ReplicatedBucket}'
- !Sub 'arn:aws:s3:::${ReplicatedBucket}/*'
Sid: S3ReplicationPolicy
Version: '2008-10-17'
Type: AWS::S3::BucketPolicy
ReplicatedBucket:
DependsOn:
- BucketEncryptionKey
- BucketEncryptionKeyAlias
Properties:
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
KMSMasterKeyID: !Ref 'BucketEncryptionKey'
SSEAlgorithm: aws:kms
BucketName: !Sub '${BucketName}-${AWS::Region}'
LifecycleConfiguration:
Rules:
- AbortIncompleteMultipartUpload:
DaysAfterInitiation: 3
NoncurrentVersionExpirationInDays: 1
Status: Enabled
Transitions:
- StorageClass: GLACIER
TransitionInDays: !If
- IsSourceRegion
- 31
- 14
ReplicationConfiguration: !If
- IsSourceRegion
- Role: !GetAtt 'ReplicationRole.Arn'
Rules:
- Destination:
Bucket: !Sub 'arn:${AWS::Partition}:s3:::${BucketName}-${ReplicaRegion}'
EncryptionConfiguration:
ReplicaKmsKeyID:
Fn::Transform:
- Name: cfnmacro-kmskey
Parameters:
KeyRegion: !Ref ReplicaRegion
KeyAlias: !Sub 'alias/${ReplicaRegion}/${BucketName}'
Prefix: ''
SourceSelectionCriteria:
SseKmsEncryptedObjects:
Status: Enabled
Status: Enabled
- !Ref 'AWS::NoValue'
VersioningConfiguration:
Status: Enabled
Type: AWS::S3::Bucket
ReplicationRole:
Condition: IsSourceRegion
Properties:
AssumeRolePolicyDocument:
Statement:
- Action:
- sts:AssumeRole
Effect: Allow
Principal:
Service:
- !Sub 's3.${AWS::URLSuffix}'
Version: '2012-10-17'
Policies:
- PolicyDocument:
Statement:
- Action:
- kms:Decrypt
Condition:
StringLike:
kms:ViaService: !Sub 's3.${SourceRegion}.${AWS::URLSuffix}'
Effect: Allow
Resource:
- !GetAtt 'BucketEncryptionKey.Arn'
- Action:
- kms:Encrypt
Condition:
StringLike:
kms:ViaService: !Sub 's3.${ReplicaRegion}.${AWS::URLSuffix}'
Effect: Allow
Resource:
- Fn::Transform:
- Name: cfnmacro-kmskey
Parameters:
KeyRegion: !Ref ReplicaRegion
KeyAlias: !Sub 'alias/${ReplicaRegion}/${BucketName}'
Version: '2012-10-17'
PolicyName: !Sub 'KMSAccess'
- PolicyDocument:
Statement:
- Action:
- s3:ListBucket
- s3:GetReplicationConfiguration
- s3:GetObjectVersionForReplication
- s3:GetObjectVersionAcl
- s3:GetObjectVersionTagging
Effect: Allow
Resource:
- !Sub 'arn:${AWS::Partition}:s3:::${BucketName}-${SourceRegion}'
- !Sub 'arn:${AWS::Partition}:s3:::${BucketName}-${SourceRegion}/*'
- Action:
- s3:ReplicateObject
- s3:ReplicateDelete
- s3:ReplicateTags
- s3:GetObjectVersionTagging
Effect: Allow
Resource:
- !Sub 'arn:${AWS::Partition}:s3:::${BucketName}-${ReplicaRegion}'
- !Sub 'arn:${AWS::Partition}:s3:::${BucketName}-${ReplicaRegion}/*'
Version: '2012-10-17'
PolicyName: S3ReplicationAccess
Type: AWS::IAM::Role