admin管理员组

文章数量:1126345

Basically I'm building a integration that runs a ECR image on ECS via a AWS batch job definition. This piece of the code works atm, and we're now moving to running the job definitions via a AWS step function.

Here is a copy of my current state machine (with some edits for the privacy of the companies involved in this code)

{
  "Comment": "A description of my state machine",
  "StartAt": "xxxxxx Job",
  "QueryLanguage": "JSONata",
  "States": {
    "blahblah Job": {
      "Type": "Task",
      "Resource": "arn:aws:states:::batch:submitJob.sync",
      "Arguments": {
        "JobName": "blahJob",
        "JobDefinition": "job-def-arn:69",
        "JobQueue": "job-queue"
      },
      "Next": "bleg Job",
      "Assign": {
        "apiCall": "$states.input.apiCall",
        "wid": "$states.input.wid"
      }
    },
    "bleg Job": {
      "Type": "Task",
      "Resource": "arn:aws:states:::batch:submitJob.sync",
      "Arguments": {
        "JobDefinition": "job-arn:69",
        "JobQueue": "job-queue",
        "JobName": "blegJob"
      },
      "End": true,
      "Assign": {
        "apiCall": "$states.input.apiCall",
        "person": "$states.input.person",
        "endpoint": "$states.input.endpoint"
      }
    }
  }
}

I've been trying a couple of different methods. My ECR images are an alpine linux image running a nodejs code. The ECR repo and the Batch job are all created via Terraform. The above assign block did not work correctly.

Here's a copy of the terraform job definition that is being run via the step functions. You can see I've tried setting the enviroment variables via the terraform as well, but that's not been working for me either.

Let me know if there's any more info I can shed on my setup, or if there are any techniques or tricks I should try.

resource "aws_batch_job_definition" "managed" {
  for_each = var.manage_job_definitions ? var.job_definitions : {}

  name = "${var.app_name}-${var.environment}-${each.key}"
  type = "container"
  platform_capabilities = [
    "FARGATE",
  ]

  container_properties = <<CONTAINER_PROPERTIES
{
  "fargatePlatformConfiguration": {
    "platformVersion": "LATEST"
  },
  "executionRoleArn": "${aws_iam_role.ecs_task_execution_role.arn}",
  "jobRoleArn": "${local.ecs_job_role}",
  "mountPoints": ${jsonencode(var.mount_points)},
  "volumes": ${jsonencode(var.volumes)},
  "containers": [
      {
        "name": "xxxxx",
        "image": "${xxxxx}",
        "environment": [
          {
            "name": "apiCall",
            "value": "{% $states.input.apiCall $}"
          },
          {
            "name": "wid",
            "value": "{% $states.input.xxx $}"
          }
        ],
        "environment": ${jsonencode(concat(localmon_env_vars, each.value.environment_variables))},
        "essential": true,
        "resourceRequirements": [
          {"type": "VCPU", "value": "0.25"},
          {"type": "MEMORY", "value": "512"}
        ]
      },
      {
        "name": "xxxxxImage",
        "image": "${var.xxxx_image}",
        "environment": [
          {
            "name": "apiCall",
            "value": "{% $states.input.apiCall $}"
          },
          {
            "name": "wid",
            "value": "{% $states.input.wid $}"
          }
        ],
        "essential": true,
        "resourceRequirements": [
          {"type": "VCPU", "value": "0.25"},
          {"type": "MEMORY", "value": "512"}
        ]
      },
      {
        "name": "dataImage",
        "image": "${var.node_server_image}",
        "environment": ${jsonencode(concat(localmon_env_vars, each.value.environment_variables))},
        "essential": true,
        "resourceRequirements": [
          {"type": "VCPU", "value": "0.25"},
          {"type": "MEMORY", "value": "512"}
        ]
      }
  ]
}
CONTAINER_PROPERTIES
}

Basically I'm building a integration that runs a ECR image on ECS via a AWS batch job definition. This piece of the code works atm, and we're now moving to running the job definitions via a AWS step function.

Here is a copy of my current state machine (with some edits for the privacy of the companies involved in this code)

