20

I can select an event from event template when I trigger a lambda function. How can I create a customized event template in terraform. I want to make it easier for developers to trigger the lambda by selecting this customized event template from the list.

I'd like to add an event on this list:

enter image description here

2 Answers 2

16

Unfortunately, at the time of this answer (2020-02-21), there is no way to accomplish this via the APIs that AWS provides. Ergo, the terraform provider does not have the ability to accomplish this (it's limited to what's available in the APIs).

I also have wanted to be able to configure test events via terraform.

A couple of options

  • Propose to AWS that they expose some APIs for managing test events. This would give contributors to the AWS terraform provider the opportunity to add this resource.
  • Provide the developers with a PostMan collection, set of shell scripts (or other scripts) using the awscli, or some other mechanism to invoke the lambdas. This is essentially the same as pulling the templating functionality out of the console and into your own tooling.
Sign up to request clarification or add additional context in comments.

Comments

7

I did try something and it worked. I must warn that this is reverse engineering and may break anytime in future, but works well for me so far.

As per Amazon doc for testing lambda functions, whenever a Shareable Test Event is created for any lambda, it is stored under a new schema under the lambda-testevent-schemas schema registry.

I made use of this information and figured out the conventions AWS follows to keep track of the events so that I can use those to manage resources using terraform

  1. The name of the schema is _<name_of_lambda_function>-schema, hence, from terraform I manage a new schema named _<name_of_lambda_function>-schema
resource "aws_schemas_schema" "my_lambda_shared_events" {
  name          = "_${aws_lambda_function.my_lambda.function_name}-schema"
  registry_name = "lambda-testevent-schemas"
  type          = "OpenApi3"
  description   = "The schema definition for shared test events"
  content       = local.my_lambda_shared_events_schema
}
  1. I create a json doc (my_lambda_shared_events_schema) which follows the OpenAPI3 convention. For example =>
{
  "components": {
    "examples": {
      "name_of_the_event_1": {
        "value": {
          ... the value you need ...
        }
      },
      "name_of_the_event_2": {
        "value": {
          ... the value you need ...
        }
      }
    },
    "schemas": {
      "Event": {
        "properties": {
          ... structure of the event you need ...
        },
        "required": [... any required params ...],
        "type": "object"
      }
    }
  },
  "info": {
    "title": "Event",
    "version": "1.0.0"
  },
  "openapi": "3.0.0",
  "paths": {}
}

After terraform apply, you should be able to see the terraform managed shareable events in AWS console.

Some important gotchas when using this method:

  1. If you add events using terraform using this method, any events added from the console would be lost.
  2. The schema registry lambda-testevent-schemas is a special registry & must NOT be managed using terraform as it may disrupt other lambda functions' events created outside the scope of this terraform module.
  3. It is required for the lambda-testevent-schemas to be available beforehand. You can either have a check to create this registry before the module is applied or else create any random shareable event for any random lambda function. This needs to be done once per region per account.
  4. If you face difficulties creating the json schema for your lambda, you can for once create the events from console and then copy the json from EventBridge schema registry.

4 Comments

I couldn't get it to work yet, probably due to the order of resource creations.. I could create two tests though: locals { my_lambda_shared_events_schema = <<EOF { "openapi": "3.0.0", "info": { "version": "1.0.0", "title": "Event" }, "paths": {}, "components": { "schemas": { "Event": { "type": "object" } }, "examples": { "empty-event": { "value": {} }, "hello-world-event": { "value": { "key1": "value1", "key2": "value2", "key3": "value3" } } } } } EOF }
@Meike this is working fine for our solution
This solution does not work, I get an error ResourceNotFound "Registry with name lambda-testevent-schemas does not exist".
@GQuintana did you check the last section of my answer? That registry will be created when first shareable test event is created manually

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.