Skip to content

Commit 27141b2

Browse files
committed
feat: add removal policy for domains and ensure default for removal policy is always DESTROY for domains and email addresses to keep backward compatibility
1 parent 3013ae9 commit 27141b2

8 files changed

+837
-86
lines changed

API.md

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ new VerifySesDomain(this, 'SesDomainVerification', {
6060
* `addDkimRecord` Whether to automatically add DKIM records to the hosted zone of your domain. This only works if your domain is managed by Route53. Otherwise disable it. Default: `true`.
6161
* `notificationTopic` An SNS topic where bounces, complaints or delivery notifications can be sent to. If none is provided, a new topic will be created and used for provided notification types.
6262
* `notificationTypes` Select for which notification types you want to configure a topic. Default: `[Bounce, Complaint]`.
63+
* `removalPolicy` Set a `RemovalPolicy` if you want to retain the resources. Default: `DESTROY`
6364

6465
### Verify an Email Address
6566

@@ -73,6 +74,7 @@ new VerifySesEmailAddress(this, 'SesEmailVerification', {
7374

7475
* `emailAddress` The email address to be verified, e.g. `hello@example.org`.
7576
* `region` An optional AWS region to validate the email address. Default: The custom resource will be created in the stack region.
77+
* `removalPolicy` Set a `RemovalPolicy` if you want to retain the resources. Default: `DESTROY`
7678

7779
## Contributing
7880

src/verify-ses-domain.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CfnOutput, Fn } from 'aws-cdk-lib';
1+
import { CfnOutput, Fn, RemovalPolicy } from 'aws-cdk-lib';
22
import { CnameRecord, HostedZone, IHostedZone, MxRecord, TxtRecord } from 'aws-cdk-lib/aws-route53';
33
import { ITopic, Topic } from 'aws-cdk-lib/aws-sns';
44
import { AwsCustomResource, PhysicalResourceId } from 'aws-cdk-lib/custom-resources';
@@ -56,6 +56,12 @@ export interface IVerifySesDomainProps {
5656
* @default [Bounce, Complaint]
5757
*/
5858
readonly notificationTypes?: NotificationType[];
59+
/**
60+
* Whether to DESTROY or RETAIN the domain on removal.
61+
*
62+
* @default DESTROY
63+
*/
64+
readonly removalPolicy?: RemovalPolicy;
5965
}
6066

6167
/**
@@ -87,9 +93,10 @@ export class VerifySesDomain extends Construct {
8793
addTxtRecord,
8894
addMxRecord,
8995
addDkimRecords,
96+
removalPolicy,
9097
} = props;
9198

92-
const verifyDomainIdentity = this.verifyDomainIdentity(domainName);
99+
const verifyDomainIdentity = this.verifyDomainIdentity(domainName, removalPolicy);
93100
this.notificationTopic = this.createTopicOrUseExisting(domainName, verifyDomainIdentity, notificationTopic);
94101
this.addTopicToDomainIdentity(domainName, this.notificationTopic, notificationTypes);
95102

@@ -113,7 +120,7 @@ export class VerifySesDomain extends Construct {
113120
}
114121
}
115122

116-
private verifyDomainIdentity(domainName: string): AwsCustomResource {
123+
private verifyDomainIdentity(domainName: string, removalPolicy?: RemovalPolicy): AwsCustomResource {
117124
return new AwsCustomResource(this, 'VerifyDomainIdentity', {
118125
onCreate: {
119126
service: 'SES',
@@ -131,7 +138,7 @@ export class VerifySesDomain extends Construct {
131138
},
132139
physicalResourceId: PhysicalResourceId.fromResponse('VerificationToken'),
133140
},
134-
onDelete: {
141+
onDelete: removalPolicy === RemovalPolicy.RETAIN ? undefined : {
135142
service: 'SES',
136143
action: 'deleteIdentity',
137144
parameters: {

src/verify-ses-email-address.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export interface IVerifySesEmailAddressProps {
1919
/**
2020
* Whether to DESTROY or RETAIN the email address on removal.
2121
*
22-
* @default RETAIN
22+
* @default DESTROY
2323
*/
2424
readonly removalPolicy?: RemovalPolicy;
2525
}
@@ -52,14 +52,14 @@ export class VerifySesEmailAddress extends Construct {
5252
physicalResourceId: PhysicalResourceId.of('verify-' + emailAddress),
5353
region,
5454
},
55-
onDelete: RemovalPolicy.DESTROY == removalPolicy ? {
55+
onDelete: removalPolicy === RemovalPolicy.RETAIN ? undefined : {
5656
service: 'SES',
5757
action: 'deleteIdentity',
5858
parameters: {
5959
Identity: emailAddress,
6060
},
6161
region,
62-
} : undefined,
62+
},
6363
policy: generateSesPolicyForCustomResource('VerifyEmailIdentity', 'DeleteIdentity'),
6464
});
6565
}

0 commit comments

Comments
 (0)