What is @aws-cdk/aws-cloudformation?
@aws-cdk/aws-cloudformation is an AWS Cloud Development Kit (CDK) module that allows you to define AWS CloudFormation templates using TypeScript or JavaScript. It provides a high-level, object-oriented abstraction to define AWS resources and manage their lifecycle.
What are @aws-cdk/aws-cloudformation's main functionalities?
Creating a CloudFormation Stack
This code sample demonstrates how to create a CloudFormation stack using the @aws-cdk/aws-cloudformation package. It defines a nested stack with a template URL and parameters.
const cdk = require('@aws-cdk/core');
const cfn = require('@aws-cdk/aws-cloudformation');
class MyStack extends cdk.Stack {
constructor(scope, id, props) {
super(scope, id, props);
new cfn.CfnStack(this, 'MyNestedStack', {
templateUrl: 'https://s3.amazonaws.com/my-bucket/my-template.json',
parameters: {
MyParameter: 'ParameterValue'
}
});
}
}
const app = new cdk.App();
new MyStack(app, 'MyStack');
app.synth();
Custom Resources
This code sample demonstrates how to create a custom resource using the @aws-cdk/aws-cloudformation package. It defines a custom resource with a service token and properties.
const cdk = require('@aws-cdk/core');
const cfn = require('@aws-cdk/aws-cloudformation');
class MyStack extends cdk.Stack {
constructor(scope, id, props) {
super(scope, id, props);
new cfn.CfnCustomResource(this, 'MyCustomResource', {
serviceToken: 'arn:aws:lambda:region:account-id:function:function-name',
properties: {
MyProperty: 'PropertyValue'
}
});
}
}
const app = new cdk.App();
new MyStack(app, 'MyStack');
app.synth();
Wait Conditions
This code sample demonstrates how to create a wait condition using the @aws-cdk/aws-cloudformation package. It defines a wait condition handle and a wait condition with a timeout and count.
const cdk = require('@aws-cdk/core');
const cfn = require('@aws-cdk/aws-cloudformation');
class MyStack extends cdk.Stack {
constructor(scope, id, props) {
super(scope, id, props);
const waitHandle = new cfn.CfnWaitConditionHandle(this, 'MyWaitHandle');
new cfn.CfnWaitCondition(this, 'MyWaitCondition', {
handle: waitHandle.ref,
timeout: '300',
count: 1
});
}
}
const app = new cdk.App();
new MyStack(app, 'MyStack');
app.synth();
Other packages similar to @aws-cdk/aws-cloudformation
aws-sdk
The aws-sdk package is the official AWS SDK for JavaScript, which allows you to interact with AWS services directly. Unlike @aws-cdk/aws-cloudformation, which provides a high-level abstraction for defining CloudFormation templates, aws-sdk provides low-level APIs to interact with AWS services, including CloudFormation.
serverless
The serverless package is a framework for building and deploying serverless applications on AWS and other cloud providers. It abstracts away much of the complexity of managing infrastructure, similar to @aws-cdk/aws-cloudformation, but focuses on serverless architectures and provides a different set of abstractions and tools.
pulumi
Pulumi is an infrastructure as code tool that allows you to define cloud resources using general-purpose programming languages. It is similar to @aws-cdk/aws-cloudformation in that it provides high-level abstractions for defining cloud infrastructure, but it supports multiple cloud providers and languages beyond just AWS and TypeScript/JavaScript.
CDK Constructs for AWS CloudFormation
This module is part of the AWS Cloud Development Kit project.
CodePipeline Actions for CloudFormation
This module contains Actions that allows you to deploy to CloudFormation from AWS CodePipeline.
For example, the following code fragment defines a pipeline that automatically deploys a CloudFormation template
directly from a CodeCommit repository, with a manual approval step in between to confirm the changes:
example Pipeline to deploy CloudFormation
See the AWS documentation
for more details about using CloudFormation in CodePipeline.
Actions defined by this package
This package defines the following actions:
- PipelineCreateUpdateStackAction - Deploy a CloudFormation template directly from the pipeline. The indicated stack is created,
or updated if it already exists. If the stack is in a failure state, deployment will fail (unless
replaceOnFailure
is set to true
, in which case it will be destroyed and recreated). - PipelineDeleteStackAction - Delete the stack with the given name.
- PipelineCreateReplaceChangeSetAction - Prepare a change set to be applied later. You will typically use change sets if you want
to manually verify the changes that are being staged, or if you want to separate the people (or system) preparing the
changes from the people (or system) applying the changes.
- PipelineExecuteChangeSetAction - Execute a change set prepared previously.
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, and can 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
Sample of a Custom Resource that copies files into an S3 bucket during deployment
(implementation of actual copy.py
operation elided).
interface CopyOperationProps {
sourceBucket: IBucket;
targetBucket: IBucket;
}
class CopyOperation extends Construct {
constructor(parent: Construct, name: string, props: DemoResourceProps) {
super(parent, name);
const lambdaProvider = new SingletonLambda(this, 'Provider', {
uuid: 'f7d4f730-4ee1-11e8-9c2d-fa7ae01bbebc',
code: new LambdaInlineCode(resources['copy.py']),
handler: 'index.handler',
timeout: 60,
runtime: LambdaRuntime.Python3,
});
new CustomResource(this, 'Resource', {
lambdaProvider,
properties: {
sourceBucketArn: props.sourceBucket.bucketArn,
targetBucketArn: props.targetBucket.bucketArn,
}
});
}
}
More examples are in the example
directory, including an example of how to use
the cfnresponse
module that is provided for you by CloudFormation.
References
See the following section of the docs on details to write Custom Resources: