|
2 | 2 | import type { Construct } from 'constructs'
|
3 | 3 | import type { StackProps } from 'aws-cdk-lib'
|
4 | 4 | import {
|
| 5 | + CustomResource, |
5 | 6 | Duration,
|
6 | 7 | Fn,
|
7 | 8 | CfnOutput as Output,
|
@@ -302,18 +303,49 @@ export class StacksCloud extends Stack {
|
302 | 303 | this.redirectZones.push(hostedZone)
|
303 | 304 | })
|
304 | 305 |
|
305 |
| - new custom_resources.AwsCustomResource(this, 'VerifyDomainIdentity', { |
| 306 | + const domainIdentity = new custom_resources.AwsCustomResource(this, 'DomainIdentity', { |
306 | 307 | onCreate: {
|
307 | 308 | service: 'SES',
|
308 | 309 | action: 'verifyDomainIdentity',
|
309 | 310 | parameters: {
|
310 | 311 | Domain: this.domain,
|
311 | 312 | },
|
312 |
| - physicalResourceId: custom_resources.PhysicalResourceId.of('VerifyDomainIdentity'), |
| 313 | + physicalResourceId: { id: 'DomainIdentityCreation' }, |
313 | 314 | },
|
314 | 315 | policy: custom_resources.AwsCustomResourcePolicy.fromSdkCalls({ resources: custom_resources.AwsCustomResourcePolicy.ANY_RESOURCE }),
|
315 | 316 | })
|
316 | 317 |
|
| 318 | + // give ourselves permission to verify the domain |
| 319 | + domainIdentity.grantPrincipal.addToPrincipalPolicy(new iam.PolicyStatement({ |
| 320 | + actions: ['ses:VerifyDomainIdentity'], |
| 321 | + resources: ['*'], |
| 322 | + effect: iam.Effect.ALLOW, |
| 323 | + })) |
| 324 | + |
| 325 | + // Enable DKIM on the domain identity |
| 326 | + const dkimAttributes = new custom_resources.AwsCustomResource(this, 'DkimAttributes', { |
| 327 | + onCreate: { |
| 328 | + service: 'SES', |
| 329 | + action: 'verifyDomainDkim', |
| 330 | + parameters: { |
| 331 | + Domain: this.domain, |
| 332 | + }, |
| 333 | + physicalResourceId: { id: 'DkimAttributesCreation' }, |
| 334 | + }, |
| 335 | + policy: custom_resources.AwsCustomResourcePolicy.fromSdkCalls({ resources: custom_resources.AwsCustomResourcePolicy.ANY_RESOURCE }), |
| 336 | + }) |
| 337 | + |
| 338 | + // Add the DKIM CNAME records to the DNS configuration |
| 339 | + const dkimTokens = dkimAttributes.getResponseField('DkimTokens') |
| 340 | + for (let i = 0; i < dkimTokens.length; i++) { |
| 341 | + new route53.CnameRecord(this, `DkimRecord${i}`, { |
| 342 | + zone: this.zone, |
| 343 | + recordName: `${dkimTokens[i]}._domainkey.${this.domain}`, |
| 344 | + domainName: `${dkimTokens[i]}.dkim.amazonses.com`, |
| 345 | + ttl: Duration.hours(1), |
| 346 | + }) |
| 347 | + } |
| 348 | + |
317 | 349 | new route53.MxRecord(this, 'MxRecord', {
|
318 | 350 | zone: this.zone,
|
319 | 351 | recordName: this.domain,
|
|
0 commit comments