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

RFC 0011: Allow Intrinsic Functions and Pseudo-Parameter References in DeletionPolicy and UpdateReplacePolicy RFC #21

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
262 changes: 262 additions & 0 deletions RFCs/0011-DeletionPolicy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
# Allow Intrinsic Functions and Pseudo-Parameter References in `DeletionPolicy` and `UpdateReplacePolicy` Resource attributes

* **Original Author(s):**: @mingujo
* **Tracking Issue**: [Tracking Issue](https://github.com/aws-cloudformation/cfn-language-discussion/issues/11)

# Summary

Use CloudFormation [intrinsic functions](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference.html) and [Pseudo-Parameter references](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/pseudo-parameter-reference.html) to dynamically determine a value for DeletionPolicy and UpdateReplacePolicy Resource attributes

# Examples

### Use [Ref](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-ref.html)
```
jlhood marked this conversation as resolved.
Show resolved Hide resolved
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Parameters": {
"DeletionPolicyParam": {
"Type": "String",
"AllowedValues": ["Delete", "Retain", "Snapshot"],
"Default": "Delete"
},
"UpdateReplacePolicyParam": {
"Type": "String",
"AllowedValues": ["Delete", "Retain", "Snapshot"],
"Default": "Delete"
}
},
"Resources": {
"Table": {
"Type": "AWS::DynamoDB::Table",
"Properties": {
"KeySchema": [
{
"AttributeName" : "primaryKey",
"KeyType" : HASH
}
]
},
"DeletionPolicy": { "Ref": "DeletionPolicyParameter" },
"UpdateReplacePolicy": { "Ref": "UpdateReplacePolicyParam" }
}
}
}
```

### Use [Conditions](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/conditions-section-structure.html) and [Fn::If](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-condition.html) based on Parameters
jlhood marked this conversation as resolved.
Show resolved Hide resolved
```
jlhood marked this conversation as resolved.
Show resolved Hide resolved
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Parameters": {
"Stage": {
"Type": "String",
"AllowedValues": ["Prod", "Staging", "Dev"]
}
},
"Conditions": {
"IsProd" : {"Fn::Equals" : [{"Ref" : "Stage"}, "Prod"]},
},
"Resources": {
"Table": {
"Type": "AWS::DynamoDB::Table",
"Properties": {
"KeySchema": [
{
"AttributeName" : "primaryKey",
"KeyType" : HASH
}
]
},
"DeletionPolicy": {
"Fn::If": [
"IsProd",
"Retain",
"Delete"
]
},
"UpdateReplacePolicy": {
"Fn::If": [
"IsProd",
"Retain",
"Delete"
]
}
}
}
}
```

### Use [Fn::FindInMap](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-findinmap.html) and `AWS::Region` Pseudo-Parameter
```
jlhood marked this conversation as resolved.
Show resolved Hide resolved
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Parameters": {
"Stage": {
"Type": "String",
"AllowedValues": ["Prod", "Staging", "Dev"]
}
},
"Mappings" : {
"RegionMap" : {
"us-east-1" : {
"DeletionPolicy": "Retain",
"UpdateReplacePolicy": "Retain"
},
"us-west-1" : {
"DeletionPolicy": "Delete",
"UpdateReplacePolicy": "Delete"
},
"eu-west-1" : {
"DeletionPolicy": "Retain",
"UpdateReplacePolicy": "Delete"
},
"ap-southeast-1" : {
"DeletionPolicy": "Delete",
"UpdateReplacePolicy": "Delete"
}
}
},
"Resources": {
"Bucket": {
"Type": "AWS::S3::Bucket",
"DeletionPolicy": {
"Fn::FindInMap" : [
"RegionMap",
{ "Ref" : "AWS::Region" },
"DeletionPolicy"
]
},
"UpdateReplacePolicy": {
"Fn::FindInMap" : [
"RegionMap",
{ "Ref" : "AWS::Region" },
"UpdateReplacePolicy"
]
}
}
}
}
```

# Motivation

CloudFormation customers expect to use conditions and intrinsic functions as a value for a `DeletionPolicy` and `UpdateReplacePolicy` Resource attributes as they can do elsewhere in the template.
Main ask from our customers was to allow them to vary DeletionPolicy and UpdateReplacePolicy values based on development stage, which tends to be passed in as a parameter, e.g., allow resource deletion in Dev stages but not Prod. This RFC proposes to support not only such use case but also other intrinsic function usages and Pseudo-parameter references.

* References
* https://github.com/aws-cloudformation/cloudformation-coverage-roadmap/issues/162 (148 thumbs up)
jlhood marked this conversation as resolved.
Show resolved Hide resolved
* https://github.com/aws/aws-cli/issues/3825
jlhood marked this conversation as resolved.
Show resolved Hide resolved

# Details
* Following intrinsic functions will be supported.
jlhood marked this conversation as resolved.
Show resolved Hide resolved
* [Ref](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-ref.html)
* [Fn::FindInMap](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-findinmap.html)
* [Fn::If](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-conditions.html#intrinsic-function-reference-conditions-if)


* Following Pseudo-Parameters references will be supported
* `AWS::AccountId`, `AWS::Region`, `AWS::Partition`

# Limitation

Does not support expressions containing references to *[Resource properties](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html)* or *certain pseudo parameters*
* Unsupported Pseudo-Parameter References
* `AWS::StackId`, `AWS::StackName`, `AWS::NotificationArns`, `AWS::URLSuffix`, `AWS::NoValue`
jlhood marked this conversation as resolved.
Show resolved Hide resolved


# FAQ
* How about other Resource attributes such as CreationPolicy, DependsOn, Metadata, UpdatePolicy?
* They are out of scope of this RFC.


* Will this be supported by CFN Lint (linter check for CFN template)?
* The AWS CloudFormation Linter (cfn-lint) will be updated to validate function and Pseudo-parameter usages under Resource attributes.

# Appendix
## More examples
### Use [Fn::FindInMap](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-findinmap.html) and `AWS::Partition`
```
jlhood marked this conversation as resolved.
Show resolved Hide resolved
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Parameters": {
"Stage": {
"Type": "String",
"AllowedValues": ["Prod", "Staging", "Dev"]
}
},
"Mappings" : {
"PartitionMap" : {
"aws" : {
"DeletionPolicy": "Retain",
"UpdateReplacePolicy": "Retain"
},
"aws-cn" : {
"DeletionPolicy": "Delete",
"UpdateReplacePolicy": "Delete"
},
"aws-us-gov" : {
"DeletionPolicy": "Retain",
"UpdateReplacePolicy": "Delete"
}
}
},
"Resources": {
"Table": {
"Type": "AWS::DynamoDB::Table",
"Properties": {
"KeySchema": [
{
"AttributeName" : "primaryKey",
"KeyType" : HASH
}
]
},
"DeletionPolicy": {
"Fn::FindInMap" : [
"PartitionMap",
{ "Ref" : "AWS::Partition" },
"DeletionPolicy"
]
},
"UpdateReplacePolicy": {
"Fn::FindInMap" : [
"PartitionMap",
{ "Ref" : "AWS::Partition" },
"UpdateReplacePolicy"
]
}
}
}
}
```

### Use [Fn::FindInMap](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-findinmap.html) and `AWS::Region` Pseudo-Parameter in YAML
```
jlhood marked this conversation as resolved.
Show resolved Hide resolved
AWSTemplateFormatVersion: "2010-09-09"
Parameters:
Stage:
Type: String
AllowedValues:
- Prod
- Staging
- Dev
Mappings:
RegionMap:
us-east-1:
DeletionPolicy: Retain
UpdateReplacePolicy: Retain
us-west-1:
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
eu-west-1:
DeletionPolicy: Retain
UpdateReplacePolicy: Delete
ap-northeast-1:
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
Resources:
Bucket:
Type: "AWS::S3::Bucket"
DeletionPolicy: !FindInMap [RegionMap, !Ref "AWS::Region", DeletionPolicy]
UpdateReplacePolicy: !FindInMap [RegionMap, !Ref "AWS::Region", DeletionPolicy]
```