1.2.219 • Published 2 days ago

cdk-custom-resource-construct-example v1.2.219

Weekly downloads
-
License
MIT-0
Repository
github
Last release
2 days ago

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:

cdk-custom-resource-construct-example@0.0.0+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 cdk-custom-resource-construct-example@0.0.0+80079907 added
1.2.219

2 days ago

1.2.218

3 days ago

1.2.217

4 days ago

1.2.215

6 days ago

1.2.216

5 days ago

1.2.214

7 days ago

1.2.213

8 days ago

1.2.212

9 days ago

1.2.211

10 days ago

1.2.210

11 days ago

1.2.209

14 days ago

1.2.208

15 days ago

1.2.207

16 days ago

1.2.206

17 days ago

1.2.205

21 days ago

1.2.204

22 days ago

1.2.202

24 days ago

1.2.203

23 days ago

1.2.201

25 days ago

1.2.200

28 days ago

1.2.199

29 days ago

1.2.196

1 month ago

1.2.198

1 month ago

1.2.197

1 month ago

1.2.195

1 month ago

1.2.194

1 month ago

1.2.193

1 month ago

1.2.192

1 month ago

1.2.191

1 month ago

1.2.190

1 month ago

1.2.189

1 month ago

1.2.188

1 month ago

1.2.187

1 month ago

1.2.186

1 month ago

1.2.185

2 months ago

1.2.184

2 months ago

1.2.183

2 months ago

1.2.182

2 months ago

1.2.181

2 months ago

1.2.178

2 months ago

1.2.179

2 months ago

1.2.180

2 months ago

1.2.177

2 months ago

1.2.176

2 months ago

1.2.175

2 months ago

1.2.174

2 months ago

1.2.173

2 months ago

1.2.172

2 months ago

1.2.171

2 months ago

1.2.170

2 months ago

1.2.169

2 months ago

1.2.167

2 months ago

1.2.168

2 months ago

1.2.166

2 months ago

1.2.165

2 months ago

1.2.164

7 months ago

1.2.163

11 months ago

1.2.136

12 months ago

1.2.135

12 months ago

1.2.138

12 months ago

1.2.137

12 months ago

1.2.139

12 months ago

1.2.145

12 months ago

1.2.144

12 months ago

1.2.147

11 months ago

1.2.146

12 months ago

1.2.149

11 months ago

1.2.148

11 months ago

1.2.141

12 months ago

1.2.140

12 months ago

1.2.143

12 months ago

1.2.142

12 months ago

1.2.156

11 months ago

1.2.155

11 months ago

1.2.158

11 months ago

1.2.157

11 months ago

1.2.159

11 months ago

1.2.150

11 months ago

1.2.152

11 months ago

1.2.151

11 months ago

1.2.154

11 months ago

1.2.153

11 months ago

1.2.161

11 months ago

1.2.160

11 months ago

1.2.162

11 months ago

1.2.134

12 months ago

1.2.133

12 months ago

1.2.130

12 months ago

1.2.132

12 months ago

1.2.131

12 months ago

1.2.112

1 year ago

1.2.111

1 year ago

1.2.114

1 year ago

1.2.113

1 year ago

1.2.116

1 year ago

1.2.115

1 year ago

1.2.118

1 year ago

1.2.117

1 year ago

1.2.110

1 year ago

1.2.109

1 year ago

1.2.123

1 year ago

1.2.122

1 year ago

1.2.125

1 year ago

1.2.124

1 year ago

1.2.127

1 year ago

1.2.126

1 year ago

1.2.129

12 months ago

1.2.128

12 months ago

1.2.121

1 year ago

1.2.120

1 year ago

1.2.119

1 year ago

1.2.93

1 year ago

1.2.96

1 year ago

1.2.97

1 year ago

1.2.94

1 year ago

1.2.95

1 year ago

1.2.98

1 year ago

1.2.99

1 year ago

1.2.101

1 year ago

1.2.100

1 year ago

1.2.103

1 year ago

1.2.102

1 year ago

1.2.105

1 year ago

1.2.104

1 year ago

1.2.107

1 year ago

1.2.106

1 year ago

1.2.108

1 year ago

1.2.85

1 year ago

1.2.86

1 year ago

1.2.84

1 year ago

1.2.89

1 year ago

1.2.87

1 year ago

1.2.88

1 year ago

1.2.92

1 year ago

1.2.90

1 year ago

1.2.91

1 year ago

1.2.81

1 year ago

1.2.82

1 year ago

1.2.80

1 year ago

1.2.83

1 year ago

1.2.79

1 year ago

1.2.78

1 year ago

1.2.77

1 year ago

1.2.75

1 year ago

1.2.76

1 year ago

1.2.68

1 year ago

1.2.69

1 year ago

1.2.70

1 year ago

1.2.71

1 year ago

1.2.74

1 year ago

1.2.72

1 year ago

1.2.73

1 year ago

1.2.67

1 year ago

1.2.66

1 year ago

1.2.64

1 year ago

1.2.65

1 year ago

1.2.56

1 year ago

1.2.57

1 year ago

1.2.54

1 year ago

1.2.55

1 year ago

1.2.58

1 year ago

1.2.59

1 year ago

1.2.60

1 year ago

1.2.63

1 year ago

1.2.61

1 year ago

1.2.62

1 year ago

1.2.0

1 year ago

1.2.8

1 year ago

1.2.7

1 year ago

1.0.9

1 year ago

1.2.6

1 year ago

1.0.8

1 year ago

1.2.5

1 year ago

1.0.7

1 year ago

1.2.4

1 year ago

1.0.6

1 year ago

1.2.3

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

1.2.41

1 year ago

1.2.42

1 year ago

1.2.40

1 year ago

1.2.45

1 year ago

1.2.46

1 year ago

1.2.43

1 year ago

1.2.44

1 year ago

1.2.49

1 year ago

1.2.47

1 year ago

1.2.48

1 year ago

1.2.52

1 year ago

1.2.53

1 year ago

1.2.50

1 year ago

1.2.51

1 year ago

1.2.12

1 year ago

1.2.13

1 year ago

1.2.10

1 year ago

1.2.11

1 year ago

1.2.16

1 year ago

1.2.17

1 year ago

1.2.14

1 year ago

1.2.15

1 year ago

1.1.0

1 year ago

1.2.18

1 year ago

1.2.20

1 year ago

1.2.23

1 year ago

1.2.24

1 year ago

1.2.21

1 year ago

1.2.22

1 year ago

1.2.27

1 year ago

1.2.28

1 year ago

1.2.25

1 year ago

1.2.26

1 year ago

1.2.29

1 year ago

1.2.30

1 year ago

1.2.31

1 year ago

1.2.9

1 year ago

1.2.34

1 year ago

1.0.11

1 year ago

1.2.35

1 year ago

1.0.10

1 year ago

1.2.32

1 year ago

1.2.33

1 year ago

1.2.38

1 year ago

1.2.39

1 year ago

1.0.14

1 year ago

1.2.36

1 year ago

1.0.13

1 year ago

1.2.37

1 year ago

1.0.12

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.0.0

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago