2
votes

CloudFormation itslef supports custom resources backed by lambda or ec2.

However, i could not find something similar on AWS CDK. The only way it seems you can define custom resources is now by specifying resources, actions and paramters.

Does it mean that with a current CDK I might be able to choose whether to call a lambda function or a specific "CLI" command ? Can anyone explain the fundamental logic behind custom resources in AWS CDK?

P.S. I perfectly know how to operate custom resources on CloudFormation.

1

1 Answers

4
votes

The custom resource you are referencing to is located in the package @aws-cdk/aws-cloudformation.

You can find a TypeScript example in the documentation:

interface CopyOperationProps {
  sourceBucket: IBucket;
  targetBucket: IBucket;
}

class CopyOperation extends Construct {
  constructor(parent: Construct, name: string, props: CopyOperationProps) {
    super(parent, name);

    const lambdaProvider = new lambda.SingletonFunction(this, 'Provider', {
      uuid: 'f7d4f730-4ee1-11e8-9c2d-fa7ae01bbebc',
      runtime: lambda.Runtime.PYTHON_3_7,
      code: lambda.Code.fromAsset('../copy-handler'),
      handler: 'index.handler',
      timeout: Duration.seconds(60),
    });

    new CustomResource(this, 'Resource', {
      provider: CustomResourceProvider.lambda(lambdaProvider),
      properties: {
        sourceBucketArn: props.sourceBucket.bucketArn,
        targetBucketArn: props.targetBucket.bucketArn,
      }
    });
  }
}