Skip to main content

CDK Constructs for AWS CloudFormation

Project description

AWS CloudFormation Construct Library

---

Stability: Stable


This module is part of the AWS Cloud Development Kit project.

Custom Resources

Custom Resources are CloudFormation resources that are implemented by arbitrary user code. They can do arbitrary lookups or modifications during a CloudFormation synthesis run.

You will typically use Lambda to implement a Construct implemented as a Custom Resource (though SNS topics can be used as well). Your Lambda function will be sent a CREATE, UPDATE or DELETE message, depending on the CloudFormation life cycle. It will perform whatever actions it needs to, and then return any number of output values which will be available as attributes of your Construct. In turn, those can be used as input to other Constructs in your model.

In general, consumers of your Construct will not need to care whether it is implemented in term of other CloudFormation resources or as a custom resource.

Note: when implementing your Custom Resource using a Lambda, use a SingletonLambda so that even if your custom resource is instantiated multiple times, the Lambda will only get uploaded once.

Example

The following shows an example of a declaring Custom Resource that copies files into an S3 bucket during deployment (the implementation of the actual Lambda handler is elided for brevity).

# Example automatically generated. See https://github.com/aws/jsii/issues/826
class CopyOperation(Construct):
    def __init__(self, parent, name, *, sourceBucket, targetBucket):
        super().__init__(parent, name)

        lambda_provider = lambda.SingletonFunction(self, "Provider",
            uuid="f7d4f730-4ee1-11e8-9c2d-fa7ae01bbebc",
            runtime=lambda.Runtime.PYTHON_3_7,
            code=lambda.Code.from_asset("../copy-handler"),
            handler="index.handler",
            timeout=Duration.seconds(60)
        )

        CustomResource(self, "Resource",
            provider=CustomResourceProvider.from_lambda(lambda_provider),
            properties={
                "source_bucket_arn": source_bucket.bucket_arn,
                "target_bucket_arn": target_bucket.bucket_arn
            }
        )

The aws-cdk-examples repository has examples for adding custom resources.

References

See the following section of the docs on details to write Custom Resources:

Nested Stacks

Nested stacks are stacks created as part of other stacks. You create a nested stack within another stack by using the NestedStack construct.

As your infrastructure grows, common patterns can emerge in which you declare the same components in multiple templates. You can separate out these common components and create dedicated templates for them. Then use the resource in your template to reference other templates, creating nested stacks.

For example, assume that you have a load balancer configuration that you use for most of your stacks. Instead of copying and pasting the same configurations into your templates, you can create a dedicated template for the load balancer. Then, you just use the resource to reference that template from within other templates.

The following example will define a single top-level stack that contains two nested stacks: each one with a single Amazon S3 bucket:

# Example automatically generated. See https://github.com/aws/jsii/issues/826
from aws_cdk.core import Stack, Construct, StackProps
import aws_cdk.aws_cloudformation as cfn
import aws_cdk.aws_s3 as s3

class MyNestedStack(cfn.NestedStack):
    def __init__(self, scope, id, *, parameters=None, timeout=None, notifications=None):
        super().__init__(scope, id, parameters=parameters, timeout=timeout, notifications=notifications)

        s3.Bucket(self, "NestedBucket")

class MyParentStack(Stack):
    def __init__(self, scope, id, *, description=None, env=None, stackName=None, tags=None):
        super().__init__(scope, id, description=description, env=env, stackName=stackName, tags=tags)

        MyNestedStack(self, "Nested1")
        MyNestedStack(self, "Nested2")

Resources references across nested/parent boundaries (even with multiple levels of nesting) will be wired by the AWS CDK through CloudFormation parameters and outputs. When a resource from a parent stack is referenced by a nested stack, a CloudFormation parameter will automatically be added to the nested stack and assigned from the parent; when a resource from a nested stack is referenced by a parent stack, a CloudFormation output will be automatically be added to the nested stack and referenced using Fn::GetAtt "Outputs.Xxx" from the parent.

Nested stacks also support the use of Docker image and file assets.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aws-cdk.aws-cloudformation-1.31.0.tar.gz (116.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aws_cdk.aws_cloudformation-1.31.0-py3-none-any.whl (114.0 kB view details)

Uploaded Python 3

File details

Details for the file aws-cdk.aws-cloudformation-1.31.0.tar.gz.

File metadata

  • Download URL: aws-cdk.aws-cloudformation-1.31.0.tar.gz
  • Upload date:
  • Size: 116.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.5

File hashes

Hashes for aws-cdk.aws-cloudformation-1.31.0.tar.gz
Algorithm Hash digest
SHA256 38b79372d511e97b20b15881ff430ec93186dca5b62cb2d490079ef4ffd9c6f8
MD5 7dfe7ee214bb6e3abfd0c42faec9d0d1
BLAKE2b-256 ec696d03adb9fe8a231ff142915d4e24a08cf2f8b18f64a15176337761e8baad

See more details on using hashes here.

File details

Details for the file aws_cdk.aws_cloudformation-1.31.0-py3-none-any.whl.

File metadata

  • Download URL: aws_cdk.aws_cloudformation-1.31.0-py3-none-any.whl
  • Upload date:
  • Size: 114.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.5

File hashes

Hashes for aws_cdk.aws_cloudformation-1.31.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ac52cb6efa11e21e4e52a1b5fea25c13288781c6cd45b65374d07b31c85edc41
MD5 4af6306ee54715ea8f9e723a8696edb7
BLAKE2b-256 add2316ed988a00cef87539de96a9a1bb0dd2873b6310c1620ba96ddd6a449be

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page