atom88
atom88

Reputation: 1554

Can you create a test event for an AWS Lambda function via the AWS CLI?

In reviewing this URL from AWS I don't see any obvious way to add a "test event" programatically via the AWS CLI? Is there a way to do this?

https://docs.aws.amazon.com/cli/latest/reference/lambda/index.html

Upvotes: 1

Views: 3041

Answers (2)

Christophe Vidal
Christophe Vidal

Reputation: 1942

It's now possible to create shareable events through Cloudformation using AWS::EventSchemas::Schema.

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-eventschemas-schema.html

For example you can deploy this along your lambda:

Resources:
  DefaultSchema:
    Type: AWS::EventSchemas::Schema
    Properties:
      RegistryName: "lambda-testevent-schemas"
      SchemaName: _NAME_OF_YOUR_FUNCTION-schema
      Description: "Default event to manually trigger..."
      Type: OpenApi3
      Content: >
        {
          "openapi": "3.0.0",
          "info": {
            "version": "1.0.0",
            "title": "Event"
          },
          "paths": {},
          "components": {
            "schemas": {
              "Event": {
                "type": "object",
                "required": [
                  "key1"
                ],
                "properties": {
                  "feeds_name": {
                    "type": "array",
                    "items": {
                      "type": "string"
                    }
                  }
                }
              }
            },
            "examples": {
              "Event-Default": {
                "value": {
                  "key1": [
                    "Value1"
                  ]
                }
              }
            }
          }
        }

NAME_OF_YOUR_FUNCTION must be replaced strictly with the name of your Lambda (keep the underscore before, the dash after)

You may be able to deploy multiple events, for example like this:

     ...
     Content: >
        {
          "openapi": "3.0.0",
          "info": {
            "version": "1.0.0",
            "title": "Event"
          },
          "paths": {},
          "components": {
            "schemas": {
              "Event": {
                "type": "object"
              }
            },
            "examples": {
              "Event1": {
                "value": {
                  "key": [
                    "value1"
                  ]
                }
              },
              "Event2": {
                "value": {
                  "key": [
                    "value2"
                  ]
                }
              },
              "Event3": {
                "value": {
                  "key": [
                    "value3"
                  ],
                  "another_field": "hello world"
                }
              }
            }
          }
        }

Please note however that AWS recommends not doing this, as this will overwrite user settings. But I suppose there is also a counter argument to this, especially if Lambda templates shall be predefined from Cloudformation.

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. Lambda saves shareable test events as schemas in an Amazon EventBridge (CloudWatch Events) schema registry named lambda-testevent-schemas. As Lambda utilizes this registry to store and call shareable test events you create, we recommend that you do not edit this registry or create a registry using the lambda-testevent-schemas name.

Upvotes: 1

John Rotenstein
John Rotenstein

Reputation: 269520

The Test capabilities presented in the AWS Lambda management console are a feature of the console itself, not the AWS Lambda service.

The Test feature provides the ability to define an incoming event record, then invoke the Lambda using that record.

You could do the same thing from an AWS CLI call, but you would need to provide the event to pass to the function.

Upvotes: 3

Related Questions