-
Notifications
You must be signed in to change notification settings - Fork 8
/
cfn-s3-x-region-replication_final.yml
150 lines (149 loc) · 4.66 KB
/
cfn-s3-x-region-replication_final.yml
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
---
Parameters:
RepRegion:
Type: String
AllowedValues:
- us-east-1
- us-east-2
- us-west-1
- us-west-2
- ca-central-1
- ap-south-1
- ap-northeast-2
- ap-southeast-1
- ap-southeast-2
- ap-northeast-1
- eu-central-1
- eu-west-1
- eu-west-2
- sa-east-1
ReplicationBucketName:
Type: String
OriginalBucketName:
Type: String
Resources:
LambdaExecutionRole:
Properties:
AssumeRolePolicyDocument:
Statement:
-
Action:
- "sts:AssumeRole"
Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Version: "2012-10-17"
Path: /
Policies:
-
PolicyDocument:
Statement:
-
Action:
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
Effect: Allow
Resource: "arn:aws:logs:*:*:*"
-
Action:
- "s3:*"
Effect: Allow
Resource: "*"
Version: "2012-10-17"
PolicyName: root
Type: "AWS::IAM::Role"
OriginalBucket:
DependsOn: TriggerLambda
Properties:
BucketName: !Ref OriginalBucketName
ReplicationConfiguration:
Role: !GetAtt S3Role.Arn
Rules:
-
Destination:
Bucket:
!Join
- ""
-
- "arn:aws:s3:::"
- !Ref ReplicationBucketName
StorageClass: STANDARD
Id: MyRule1
Prefix: ""
Status: Enabled
VersioningConfiguration:
Status: Enabled
Type: "AWS::S3::Bucket"
ReplicationBucket:
Properties:
Code:
ZipFile:
!Sub |
var aws = require('aws-sdk');
var response = require('cfn-response');
exports.handler = function(event, context, callback){
var s3 = new aws.S3({region: event.ResourceProperties.DestBucketRegion});
if (event.RequestType == 'Create' || event.RequestType == 'Update'){
var bucketParams = {
Bucket: event.ResourceProperties.DestBucketName,
};
s3.createBucket(bucketParams, function(err, data) {
if (err){
console.log(err, err.stack)
response.send(event, context, response.FAILED, err)
}
else {
console.log(data)
var versioningParams = {
Bucket: event.ResourceProperties.DestBucketName,
VersioningConfiguration: {
Status: 'Enabled'
}
};
s3.putBucketVersioning(versioningParams, function(err, data) {
if (err) {
console.log(err, err.stack);
}
else {
console.log(data)
response.send(event, context, response.SUCCESS, {}, event.destBucketName);
callback(null,'Bucket created!');
}
});
}
});
}
else if (event.RequestType == 'Delete'){
console.log('Please delete the ' + event.PhysicalResourceId + ' bucket.')
response.send(event, context, response.SUCCESS, {}, event.PhysicalResourceId);
callback(null);
}
};
Handler: index.handler
Role: !GetAtt LambdaExecutionRole.Arn
Runtime: nodejs8.10
Timeout: 30
Type: "AWS::Lambda::Function"
TriggerLambda:
Type: Custom::LambdaTrig
Properties:
ServiceToken: !GetAtt ReplicationBucket.Arn
DestBucketName: !Ref ReplicationBucketName
DestBucketRegion: !Ref RepRegion
S3Role:
Properties:
AssumeRolePolicyDocument:
Statement:
-
Action: "sts:AssumeRole"
Effect: Allow
Principal:
Service:
- s3.amazonaws.com
Version: "2012-10-17"
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonS3FullAccess
Path: /
Type: "AWS::IAM::Role"