Dynamic AWS Sam Schedule Event Input param

Viewed 1166

We are automating a lambda via SAM to run on a Schedule Event. We use YAML but we are unable to work out how to use !Sub to make the Input be dynamic.

If you read the sam documentation it says that Input needs to be a JSON formatted string

The following code works for us:

Events:
    Event1:
      Type: Schedule
      Properties:
        Schedule: rate(1 minute)
        Input: >-
          {
            "sqsUrl": "https://sqs.12344.url",
            "snsArn": "arn:val"
          }

But we need to insert dynamic params into the Input like so:

Events:
    Event1:
      Type: Schedule
      Properties:
        Schedule: rate(1 minute)
        Input: >-
          {
            "sqsUrl": "https://sqs.${AWS::AccountId}.url",
            "snsArn": "arn:val"
          }

We have tried to do this in multiple ways, using a !Sub but the deployment always fails saying that it needs to be valid JSON.

What is the correct way to make this JSON string use variables?

Thanks, Mark

2 Answers

So, you should wrap all Input value (in your case this is json-string and of course it should be wrapped with some quotes) with the !Sub function.

Then, it will look like:

Input:
  Fn::Sub: '{"sqsUrl": "https://sqs.${AWS::AccountId}.url","snsArn": "arn:val"}'
Related