35

I describe existing AWS Lambda function in CloudFormation template and I face with the next issue. In our Lambda we configured few test events which helps us to verify some usecases (I mean functionality from the screenshot below).

enter image description here

But I don't see any abilities to add these test events to the CloudFormation template. AWS documentation don't help me with that. Is that possible at all or are there any workarounds how to export and import Lambda function test events?

Hleb
  • 7,037
  • 12
  • 58
  • 117
  • 9
    I found this question because it occurred to me that this would be useful ... so if anyone from aws reads this ... +1. – RH Becker Apr 10 '19 at 21:17
  • 1
    As indicated [here](https://docs.aws.amazon.com/lambda/latest/dg/testing-functions.html), "Lambda saves shareable test events as schemas in an Amazon EventBridge (CloudWatch Events) schema registry named `lambda-testevent-schemas`". In theory you could create a schema registry of that name using CFN -> add test events that way, however "... we recommend that you do not ..." – Brian Vandenberg Oct 11 '22 at 18:31

2 Answers2

8

Lambda test functionality is available only in the UI console, You can use Cloudformation Custom Resource to invoke a function from a cloudformation template. Resource properties allow AWS CloudFormation to create a custom payload to send to the Lambda function.

Sample code:

Resources:
  EnableLogs:
    Type: Custom::EnableLogs
    Version: '1.0'
    Properties:
      ServiceToken: arn:aws:lambda:us-east-1:acc:function:rds-EnableRDSLogs-1O6XLL6LWNR5Z
      DBInstanceIdentifier: mydb

the event parameter provides the resource properties. ex:

event['ResourceProperties']['DBInstanceIdentifier']
  • 1
    +1 for this being interesting & useful, however the OP was asking how to describe test events in Cloudformation templates -- presumably so they could put them under source control. – Brian Vandenberg Oct 11 '22 at 18:28
1

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 cloud formation (and also CDK and Terraform).

A solution I found online for cloud formation is this blog post:

Resources:
  MyLambdaSampleEventSchema:
    Type: AWS::EventSchemas::Schema
    Properties:
      # do not change. this is where aws stores events created in the Lambda Console
      RegistryName: lambda-testevent-schemas 
      # the name format is key to associate it with the Lambda Function
      SchemaName: !Sub
        - _${Name}-schema
        - Name: !Ref MyLambdaFunction
      Type: OpenApi3
      # An OpenAPI doc defining all the event schemas
      Content: '{}'

for Content, use the following json schema:

{
  "openapi": "3.0.0",
  "info": {
    "version": "1.0.0",
    "title": "Event"
  },
  "paths": {},
  "components": {
  "schemas": {
    "Event": {
      "type": "object",
      "required": [
        "foo",
        "count"
      ],
      "properties": {
        "foo": {
          "type": "string"
        },
        "count": {
          "type": "integer"
        }
      }
    },
    "examples": {
      "my-predefined-event": {
        "value": {
          "foo": "bar",
          "count": 10
        }
      },
      "throws-validation-error": {
        "value": {
          "foo": "bar"
        }
      }
    }
  }
}

I myself used CDK, as described here and a there is also a Terraform example

Meike
  • 171
  • 13