AwsCustomResource

class aws_cdk.custom_resources.AwsCustomResource(scope, id, *, policy, function_name=None, install_latest_aws_sdk=None, log_retention=None, on_create=None, on_delete=None, on_update=None, resource_type=None, role=None, timeout=None)

Bases: Construct

Defines a custom resource that is materialized using specific AWS API calls.

These calls are created using a singleton Lambda function.

Use this to bridge any gap that might exist in the CloudFormation Coverage. You can specify exactly which calls are invoked for the ‘CREATE’, ‘UPDATE’ and ‘DELETE’ life cycle events.

ExampleMetadata:

infused

Example:

aws_custom = cr.AwsCustomResource(self, "aws-custom",
    on_create=cr.AwsSdkCall(
        service="...",
        action="...",
        parameters={
            "text": "..."
        },
        physical_resource_id=cr.PhysicalResourceId.of("...")
    ),
    on_update=cr.AwsSdkCall(
        service="...",
        action="...",
        parameters={
            "text": "...",
            "resource_id": cr.PhysicalResourceIdReference()
        }
    ),
    policy=cr.AwsCustomResourcePolicy.from_sdk_calls(
        resources=cr.AwsCustomResourcePolicy.ANY_RESOURCE
    )
)
Parameters:
  • scope (Construct) –

  • id (str) –

  • policy (AwsCustomResourcePolicy) – The policy that will be added to the execution role of the Lambda function implementing this custom resource provider. The custom resource also implements iam.IGrantable, making it possible to use the grantXxx() methods. As this custom resource uses a singleton Lambda function, it’s important to note the that function’s role will eventually accumulate the permissions/grants from all resources.

  • function_name (Optional[str]) – A name for the singleton Lambda function implementing this custom resource. The function name will remain the same after the first AwsCustomResource is created in a stack. Default: - AWS CloudFormation generates a unique physical ID and uses that ID for the function’s name. For more information, see Name Type.

  • install_latest_aws_sdk (Optional[bool]) – Whether to install the latest AWS SDK v2. Allows to use the latest API calls documented at https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/index.html. The installation takes around 60 seconds. Default: true

  • log_retention (Optional[RetentionDays]) – The number of days log events of the singleton Lambda function implementing this custom resource are kept in CloudWatch Logs. Default: logs.RetentionDays.INFINITE

  • on_create (Union[AwsSdkCall, Dict[str, Any], None]) – The AWS SDK call to make when the resource is created. Default: - the call when the resource is updated

  • on_delete (Union[AwsSdkCall, Dict[str, Any], None]) – The AWS SDK call to make when the resource is deleted. Default: - no call

  • on_update (Union[AwsSdkCall, Dict[str, Any], None]) – The AWS SDK call to make when the resource is updated. Default: - no call

  • resource_type (Optional[str]) – Cloudformation Resource type. Default: - Custom::AWS

  • role (Optional[IRole]) – The execution role for the singleton Lambda function implementing this custom resource provider. This role will apply to all AwsCustomResource instances in the stack. The role must be assumable by the lambda.amazonaws.com service principal. Default: - a new role is created

  • timeout (Optional[Duration]) – The timeout for the singleton Lambda function implementing this custom resource. Default: Duration.minutes(2)

Methods

get_response_field(data_path)

Returns response data for the AWS SDK call as string.

Example for S3 / listBucket : ‘Buckets.0.Name’

Note that you cannot use this method if ignoreErrorCodesMatching is configured for any of the SDK calls. This is because in such a case, the response data might not exist, and will cause a CloudFormation deploy time error.

Parameters:

data_path (str) – the path to the data.

Return type:

str

get_response_field_reference(data_path)

Returns response data for the AWS SDK call.

Example for S3 / listBucket : ‘Buckets.0.Name’

Use Token.asXxx to encode the returned Reference as a specific type or use the convenience getDataString for string attributes.

Note that you cannot use this method if ignoreErrorCodesMatching is configured for any of the SDK calls. This is because in such a case, the response data might not exist, and will cause a CloudFormation deploy time error.

Parameters:

data_path (str) – the path to the data.

Return type:

Reference

to_string()

Returns a string representation of this construct.

Return type:

str

Attributes

grant_principal

The principal to grant permissions to.

node

The construct tree node associated with this construct.

Static Methods

classmethod is_construct(x)

Return whether the given object is a Construct.

Parameters:

x (Any) –

Return type:

bool