Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(iam): allow intrinsic functions in deletion policy #28834

Merged
merged 11 commits into from
Jan 24, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"AWSTemplateFormatVersion": "2010-09-09",
"Transform": "AWS::LanguageExtensions",
"Parameters": {
"Stage": {
"Type": "String",
"AllowedValues": [
"Prod",
"Staging",
"Dev"
]
}
},
"Conditions": {
"IsProd": {
"Fn::Equals": [
{
"Ref": "Stage"
},
"Prod"
]
}
},
"Resources": {
"Table": {
ConnorRobertson marked this conversation as resolved.
Show resolved Hide resolved
"Type": "AWS::DynamoDB::Table",
"Properties": {
"KeySchema": [{
"AttributeName": "primaryKey",
"KeyType": "HASH"
}],
"AttributeDefinitions": [{
"AttributeName": "primaryKey",
"AttributeType": "S"
}]
},
"DeletionPolicy": {
"Fn::If": [
"IsProd",
"Retain",
"Delete"
]
},
"UpdateReplacePolicy": {
"Fn::If": [
"IsProd",
"Retain",
"Delete"
]
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,14 @@ describe('CDK Include', () => {
);
});

test('can ingest a template with intrinsic functions used in deletion policy', () => {
includeTestTemplate(stack, 'intrinsic-deletion-policy.json');

Template.fromStack(stack).templateMatches(
loadTestFileToJsObject('intrinsic-deletion-policy.json'),
);
});

test('can ingest a JSON template with string-form Fn::GetAtt, and output it unchanged', () => {
includeTestTemplate(stack, 'get-att-string-form.json');

Expand Down
13 changes: 10 additions & 3 deletions packages/aws-cdk-lib/core/lib/helpers-internal/cfn-parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,14 +466,21 @@ export class CfnParser {
}

private parseDeletionPolicy(policy: any): CfnDeletionPolicy | undefined {
if (policy === undefined || policy === null) {
return undefined;
}
const isIntrinsic = this.looksLikeCfnIntrinsic(policy) !== undefined;
ConnorRobertson marked this conversation as resolved.
Show resolved Hide resolved
switch (policy) {
case null: return undefined;
case undefined: return undefined;
case 'Delete': return CfnDeletionPolicy.DELETE;
case 'Retain': return CfnDeletionPolicy.RETAIN;
case 'Snapshot': return CfnDeletionPolicy.SNAPSHOT;
case 'RetainExceptOnCreate': return CfnDeletionPolicy.RETAIN_EXCEPT_ON_CREATE;
default: throw new Error(`Unrecognized DeletionPolicy '${policy}'`);
default: if (isIntrinsic) {
policy = this.parseValue(policy);
return policy;
} else {
throw new Error(`Unrecognized DeletionPolicy '${policy}'`);
}
}
}

Expand Down
Loading