|
| 1 | +export const deletionPolicyValueDocsMap: ReadonlyMap<string, string> = getDeletionPolicyValueDocsMap(); |
| 2 | + |
| 3 | +function getDeletionPolicyValueDocsMap(): Map<string, string> { |
| 4 | + const docsMap = new Map<string, string>(); |
| 5 | + |
| 6 | + docsMap.set( |
| 7 | + 'Delete', |
| 8 | + [ |
| 9 | + '**Delete**', |
| 10 | + '\n', |
| 11 | + '---', |
| 12 | + 'CloudFormation deletes the resource and all its content if applicable during stack deletion. ', |
| 13 | + 'You can add this deletion policy to any resource type. ', |
| 14 | + "By default, if you don't specify a `DeletionPolicy`, CloudFormation deletes your resources. ", |
| 15 | + 'However, be aware of the following considerations:', |
| 16 | + '\n', |
| 17 | + '- For `AWS::RDS::DBCluster` resources, the default policy is `Snapshot`. ', |
| 18 | + "- For `AWS::RDS::DBInstance` resources that don't specify the DBClusterIdentifier property, the default policy is `Snapshot`.", |
| 19 | + '- For Amazon S3 buckets, you must delete all objects in the bucket for deletion to succeed. ', |
| 20 | + '- The default behavior of CloudFormation is to delete the secret with the ForceDeleteWithoutRecovery flag. ', |
| 21 | + '\n', |
| 22 | + '[Source Documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-attribute-deletionpolicy.html)', |
| 23 | + ].join('\n'), |
| 24 | + ); |
| 25 | + |
| 26 | + docsMap.set( |
| 27 | + 'Retain', |
| 28 | + [ |
| 29 | + '**Retain**', |
| 30 | + '\n', |
| 31 | + '---', |
| 32 | + 'CloudFormation keeps the resource without deleting the resource or its contents when its stack is deleted. ', |
| 33 | + 'You can add this deletion policy to any resource type. ', |
| 34 | + 'When CloudFormation completes the stack deletion, the stack will be in `Delete_Complete` state; however, resources that are retained continue to exist and continue to incur applicable charges until you delete those resources. ', |
| 35 | + '\n', |
| 36 | + 'For update operations, the following considerations apply: ', |
| 37 | + '\n', |
| 38 | + "- If a resource is deleted, the `DeletionPolicy` retains the physical resource but ensures that it's deleted from CloudFormation's scope. ", |
| 39 | + "- If a resource is updated such that a new physical resource is created to replace the old resource, then the old resource is completely deleted, including from CloudFormation's scope. ", |
| 40 | + '\n', |
| 41 | + '[Source Documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-attribute-deletionpolicy.html)', |
| 42 | + ].join('\n'), |
| 43 | + ); |
| 44 | + |
| 45 | + docsMap.set( |
| 46 | + 'RetainExceptOnCreate', |
| 47 | + [ |
| 48 | + '**RetainExceptOnCreate**', |
| 49 | + '\n', |
| 50 | + '---', |
| 51 | + '`RetainExceptOnCreate` behaves like `Retain` for stack operations, except for the stack operation that initially created the resource.', |
| 52 | + 'If the stack operation that created the resource is rolled back, CloudFormation deletes the resource. ', |
| 53 | + 'For all other stack operations, such as stack deletion, CloudFormation retains the resource and its contents. ', |
| 54 | + 'The result is that new, empty, and unused resources are deleted, while in-use resources and their data are retained. ', |
| 55 | + 'Refer to the [UpdateStack](https://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/API_UpdateStack.html) API documentation to use this deletion policy as an API parameter without updating your template.', |
| 56 | + '\n', |
| 57 | + '[Source Documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-attribute-deletionpolicy.html)', |
| 58 | + ].join('\n'), |
| 59 | + ); |
| 60 | + |
| 61 | + docsMap.set( |
| 62 | + 'Snapshot', |
| 63 | + [ |
| 64 | + '**Snapshot**', |
| 65 | + '\n', |
| 66 | + '---', |
| 67 | + 'For resources that support snapshots, CloudFormation creates a snapshot for the resource before deleting it. ', |
| 68 | + 'When CloudFormation completes the stack deletion, the stack will be in the `Delete_Complete` state; however, the snapshots that are created with this policy continue to exist and continue to incur applicable charges until you delete those snapshots. ', |
| 69 | + 'Resources that support snapshots include: ', |
| 70 | + '\n', |
| 71 | + '- [AWS::DocDB::DBCluster](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-docdb-dbcluster.html)', |
| 72 | + '- [AWS::EC2::Volume](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-ec2-volume.html)', |
| 73 | + '- [AWS::ElastiCache::CacheCluster](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-elasticache-cachecluster.html)', |
| 74 | + '- [AWS::ElastiCache::ReplicationGroup](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-elasticache-replicationgroup.html)', |
| 75 | + '- [AWS::Neptune::DBCluster](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-neptune-dbcluster.html)', |
| 76 | + '- [AWS::RDS::DBCluster](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-rds-dbcluster.html)', |
| 77 | + '- [AWS::RDS::DBInstance](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-rds-dbinstance.html)', |
| 78 | + '- [AWS::Redshift::Cluster](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-redshift-cluster.html)', |
| 79 | + '\n', |
| 80 | + '[Source Documentation](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-attribute-deletionpolicy.html)', |
| 81 | + ].join('\n'), |
| 82 | + ); |
| 83 | + |
| 84 | + return docsMap; |
| 85 | +} |
| 86 | + |
| 87 | +export const SNAPSHOT_SUPPORTED_RESOURCE_TYPES: ReadonlyArray<string> = [ |
| 88 | + 'AWS::DocDB::DBCluster', |
| 89 | + 'AWS::EC2::Volume', |
| 90 | + 'AWS::ElastiCache::CacheCluster', |
| 91 | + 'AWS::ElastiCache::ReplicationGroup', |
| 92 | + 'AWS::Neptune::DBCluster', |
| 93 | + 'AWS::RDS::DBCluster', |
| 94 | + 'AWS::RDS::DBInstance', |
| 95 | + 'AWS::Redshift::Cluster', |
| 96 | +]; |
| 97 | + |
| 98 | +export const DEFAULT_SNAPSHOT_RESOURCE_TYPES: ReadonlyArray<string> = [ |
| 99 | + 'AWS::RDS::DBCluster', |
| 100 | + 'AWS::RDS::DBInstance', // Only when DBClusterIdentifier is not specified |
| 101 | +]; |
| 102 | + |
| 103 | +export const DELETION_POLICY_VALUES: ReadonlyArray<string> = ['Delete', 'Retain', 'RetainExceptOnCreate', 'Snapshot']; |
| 104 | + |
| 105 | +export function supportsSnapshot(resourceType: string): boolean { |
| 106 | + return SNAPSHOT_SUPPORTED_RESOURCE_TYPES.includes(resourceType); |
| 107 | +} |
| 108 | + |
| 109 | +export function getDefaultDeletionPolicy(resourceType: string): string { |
| 110 | + if (DEFAULT_SNAPSHOT_RESOURCE_TYPES.includes(resourceType)) { |
| 111 | + return 'Snapshot'; |
| 112 | + } |
| 113 | + return 'Delete'; |
| 114 | +} |
| 115 | + |
| 116 | +export function isValidDeletionPolicyValue(value: string): boolean { |
| 117 | + return DELETION_POLICY_VALUES.includes(value); |
| 118 | +} |
0 commit comments