-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Description
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