8000 CustomResource: ServiceToken clobbered by properties · Issue #32468 · aws/aws-cdk · GitHub
[go: up one dir, main page]

Skip to content
CustomResource: ServiceToken clobbered by properties #32468
@Shwaring

Description

@Shwaring

Describe the bug

The properties construct props implies that all the properties are sent to the lambda. However if a key ServiceToken is added to the properties object it will set the service token for the custom resource, and even overwrite the value provided in the seviceToken constructor prop.

Regression Issue

  • Select this option if this issue appears to be a regression.

Last Known Working CDK Version

No response

Expected Behavior

The service token of the custom resource should always be the value provided in serviceToken

Current Behavior

The ServiceToken property of the custom resource is overwritten with the value in the property.

Reproduction Steps

Warning

Deploying this stack will fail and take 2 hours too resolve due to custom resource timeout, Only synth is needed to verify the bug.

Create a basic typescript cdk app,

mkdir testApp
cd testApp
cdk init app --language=typescript

update ./bin/test_app.ts

#!/usr/bin/env node
import * as cdk from 'aws-cdk-lib'
import { Code, Function, Runtime } from 'aws-cdk-lib/aws-lambda';
import { Provider } from 'aws-cdk-lib/custom-resources';
import { Construct } from 'constructs';
const app = new cdk.App();
export class TestCdkAppStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);
Function
    const myFunction = new Function(this, 'MyFunction', {
      runtime: Runtime.NODEJS_18_X,
      handler: 'index.handler',
      code: Code.fromInline(`
        exports.handler = async (event) => {
          return(JSON.stringify(event, undefined, 2));
        };
      `),
    });
    const provider = new Provider(this, 'Provider', {
      onEventHandler: myFunction,
    });
    const invoker = new cdk.CustomResource(this, 'Invoker', {
      serviceToken: provider.serviceToken,
      properties: {
        ServiceToken: myFunction.functionArn
      },
    });
  }
}
new TestCdkAppStack(app, 'TestCdkAppStack', {

});

Run CDK synth and observe the cdk.out/TestCdkAppStack.template.json. See that the invoker has the service token of the lambda function instead of the provider.

  "Invoker": {
   "Type": "AWS::CloudFormation::CustomResource",
   "Properties": {
    "ServiceToken": {
     "Fn::GetAtt": [
      "MyFunction3BAA72D1",
      "Arn"
     ]
    }
   },
   "UpdateReplacePolicy": "Delete",
   "DeletionPolicy": "Delete",
   "Metadata": {
    "aws:cdk:path": "TestCdkAppStack/Invoker/Default"
    }

Comment out the properties attribute of the custom resource and synth again. Observe that it is now correctly has the service token of the provider,

    const invoker = new cdk.CustomResource(this, 'Invoker', {
      serviceToken: provider.serviceToken,
      // properties: {
      //   ServiceToken: myFunction.functionArn
      // },
    });
  "Invoker": {
   "Type": "AWS::CloudFormation::CustomResource",
   "Properties": {
    "ServiceToken": {
     "Fn::GetAtt": [
      "ProviderframeworkonEvent83C1D0A7",
      "Arn"
     ]
    }
   },
   "UpdateReplacePolicy": "Delete",
   "DeletionPolicy": "Delete",
   "Metadata": {
    "aws:cdk:path": "TestCdkAppStack/Invoker/Default"
   }
  },

Possible Solution

properties object overwrites the invokers base properties. Potentially provide a warning of this.

Additional Information/Context

No response

CDK CLI Version

2.171.1 (build a95560c)

Framework Version

No response

Node.js Version

v20.12.2

OS

linux

Language

TypeScript

Language Version

5.6.3

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/custom-resourcesRelated to AWS CDK Custom ResourcesbugThis issue is a bug.effort/smallSmall work item – less than a day of effortp2

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0