By using AWS re:Post, you agree to the Terms of Use
/Write containerOverwrites from Lambda to Container/

Write containerOverwrites from Lambda to Container

0

Hello All

I have a step function that is calling a lambda, which does some processing, and gets the id of the job that needs to be processed by the ecs runtask.

What I am trying to do is to pass the job Id as containerOverride so that each run a different Id can be passed to the ecs Run task.

Here is the dummy lambda Output:

Test Event Name
dummyTest

Response
{
  "containerOverrides": " [ { \"name\": \"getFileTask\", \"environment\": [ { \"name\": \"name1\", \"value\": \"123\" }, { \"name\": \"DATE\", \"value\": \"1234-12-12\" }, { \"name\": \"SCRIPT\", \"value\": \"123456\" } ] } ] "
}

Dummy Lambda Code:

def lambda_handler(event, context):
    # TODO implement
    print("Success")
    
    overridden_Text = ' [ { "name": "getFileTask", "environment": [ { "name": "name1", "value": "123" }, { "name": "DATE", "value": "1234-12-12" }, { "name": "SCRIPT", "value": "123456" } ] } ] '
    
    return{
        'containerOverrides': overridden_Text
          

        }

Here is the record when the ECS run task is triggered(TaskStateEntered) from the step function:

{
  "name": "ECS RunTask",
  "input": {
    "containerOverrides": " [ { \"name\": \"getFileTask\", \"environment\": [ { \"name\": \"name1\", \"value\": \"123\" }, { \"name\": \"DATE\", \"value\": \"1234-12-12\" }, { \"name\": \"SCRIPT\", \"value\": \"123456\" } ] } ] "
  },
  "inputDetails": {
    "truncated": false
  }
}

The issue is that when the run task enters the TaskSubmitted stage:

"LastStatus": "PROVISIONING",
        "LaunchType": "FARGATE",
        "Memory": "4096",
        "Overrides": {
          "ContainerOverrides": [
            {
              "Command": [],
              "Environment": [],
              "EnvironmentFiles": [],
              "Name": "getFileTask",
              "ResourceRequirements": []
            }
          ],
          "InferenceAcceleratorOverrides": []
        },
        "PlatformFamily": "Linux",
        "PlatformVersion": "1.4.0",
        "StartedBy": "AWS Step Functions",

For what ever reasons the Environment variable is not being pushed from lambda output to the Container as launch override. Is there something that I am doing incorrectly ?

Thanks

1 Answers
0

Container overrides need to go into the Parameters attributes of your Action in your state machine. Here is an example that will retrieve the "foo" attribute from the input and set it as an environment variable:

{  
   "States":{  
      ...,
      "Manage ECS task":{  
         "Type":"Task",
         "Resource":"arn:aws:states:::ecs:runTask.waitForTaskToken",
         "Parameters":{  
            "LaunchType":"FARGATE",
            "Cluster":"cluster-arn",
            "TaskDefinition":"job-id",
            "Overrides":{  
               "ContainerOverrides":[  
                  {  
                     "Name":"container-name",
                     "Environment":[  
                        {  
                           "Name":"FOO_FROM_INPUT",
                           "Value.$":"$.foo"
                        }
                     ]
                  }
               ]
            }
         }
      }
   }
}

If your input supplies the entire value of the "ContainerOverrides" configuration, then you can do something like this:

{  
   "States":{  
      ...,
      "Manage ECS task":{  
         "Type":"Task",
         "Resource":"arn:aws:states:::ecs:runTask.waitForTaskToken",
         "Parameters":{  
            "LaunchType":"FARGATE",
            "Cluster":"cluster-arn",
            "TaskDefinition":"job-id",
            "Overrides":{  
               "ContainerOverrides.$": "$.containerOverrides"
            }
         }
      }
   }
}

See also Pass State Input as Parameters Using Paths in the Step Functions documentation.

EXPERT
answered 5 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions