npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cdk-custom-resource-construct-example

v1.2.315

Published

This is an updated example of how to create a Custom Resource Construct for CDK. This example will walk you through the process of creating a [CDK Construct](https://constructs.dev/) that uses Typescript Custom Resources.

Downloads

1,284

Readme

CDK Custom Resource Construct Example

This is an updated example of how to create a Custom Resource Construct for CDK. This example will walk you through the process of creating a CDK Construct that uses Typescript Custom Resources.

Custom Resources

Custom Resources allow you to deploy and invoke Lambda functions during the deployment of a CDK. This can be used to supplement CDKs with features that are not natively part of CloudFormation or CDK.

In this example, our Lambda function is simply multiplying an input and returning the result

try {
  const multiplyResult = event.ResourceProperties.customResourceNumber * 2;
  response.Status = 'SUCCESS';
  response.Data = { Result: multiplyResult };
  return response;
} catch (error) {
  if (error instanceof Error) {
    response.Reason = error.message;
  }
  response.Status = 'FAILED';
  response.Data = { Result: error };
  return response;
}

Bundling

Because we are using Typescript for our Lambda, we must transpile it before we use it. To do this, we will use projen to execute an esbuild on our Lambda source code during the build.

To do this, in the .projenrc.ts we use bundler.addBundle:

project.bundler.addBundle('./resources/lambda/', {
  platform: 'node',
  target: 'node18',
});

This will create an index.js in the assets/resources/lambda directory. This is the what we will use in the Custom Resource.

Custom Resource Provider

To create the Custom Resource we will use CustomResourceProvider. This is different from Provider which is what is normally used within a CDK.

const customResourceProvider = CustomResourceProvider.getOrCreateProvider(
  this,
  'Custom::Resource',
  {
    codeDirectory:
      'node_modules/cdk-custom-resource-construct-example/assets/resources/lambda',
    runtime: CustomResourceProviderRuntime.NODEJS_18_X,
    timeout: Duration.seconds(60),
    policyStatements: [
      {
        Effect: 'Allow',
        Action: [
          'logs:CreateLogGroup',
          'logs:CreateLogStream',
          'logs:PutLogEvents',
        ],
        Resource: '*',
      },
    ],
  },
);

By using []getOrCreateProvider](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.CustomResourceProvider.html#static-getwbrorwbrcreatewbrproviderscope-uniqueid-props) we can ensure that the Lambda function is created only once even if it used multiple times. The codeDirectory used here is the output directory of the bundling. This is what will be used in the CDK that uses this Construct.

Custom Resource

Finally, we will create the Custom Resource using the previously created Provider.

const customResourceResult = new CustomResource(this, 'customResourceResult', {
  serviceToken: customResourceProvider.serviceToken,
  properties: {
    customResourceNumber: props.customResourceNumber,
  },
});

this.customResourceResult = customResourceResult.getAttString('Result');

This Custom Resource will take a prop when called and return the result.

Using the Custom Resource

To use this Construct, we will import the CustomResourceExample, implement it, and pass it a prop.

import { CfnOutput, Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { CustomResourceExample } from 'cdk-custom-resource-construct-example';

export class CdkCustomResourceConstructDemoStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    const result = new CustomResourceExample(this, 'customResourceResult', {
      customResourceNumber: 5,
    });

    new CfnOutput(this, 'customResourceOutput', {
      value: result.customResourceResult,
    });
  }
}

When used this way, the Custom Resource will deploy the Lambda, invoke it, and return the result to the calling CDK.

Developing

It can be difficult to develop and test a Construct. yalc is a tool that can help with this by allowing us to "publish" locally. To use yalc, it should be installed as a devDep in the .projenrc.ts of the source Construct.

  devDeps: ['yalc', 'esbuild'],

After building the construct using yarn projen build, you can use yalc publish --push --sig to push this package to the local store. This should result in an output similar to:

[email protected]+80079907 published in store.

By adding --sig we can differentiate versions. In this example +80079907 is the sig.

Next, you can create a project that uses this package. To do this, you can add the package with yalc: yalc add cdk-custom-resource-construct-example. This will add the package to the package.json file that can be used like any other package. If needed, you can use yalc update. The output should be similar to:

Package [email protected]+80079907 added