Search code examples
amazon-web-servicesaws-lambdaaws-sam

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


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?


Solution

  • 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.