'Pass date or cron schedule from cloud watch events to step function input

Is there a way I can pass a date or a cron schedule as an input to my state function - which is getting called by a cloud watch event? The cloud watch event runs on a cron schedule and I would like to pass that dynamically on a daily basis to the step function

For example:

This gives some static input, but I want to give each day's date as input

resource "aws_cloudwatch_event_target" "target" {

rule = aws_cloudwatch_event_rule.samplerule.id
arn = aws_sfn_state_machine.samplemachine.id
role_arn = aws_iam_role.iam_for_sfn.arn
input = <<EOF
{
  "operand1": "3",
  "operand2": "5",
  "operator": "add"
}
EOF
}


Solution 1:[1]

The input to your Lambda function from a Scheduled Event looks something like this:

{
 "id": "53dc4d37-cffa-4f76-80c9-8b7d4a4d2eaa",
 "detail-type": "Scheduled Event",
 "source": "aws.events",
 "account": "123456789012",
 "time": "2019-10-08T16:53:06Z",
 "region": "us-east-1",
 "resources": [ "arn:aws:events:us-east-1:123456789012:rule/MyScheduledRule" ],
 "detail": {}
}

Using your choice of programming language, you can extract the time value and convert it from a string to a date resource/object (depending on your language). From that time, you can get the data components you are looking for.

IMPORTANT: All scheduled events use UTC time zone and the minimum precision for schedules is 1 minute: https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html

Solution 2:[2]

An alternative solution could be to use the global access to the context object, as explained here to get the execution start time of the step functions state machine.

So you can send it through your different states of your state machine like this:

"mystep1": {
  "Type": "task",
  "Parameters": {
    "StartTime.$": "$$.Execution.StartTime"
 }
}

Make sure to use the double $ to tell Cloudformation that you're using the global access to the context object.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Simon
Solution 2