6

In the AWS Lambda management console you can have test events associated with a function.

Is it possible to configure the test events when deploying the Lambda function using the AWS CDK such that the test events are ready to use when someone later views the function in the management console?

Tom Dufall
  • 871
  • 1
  • 10
  • 21

2 Answers2

6

That is not possible at the moment as CloudFormation itself does not support this (see this answer). You can, as mentioned in the linked post, use a CloudFormation CustomResource to prepare the invocation.

Another option is to create a output that prepares a cli command with payload. So that you can just copy past the generated call aws lambda invoke --function-name {PopulateFromCDK} --payload '{"key": "value"}'

quadroid
  • 8,444
  • 6
  • 49
  • 82
  • Or for AWS CLI v2 : `aws lambda invoke --function-name 'myfunction' --cli-binary-format raw-in-base64-out --payload '{"id": "all"}' 'log.json'` – monkey Feb 26 '21 at 04:49
0

As of 16th of march 22, this is finally possible. AWS announced 'shareable test events' that consist of an eventbridge schema, which in turn can be managed with CDK and Terraform.

I found a very useful example for terraform in this Blogpost by tecRacer and used it as guideline for a CDK version. Here a CDK version in Python, using the aws documentation:

def shared_test_event(self, event_id, description, lambda_function_name):
        shareable_test = eventschemas.CfnSchema(
          self,
          event_id,
            registry_name="lambda-testevent-schemas",
            type="OpenApi3",

            # the properties below are optional
            description=description,
            schema_name=f"_{lambda_function_name}-schema",
            tags=[eventschemas.CfnSchema.TagsEntryProperty(
                key="Author",
                value="Me"
            )],
            
            content="""
            {
                "openapi": "3.0.0",
                "info": {
                    "version": "1.0.0",
                    "title": "Event"
                },
                "paths": {},
                "components": {
                    "schemas": {
                        "Event": {
                            "type": "object",
                            "required": [
                                "Parameter1",
                                "Parameter2"
                            ],
                            "properties": {
                                "Parameter1": {
                                    "type": "string"
                                },
                                "Parameter2": {
                                    "type": "string"
                                }
                            }
                        }
                    },
                    "examples": {
                        "test with 2 parameters: {
                            "value": {
                                "Parameter1": "test",
                                "Parameter2": "this"
                            }
                        },
                        "Test2": {
                            "value": {
                                "Parameter1": "different",
                                "Parameter2": „values“
                            }
                        }
                    }
                }
            }
            """
        )

Important subtleties:

  • The naming of schema_name is important for the association of schema with lambda
  • The parameter registry_name defines the type of the event, so it may not be changed, in order to be recognized as shareable lambda test
  • The parameter content needs to be provided in json format.
Meike
  • 171
  • 13