Can I include a test event in a SAM template that is deployed to the lambda web console?
Asked Answered
I

2

6

I know I can invoke lambdas with sam local and also the command line.

But sometimes it's convenient to test the lambda by invoking it through the UI by configuring a test event there.

Is it possible to include sample/test event in the sam template so when it's deployed the test event is populated in the UI for me? So I can keep that test event updated and just have to press the test button if I'm working with it through the UI?

Implead answered 12/5, 2020 at 23:56 Comment(1)
The test event options in lambda console are specific to console only. There is no api to populate or mange them unfortunately. – Roister
H
1

Yes, as of SAM CLI v1.98.0. That release introduced support for Lambda shareable test events.

Shareable test events are test events that you can share with other users in the same AWS account. You can edit other users' shareable test events and invoke your function with them.

Shareable test events are visible from the Lamba console alongside the old console test events.1

The sam remote test-event command has put, list, get and delete subcommands to manage shareable test events. For instance, the put subcommand creates or updates an event. Events are associated with a Lambda function by LogicalID+Stack or ARN, which are passed as an argument:

sam remote test-event put <Lambda Logical ID> --stack-name MyStack --file event.json --name MyEvent

sam remote test-event put <Lambda ARN> --file event.json --name MyEvent

Invoke a remote function with a shareable test event using the --test-event-name option.2, passing the event name:

sam remote invoke <Lambda Logical ID> --stack-name MyStack --test-event-name MyEvent

  1. Shareable test events are stored in an EventBridge schema registry named lambda-testevent-schemas in your account. IAM access to shared events is governed through permissions on the registry.
  2. The new --test-event-name option uses a remote sharable test event. The existing --event-file option uses a local json event.
Hombre answered 9/10, 2023 at 16:36 Comment(2)
Does the SAM template spec include a field for this test event? So I could point the template itself to event.json and it just uploads it for me when I deploy? – Implead
@Implead Not that I can see. The feature appears to be CLI only. – Hombre
F
1

Others are wanting the same thing 😁, but when the same question was asked in a github issue on the SAM repo in 2018, Configuring test events through SAM template, AWS engineers said it's not in scope for SAM.

FWIW the answer suggested CodePipeline as a possible way to do something similar, programmatically:

I think this is out of scope of what SAM can do. SAM can't set up a console test event, like Brett mentioned, but something similar to this could be set up in a CodePipeline workflow. For more information about CodePipeline, see the docs:
https://docs.aws.amazon.com/lambda/latest/dg/build-pipeline.html

Fosterling answered 14/12, 2021 at 5:36 Comment(0)
H
1

Yes, as of SAM CLI v1.98.0. That release introduced support for Lambda shareable test events.

Shareable test events are test events that you can share with other users in the same AWS account. You can edit other users' shareable test events and invoke your function with them.

Shareable test events are visible from the Lamba console alongside the old console test events.1

The sam remote test-event command has put, list, get and delete subcommands to manage shareable test events. For instance, the put subcommand creates or updates an event. Events are associated with a Lambda function by LogicalID+Stack or ARN, which are passed as an argument:

sam remote test-event put <Lambda Logical ID> --stack-name MyStack --file event.json --name MyEvent

sam remote test-event put <Lambda ARN> --file event.json --name MyEvent

Invoke a remote function with a shareable test event using the --test-event-name option.2, passing the event name:

sam remote invoke <Lambda Logical ID> --stack-name MyStack --test-event-name MyEvent

  1. Shareable test events are stored in an EventBridge schema registry named lambda-testevent-schemas in your account. IAM access to shared events is governed through permissions on the registry.
  2. The new --test-event-name option uses a remote sharable test event. The existing --event-file option uses a local json event.
Hombre answered 9/10, 2023 at 16:36 Comment(2)
Does the SAM template spec include a field for this test event? So I could point the template itself to event.json and it just uploads it for me when I deploy? – Implead
@Implead Not that I can see. The feature appears to be CLI only. – Hombre

© 2022 - 2024 β€” McMap. All rights reserved.