(triggers): Specify payload for the triggered lambda function
- Dominant language
- TypeScript
- Stars
- 12.9k
- Forks
- 4.6k
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 83
Description
### Describe the feature
The [Trigger](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.triggers.Trigger.html) construct invokes the lambda function. There is no way to provide any payload for the function.
I suggest adding a new property to the [TriggerProps](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.triggers.TriggerProps.html) interface and using the property's value as a payload for the function.
We can use `ResourceProperties` field in the `AWSLambda.CloudFormationCustomResourceEvent` event to store the information. We already put the `HandlerArn` there.
[Trigger's lambda function](https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk/triggers/lib/lambda/index.ts) can get the payload from the event and forward it to the called lambda function.
### Use Case
I create a trigger to run after the staging/development/whatever database is restored from the snapshot to change user passwords. The lambda takes a list of secrets with original passwords and staging/development/whatever passwords and changes passwords on the restored database.
Lambda function's environment variables hold all the necessary information now. I prefer to send the information as the lambda function payload.
One of the disadvantages of passing parameters as environment variables is updating the lambda every time we change parameters.
Working code
```typescript
const databaseCluster = new ServerlessClusterFromSnapshot(...);
const changePwdFunction = new NodejsFunction(stack, "ChangePwdFunction", {
entry: "src/ChangePassword.ts",
environment: {
DATABASE_HOST: databaseCluster.clusterEndpoint.hostname,
// other parameters
},
// ...
});
const trigger = new Trigger(stack, "ChangePwdTrigger", {
executeAfter: [databaseCluster],
handler: changePwdFunction,
});
```
Suggested code
```typescript
const databaseCluster = new ServerlessClusterFromSnapshot(...);
const changePwdFunction = new NodejsFunction(stack, "ChangePwdFunction", {
entry: "src/ChangePassword.ts",
// ...
});
const trigger = new Trigger(stack, "ChangePwdTrigger", {
executeAfter: [databaseCluster],
handler: changePwdFunction,
handlerPayload: {
databaseHost: databaseCluster.clusterEndpoint.hostname,
// other parameters
},
});
```
### Proposed Solution
I suggest the following changes:
1. Extend the [TriggerProps](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.triggers.TriggerProps.html) interface and add handler payload.
2. Send the payload in the [lambda](https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk/triggers/lib/lambda/index.ts).
3. Send payload to the lambda. I do not know how where this is done.
I might be able to implement this with a bit of help and some time.
### Other Information
_No response_
### Acknowledgements
- [X] I may be able to implement this feature request
- [ ] This feature might incur a breaking change
### CDK version used
2.20.0
### Environment details (OS name and version, etc.)
Environment independent (I think).
Contributor guide
Research direction
Start with the TriggerProps interface and packages/@aws-cdk/triggers/lib/lambda/index.ts, then trace how the CloudFormationCustomResourceEvent ResourceProperties field reaches the handler. Done means a new handler-payload property is forwarded to the invoked Lambda without relying on environment variables, with the trigger behavior and public API covered as appropriate.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, typescript
- Domain
- cloud, infrastructure
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100