Recently, the CloudFormation team has released a new feature to allow resource creation directly from an archive located in an S3 bucket (it was only possible on the CLI before):
Since this is a new feature, CDK does not provide a high-level construct to create your resource, so the workaround is to use a
CfnResource to execute a CloudFormation template in your CDK stack.
The code below explains how to add a new CloudFormation type,
Datadog::Monitors::Monitor, that allows you to create
Datadog monitors with CloudFormation. Since CDK is deploying CFN, we can use all the new features directly.
As you can see, we are creating a new
CfnResource with the type
AWS::CloudFormation::ResourceVersion, which is the CFN type that allows you to create a new custom resource. Now we’ll set some properties for this new type:
TypeNameis the name we will use each time we want to create a new monitor.
SchemaHandlerPackageis the location of the sources of the code we should execute when using this type.
Verify that your new resource is created
Since you cannot see your custom resources in the console, if you want to check that your resource is well created, you can use this command:
$ aws cloudformation list-types# output
"Description": "Datadog Monitor"
How to use this new custom resource
Now that you have your custom resource type, you can use it in your stack, but you may have this type of error when you try to use the new type in the same stack where you are creating it:
Error [ValidationError]: Template format error: Unrecognized resource types: [Datadog::Monitors::Monitor]
To avoid this problem, you should use the type in a
NestedStack and add a
dependency to the creation of the type for this