{
  "Comment": "A description of my state machine",
  "StartAt": "xxxxxx Job",
  "QueryLanguage": "JSONata",
  "States": {
    "blahblah Job": {
      "Type": "Task",
      "Resource": "arn:aws:states:::batch:submitJob.sync",
      "Arguments": {
        "JobName": "blahJob",
        "JobDefinition": "job-def-arn:69",
        "JobQueue": "job-queue"
      },
      "Next": "bleg Job",
      "Assign": {
        "apiCall": "$states.input.apiCall",
        "wid": "$states.input.wid"
      }
    },
    "bleg Job": {
      "Type": "Task",
      "Resource": "arn:aws:states:::batch:submitJob.sync",
      "Arguments": {
        "JobDefinition": "job-arn:69",
        "JobQueue": "job-queue",
        "JobName": "blegJob"
      },
      "End": true,
      "Assign": {
        "apiCall": "$states.input.apiCall",
        "person": "$states.input.person",
        "endpoint": "$states.input.endpoint"
      }
    }
  }
}

I've been trying a couple of different methods. My ECR images are an alpine linux image running a nodejs code. The ECR repo and the Batch job are all created via Terraform. The above assign block did not work correctly.

Here's a copy of the terraform job definition that is being run via the step functions. You can see I've tried setting the enviroment variables via the terraform as well, but that's not been working for me either.

Let me know if there's any more info I can shed on my setup, or if there are any techniques or tricks I should try.

resource "aws_batch_job_definition" "managed" {
  for_each = var.manage_job_definitions ? var.job_definitions : {}

  name = "${var.app_name}-${var.environment}-${each.key}"
  type = "container"
  platform_capabilities = [
    "FARGATE",
  ]

  container_properties = <<CONTAINER_PROPERTIES
{
  "fargatePlatformConfiguration": {
    "platformVersion": "LATEST"
  },
  "executionRoleArn": "${aws_iam_role.ecs_task_execution_role.arn}",
  "jobRoleArn": "${local.ecs_job_role}",
  "mountPoints": ${jsonencode(var.mount_points)},
  "volumes": ${jsonencode(var.volumes)},
  "containers": [
      {
        "name": "xxxxx",
        "image": "${xxxxx}",
        "environment": [
          {
            "name": "apiCall",
            "value": "{% $states.input.apiCall $}"
          },
          {
            "name": "wid",
            "value": "{% $states.input.xxx $}"
          }
        ],
        "environment": ${jsonencode(concat(local.common_env_vars, each.value.environment_variables))},
        "essential": true,
        "resourceRequirements": [
          {"type": "VCPU", "value": "0.25"},
          {"type": "MEMORY", "value": "512"}
        ]
      },
      {
        "name": "xxxxxImage",
        "image": "${var.xxxx_image}",
        "environment": [
          {
            "name": "apiCall",
            "value": "{% $states.input.apiCall $}"
          },
          {
            "name": "wid",
            "value": "{% $states.input.wid $}"
          }
        ],
        "essential": true,
        "resourceRequirements": [
          {"type": "VCPU", "value": "0.25"},
          {"type": "MEMORY", "value": "512"}
        ]
      },
      {
        "name": "dataImage",
        "image": "${var.node_server_image}",
        "environment": ${jsonencode(concat(local.common_env_vars, each.value.environment_variables))},
        "essential": true,
        "resourceRequirements": [
          {"type": "VCPU", "value": "0.25"},
          {"type": "MEMORY", "value": "512"}
        ]
      }
  ]
}
CONTAINER_PROPERTIES
}
Share Improve this question asked Jan 8 at 22:50 gatorpatchgatorpatch 194 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I ended up figuring it out thanks to some people over on reddit. You just need to add

"ContainerOverrides": {
  "Environment": [
    {
      "Name": "var1",
      "Value": "{% $states.input.value1 %}"
    },
    {
      "Name": "var2",
      "Value": "{% $states.input.value2 %}"
    }
  ]
}

to whichever container you are trying to get the variables into on the State Machine/Step Function definition, under the Arguments field.

本文标签: