Invoking an existing Lambda Function using AWSCustomResource #28240
-
I am trying to invoke an existing lambda function using AWSCustomResource with the code below: const existingLambda = cdk.aws_lambda.Function.fromFunctionName(
stack,
`TriggerExistingLambda`,
'existing-function-name'
);
return new cdk.custom_resources.AwsCustomResource(stack, 'TriggerExistingLambdaCustomResource', {
policy: cdk.custom_resources.AwsCustomResourcePolicy.fromStatements([
new cdk.aws_iam.PolicyStatement({
actions: ['lambda:InvokeFunction'],
effect: cdk.aws_iam.Effect.ALLOW,
resources: [existingLambda.functionArn],
}),
]),
// I presume AWS will generate another lambda internall to execute the onCreate API call?
onCreate: {
service: 'Lambda',
action: 'invoke',
parameters: {
FunctionName: existingLambda.functionName,
InvocationType: 'RequestResponse',
Payload: 'some-payload',
},
physicalResourceId: cdk.custom_resources.PhysicalResourceId.of(`${resourceName}LambdaPhysicalResourceId`),
},
installLatestAwsSdk: false,
}); Here is the generated cloudformation manifest.yml LambdaTriggerSetupRDSIAMEE3AB6F1:
Type: Custom::AWS
Properties:
ServiceToken:
Fn::GetAtt:
- AWS679f53fac002430cb0da5b7982bd22872D164C4C
- Arn
Create: ....
AWS679f53fac002430cb0da5b7982bd22872D164C4C:
Type: AWS::Lambda::Function
Properties:
Code:
S3Bucket: cdk-hnb659fds-assets-020653668000-ap-southeast-1
S3Key: d62cd506a0f4ef6a29d7a97e2a02d80bc8248142ce428543cfbf0d84b3c116a2.zip
Role:
Fn::GetAtt:
- AWS679f53fac002430cb0da5b7982bd2287ServiceRoleC1EA0FF2
- Arn
Handler: index.handler
Runtime: nodejs18.x
Timeout: 900
DependsOn:
- AWS679f53fac002430cb0da5b7982bd2287ServiceRoleC1EA0FF2
Metadata:
aws:cdk:path: Stack/AWS679f53fac002430cb0da5b7982bd2287/Resource
aws:asset:path: asset.d62cd506a0f4ef6a29d7a97e2a02d80bc8248142ce428543cfbf0d84b3c116a2
aws:asset:is-bundled: false
aws:asset:property: Code The error: For resource and when I checked the S3 console, Am I understanding AWSCustomResource wrongly? I presumed the CDK should handle the onCreate event handling to call the fyi: I am also using AWS's |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I've found the reason why: I am using AWS's Script for copilot deployments: export $SERVICE_NAME=<name>
copilot svc package --name $SERVICE_NAME --env dev
cdk-assets publish --path /copilot/$SERVICE_NAME/overrides/cdk.out/Stack.assets.json
copilot svc deploy --name $SERVICE_NAME --env dev |
Beta Was this translation helpful? Give feedback.
I've found the reason why:
I am using AWS's
copilot
to deploy.copilot
runscdk synth
internally to generate the cloudformation templates, and then deploys the cloudformation template. It does not upload the assets that generated withcdk synth
to the cdk S3 bucket. To resolve this, I separately use acdk-assets publish --path <some-copilot-path>/cdk.out/Stack.assets.json
to upload the assets first to S3 before deploying.Script for copilot deployments: