Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference a dynamic lambda ARN in AWS Step Functions written in the Serverless Framework?

EDIT: Just found this https://github.com/serverless-operations/serverless-step-functions/issues/209 Basically that example, but instead of hardcoding the ARN, I want to use a input variable, if that makes sense.

Here's the context:

Using Serverless' error destinations, an error payload is sent to SQS, which triggers a lambda that starts a state machine to perform a retry.

For example, if Lambda A fails, the failure is sent to SQS -> Lambda B which triggers a state machine to retry Lambda A.

I'm defining my state machine in Serverless.yml like so (This is what I've been trying so far):

stepFunctions:
  stateMachines:
    MyStateMachine:
      name: RetryLambdaMachine
      definition:
        Comment: Example to test retries
        StartAt: StepOne
        States:
          StepOne:
            Type: Task
            Resource: arn:aws:states:::lambda:invoke
            Parameters:
            - FunctionName.$: $$.lambdaArn 
            #### ^^ This is where I need Lambda A to be referenced ####
            Retry:
            - ErrorEquals: 
              - States.ALL
              MaxAttempts: 2
            Catch:
            - ErrorEquals: ["States.ALL"]
              Next: CatchAllFallback
            End: true
          CatchAllFallback:
            Type: Task
            Resource: 
              Fn::GetAtt: [lambda_c, Arn]
            End: true

Here is the error I get when I run serverless deploy:

Error: The CloudFormation template is invalid: [/Resources/RetryLambdasMachineRole/Type/Policies/0/PolicyDocument/Statement/0/Resource/0] 'null' values are not allowed in templates

How do I reference Lambda A as a variable? I know that the error attributes will contain Lambda A's ARN, but how do I pass that to the state machine for the retry step?

Note: Lambda A is NOT defined within this Serverless.yml, it could come from anywhere. The ARN will be passed in as part of the error event coming from SQS, as stated above.

like image 365
EnterPassword Avatar asked Sep 10 '25 15:09

EnterPassword


1 Answers

I found out that my original code was actually pretty close. The only issue was that there was a hyphen in front of the FunctionName key and and extra '$' on lambdaArn.

    States:
      StepOne:
        Type: Task
        Resource: arn:aws:states:::lambda:invoke
        Parameters:
          FunctionName.$: $.lambdaArn

This allows the lambda to be passed in as a dynamic resource.

like image 200
EnterPassword Avatar answered Sep 13 '25 05:09

EnterPassword