Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for CDK Custom Resources #190

Merged
merged 8 commits into from
Nov 15, 2024

Conversation

flostadler
Copy link
Contributor

@flostadler flostadler commented Oct 31, 2024

This PR adds support for CloudFormation Custom Resource to pulumi-cdk. It implements does so by using the CustomResourceEmulator resource from aws-native.

For the first implementation we decided to limit the scope to Lambda backed Custom Resources, because the SNS variants are not widely used.

I'd recommend reviewing in this order:

  • src/graph.tsand src/converters/app-converter.ts. The changes in these files ensure that Custom Resources get correctly parsed and other resources can reference their attributes with the GetAtt intrinsic
  • src/cfn-resource-mappings.ts: This constructs the CustomResourceEmulator based on the CDK inputs while re-using the staging bucket to store the CustomResource responses.
  • unit & integration tests

Noteworthy:
I added a temporary workaround for shortening the resource names until pulumi/pulumi-aws-native#1816 is resolved. It can be toggled on by setting the PULUMI_CDK_EXPERIMENTAL_MAX_NAME_LENGTH env variable. Without this none of the CustomResources worked because they have deeply nested Lambdas and IAM roles. Those resources have a max name limit of 64.

Closes #109
Closes #60

@flostadler flostadler self-assigned this Oct 31, 2024
@flostadler flostadler force-pushed the flostadler/cdk-custom-resources branch from 8768959 to 3b3a40a Compare October 31, 2024 16:31
@flostadler flostadler force-pushed the flostadler/cdk-custom-resources branch from 3b3a40a to e6b7f14 Compare November 14, 2024 10:34
@@ -203,13 +211,33 @@ export class GraphBuilder {
if (resource.Type === 'AWS::EC2::VPC') {
this.vpcNodes[node.logicalId] = { vpcNode: node, vpcCidrBlockNode: undefined };
}
} else if (node.construct.constructInfo?.fqn === 'aws-cdk-lib.CfnResource') {
Copy link
Contributor Author

@flostadler flostadler Nov 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously we were missing resources that were not in the tree.
I noticed this because CDK CustomResources label the CfnCustomResource with the id Default. This is a special ID that causes those resources to be ignored in the tree.

We need #186 to validate all resources of the template get mapped.

@@ -116,7 +116,19 @@ export function mapToCfnResource(
const mName = moduleName(typeName).toLowerCase();
const pType = pulumiTypeName(typeName);
const awsModule = aws as any;
return new awsModule[mName][pType](logicalId, props, options);

// Workaround until TODO[pulumi/pulumi-aws-native#1816] is resolved.
Copy link
Contributor Author

@flostadler flostadler Nov 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this none of the CDK CustomResources I tried deploy successfully. They are all nested too deep for the 64 char limits of Lambda Functions and IAM roles

@flostadler flostadler requested review from corymhall, t0yv0 and a team November 15, 2024 16:24
@flostadler flostadler marked this pull request as ready for review November 15, 2024 16:24
Copy link
Contributor

@corymhall corymhall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@flostadler flostadler merged commit 0f18dfe into main Nov 15, 2024
13 checks passed
@flostadler flostadler deleted the flostadler/cdk-custom-resources branch November 15, 2024 17:15
assert.NotEmpty(t, url)

// Validate that the index.html file is deployed
integration.AssertHTTPResultWithRetry(t, url, nil, 60*time.Second, func(body string) bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really nice.

stackId: stack.node.id,
bucketName: stagingBucket,
bucketKeyPrefix: `${synth.getDeployTimePrefix()}pulumi/custom-resources/${stackId}/${logicalId}`,
serviceToken: rawProps.ServiceToken,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to check that ServiceToken is not undefined?

// WHEN
mapToCustomResource(logicalId, cfnType, cfnProps, {}, stack);
// THEN
expect(aws.cloudformation.CustomResourceEmulator).toHaveBeenCalledWith(logicalId, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm ok I see.

@pulumi-bot
Copy link
Contributor

This PR has been shipped in release v1.0.0.

This was referenced Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support CDK Custom Resources Support BucketDeployment
4 participants