1

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
}
aashitvyas
  • 858
  • 2
  • 10
  • 20
  • Why not simply call today() from datetime, or whatever your language's function for getting the current date is? – Oscar De León Mar 27 '21 at 18:48
  • I would want to pass a date that starts the step function and use it for all my step functions- This is mainly because if one step function goes beyond a particular date, we don’t want to use the time and date the particular step function is running and rather the date that started the step function – srinidhi sridharan Mar 28 '21 at 22:40

2 Answers2

1

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.

Douglas Figueroa
  • 675
  • 6
  • 17
0

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

Simon
  • 865
  • 1
  • 5
  • 15
  • I would want to pass a date that starts the step function and use it for all my step functions- This is mainly because if one step function goes beyond a particular date, we don’t want to use the time and date the particular step function is running and rather the date that started the step function – srinidhi sridharan Mar 28 '21 at 22:37