RemovalPolicy

class aws_cdk.core.RemovalPolicy(value)

Bases: Enum

Possible values for a resource’s Removal Policy.

The removal policy controls what happens to the resource if it stops being managed by CloudFormation. This can happen in one of three situations:

  • The resource is removed from the template, so CloudFormation stops managing it;

  • A change to the resource is made that requires it to be replaced, so CloudFormation stops managing it;

  • The stack is deleted, so CloudFormation stops managing all resources in it.

The Removal Policy applies to all above cases.

Many stateful resources in the AWS Construct Library will accept a removalPolicy as a property, typically defaulting it to RETAIN.

If the AWS Construct Library resource does not accept a removalPolicy argument, you can always configure it by using the escape hatch mechanism, as shown in the following example:

# bucket: s3.Bucket


cfn_bucket = bucket.node.find_child("Resource")
cfn_bucket.apply_removal_policy(RemovalPolicy.DESTROY)
ExampleMetadata:

infused

Example:

import aws_cdk.aws_opensearchservice as opensearch

# api: appsync.GraphqlApi


user = iam.User(self, "User")
domain = opensearch.Domain(self, "Domain",
    version=opensearch.EngineVersion.OPENSEARCH_1_2,
    removal_policy=RemovalPolicy.DESTROY,
    fine_grained_access_control=opensearch.AdvancedSecurityOptions(master_user_arn=user.user_arn),
    encryption_at_rest=opensearch.EncryptionAtRestOptions(enabled=True),
    node_to_node_encryption=True,
    enforce_https=True
)
ds = api.add_open_search_data_source("ds", domain)

ds.create_resolver(
    type_name="Query",
    field_name="getTests",
    request_mapping_template=appsync.MappingTemplate.from_string(JSON.stringify({
        "version": "2017-02-28",
        "operation": "GET",
        "path": "/id/post/_search",
        "params": {
            "headers": {},
            "query_string": {},
            "body": {"from": 0, "size": 50}
        }
    })),
    response_mapping_template=appsync.MappingTemplate.from_string("""[
            #foreach($entry in $context.result.hits.hits)
            #if( $velocityCount > 1 ) , #end
            $utils.toJson($entry.get("_source"))
            #end
          ]""")
)

Attributes

DESTROY

This is the default removal policy.

It means that when the resource is removed from the app, it will be physically destroyed.

RETAIN

This uses the ‘Retain’ DeletionPolicy, which will cause the resource to be retained in the account, but orphaned from the stack.

SNAPSHOT

This retention policy deletes the resource, but saves a snapshot of its data before deleting, so that it can be re-created later.

Only available for some stateful resources, like databases, EFS volumes, etc.

See:

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-deletionpolicy.html#aws-attribute-deletionpolicy-options