Search code examples
amazon-web-servicesaws-lambdaserverless-frameworkaws-fargateaws-step-functions

Serverless Steps Functions: how to pass data from Lambda to ECS Fargate task


I am trying to configure a simple multi-step job using AWS Steps Functions with the Serverless Framework which involves as a first step a lambda generating json output and as a second step a Fargate Task which should receive the json as input.

Basically the lambda generates the following output:

{
  'payload': 'Some payload as a string'
}

I need to pass this output as input to the Fargate task, as environment variable or any other method but I can't understand which syntax should I use. I've tried the following:

stepFunctions:
  stateMachines:
    MyStateMachine:
      name: MyStateMachine
      loggingConfig:
        level: ALL
        includeExecutionData: true
        destinations:
          - Fn::GetAtt: [StateMachineLogGroup, Arn]
      definition:
        StartAt: LambdaStep
        States:
          LambdaStep:
            Type: Task
            Resource:
              Fn::GetAtt: [lambda_step, Arn]
            ResultPath: $
            Next: FargateStep

          FargateStep:
            Type: Task
            Resource: arn:aws:states:::ecs:runTask.sync
            Parameters:
              Cluster: "#{ECSCluster}"
              TaskDefinition: "#{FargateTaskDefinition}"
              LaunchType: FARGATE
              NetworkConfiguration:
                AwsvpcConfiguration:
                  Subnets:
                    - "#{PublicSubnetOne}"
                    - "#{PublicSubnetTwo}"
                  AssignPublicIp: ENABLED
              Overrides:
                ContainerOverrides:
                  - Name: my-fargate-container
                    Environment:
                      - Name: LAMBDA_RESULT
                        Value: $.payload
            Next: Done

          Done:
            Type: Succeed

But in the container logs the environment variable LAMBDA_RESULT is simply set to $.payload meaning that the json path syntax is not resolved from the input.

I've also tried this

Overrides:
  ContainerOverrides:
    - Name: my-fargate-container
      Environment:
        - Name: LAMBDA_RESULT
          Value: $$.payload

and this

FargateStep:
  Type: Task
  Resource: arn:aws:states:::ecs:runTask.sync
  InputPath: $.payload # <-- added this
  Parameters:
    Cluster: "#{ECSCluster}"
    TaskDefinition: "#{FargateTaskDefinition}"
    LaunchType: FARGATE
    NetworkConfiguration:
      AwsvpcConfiguration:
        Subnets:
          - "#{PublicSubnetOne}"
          - "#{PublicSubnetTwo}"
        AssignPublicIp: ENABLED
    Overrides:
      ContainerOverrides:
        - Name: my-fargate-container
          Environment:
            - Name: LAMBDA_RESULT
              Value: $.payload
  Next: Done

No one worked. Any working example on how to properly pass data from Lambda to Fargate task using Step Functions?


Solution

  • I think the syntax should be:

          ContainerOverrides:
            - Name: my-fargate-container
              Environment:
                - Name: LAMBDA_RESULT
                  'Value.$': $.payload