forked from Scout24/aws-cf-verified-ssl-certificate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssl-certificate.template.yaml
258 lines (247 loc) · 7.42 KB
/
ssl-certificate.template.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
Description: SES Domain Verification
Parameters:
domain:
Type: String
Description: DNS domain to create a SES email for
hostedZoneName:
Type: String
Description: Route53 hosted zone name
lambdaCodeS3Bucket:
Type: String
Description: S3 bucket hosting the lambda function code
Default: "is24-infrastructure-public"
lambdaCodeS3Key:
Type: String
Description: S3 key pointing to lambda function code
Default: "cloudformation/verified-ssl-certificate/lambda_functions.zip"
Resources:
#
# lambda function to create a SES domain idendity
# and return verification token for TXT record
#
sesDomainCustomResourceRole:
Type: AWS::IAM::Role
Properties:
Policies:
- PolicyName: SES
PolicyDocument:
Version: 2012-10-17
Statement:
- Action:
- ses:VerifyDomainIdentity
- ses:DeleteIdentity
Resource:
- "*"
Effect: Allow
- PolicyName: cloudwatchLogsWriteAccess
PolicyDocument:
Version: 2012-10-17
Statement:
- Action:
- logs:Describe*
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: "*"
Effect: Allow
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Action: sts:AssumeRole
Principal:
Service: lambda.amazonaws.com
Effect: Allow
sesDomainCustomResourceFunction:
Type: AWS::Lambda::Function
Properties:
Code:
S3Bucket: !Ref lambdaCodeS3Bucket
S3Key: !Ref lambdaCodeS3Key
Runtime: python2.7
Handler: ses_domain_identity.lambda_handler
Role: !GetAtt sesDomainCustomResourceRole.Arn
Timeout: 30
#
# lambda function to wait for SES domain verification finished
# and create a SES ruleset to forward admin-email-address to SNS
#
sesEmailAddressCustomResourceRole:
Type: AWS::IAM::Role
Properties:
Policies:
- PolicyName: SES
PolicyDocument:
Version: 2012-10-17
Statement:
- Action:
- ses:CreateReceiptRule
- ses:CreateReceiptRuleSet
- ses:DeleteReceiptRule
- ses:DeleteReceiptRuleSet
- ses:DescribeActiveReceiptRuleSet
- ses:GetIdentityVerificationAttributes
- ses:ReorderReceiptRuleSet
- ses:SetActiveReceiptRuleSet
Resource:
- "*"
Effect: Allow
- PolicyName: cloudwatchLogsWriteAccess
PolicyDocument:
Version: 2012-10-17
Statement:
- Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:Describe*
- logs:PutLogEvents
Resource: "*"
Effect: Allow
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Action: sts:AssumeRole
Principal:
Service: lambda.amazonaws.com
Effect: Allow
sesEmailAddressCustomResourceFunction:
Type: AWS::Lambda::Function
Properties:
Code:
S3Bucket: !Ref lambdaCodeS3Bucket
S3Key: !Ref lambdaCodeS3Key
Runtime: python2.7
Handler: ses_wait_for_verification_and_create_rule_set.lambda_handler
Role: !GetAtt sesEmailAddressCustomResourceRole.Arn
Timeout: 300
#
# lambda function to receice certificate verification SNS message,
# open verfication form and send form data
#
certificateVerificationLambdaRole:
Type: AWS::IAM::Role
Properties:
Policies:
- PolicyName: SES
PolicyDocument:
Version: 2012-10-17
Statement:
- Action:
- ses:GetIdentityVerificationAttributes
Resource:
- "*"
Effect: Allow
- PolicyName: cloudwatchLogsWriteAccess
PolicyDocument:
Version: 2012-10-17
Statement:
- Action:
- logs:Describe*
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: "*"
Effect: Allow
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Action: sts:AssumeRole
Principal:
Service: lambda.amazonaws.com
Effect: Allow
- Action: sts:AssumeRole
Principal:
Service: ses.amazonaws.com
Effect: Allow
certificateVerificationLambdaFunction:
Type: AWS::Lambda::Function
Properties:
Code:
S3Bucket: !Ref lambdaCodeS3Bucket
S3Key: !Ref lambdaCodeS3Key
Runtime: python2.7
Handler: process_cert_manager_mail.lambda_handler
Role: !GetAtt certificateVerificationLambdaRole.Arn
Timeout: 300
#
# custom resource to create SES domain identitiy
# and return verification token
#
sesDomain:
Type: Custom::sesDomain
Properties:
ServiceToken: !GetAtt sesDomainCustomResourceFunction.Arn
Region: !Ref AWS::Region
Domain: !Ref domain
#
# DNS TXT record to allow AWS to verify SES domain identity
#
sesDomainVerificationRecord:
Type: AWS::Route53::RecordSet
Properties:
Comment: DNS entry for SES domain verification
HostedZoneName: !Ref hostedZoneName
Name: !Sub _amazonses.${domain}.
Type: TXT
TTL: 180
ResourceRecords:
- !Sub "\"${sesDomain.VerificationToken}\""
#
# DNS MX record to let AWS SES retrieve emails for domain wanted
#
sesDomainMXRecord:
Type: AWS::Route53::RecordSet
Properties:
Comment: MX record for domain
HostedZoneName: !Ref hostedZoneName
Name: !Sub ${domain}.
Type: MX
TTL: 900
ResourceRecords:
- !Sub "10 inbound-smtp.${AWS::Region}.amazonaws.com."
#
# SNS topic to forward incoming admin emails to.
# Email verification lambda function is subscribed to this topic.
#
certificateVerificationSNSTopic:
Type: AWS::SNS::Topic
Properties:
Subscription:
- Endpoint: !GetAtt certificateVerificationLambdaFunction.Arn
Protocol: lambda
certifactionVerificationSNSLambdaPermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !GetAtt certificateVerificationLambdaFunction.Arn
Action: lambda:InvokeFunction
Principal: sns.amazonaws.com
SourceArn: !Ref certificateVerificationSNSTopic
# Waits for domain to be validated by SES
# Creates rule to forward admin@domain.com to SNS topic
sesEmailAddress:
Type: Custom::sesEmailAddress
Properties:
ServiceToken: !GetAtt sesEmailAddressCustomResourceFunction.Arn
Region: !Ref AWS::Region
Domain: !Ref domain
VerifyRecord: !Ref sesDomainVerificationRecord
CertificateVerificationSNSTopicArn: !Ref certificateVerificationSNSTopic
DependsOn: sesDomainVerificationRecord
# the SSL certificate
sslCertificate:
Type: AWS::CertificateManager::Certificate
Properties:
DomainName: !Ref domain
SubjectAlternativeNames:
- !Sub "*.${domain}"
DomainValidationOptions:
- DomainName: !Ref domain
ValidationDomain: !GetAtt sesEmailAddress.Domain
- DomainName: !Sub "*.${domain}"
ValidationDomain: !GetAtt sesEmailAddress.Domain
Outputs:
sslCertificateArn:
Description: SSL certificate ARN
Value: !Ref sslCertificate
Arn:
Description: SSL certificate ARN
Value: !Ref sslCertificate