简体   繁体   English

基于相同的任务定义但使用不同的环境变量运行多个ECS任务

[英]Running multiple ECS tasks based on same task definition but with different environment variables

I have a task to process large amount of data, so data is batched into lots of parts. 我有一个处理大量数据的任务,因此数据被分批到很多部分。 I have written the task definition for this type of work but now I know only how to set up them manually via registering multiple task definitions for each env. 我已经为这种类型的工作编写了任务定义,但现在我只知道如何通过为每个env注册多个任务定义来手动设置它们。

sample where each task has its own env BATCH_ID 示例每个任务都有自己的env BATCH_ID

  aws ecs register-task-definition --cli-input-json file://taskdef1.json  
  aws ecs run-task --cluster $cluster --task-definition process_data_1

  aws ecs register-task-definition --cli-input-json file://taskdef2.json  
  aws ecs run-task --cluster $cluster --task-definition process_data_2

It would be even nice to have some .manifest file of all task arns put for cluster. 将所有任务arns的一些.manifest文件放入群集中会更好。

Is there someway to run multiple similar ECS tasks with different env params in more elegant way then creating enormous amount of different taskdefs files? 有没有办法以更优雅的方式运行具有不同env参数的多个类似ECS任务,然后创建大量不同的taskdefs文件?

Thanks for any help and suggestions 感谢您的任何帮助和建议

You can override environmental variable when you run the task with --overrides flag. 使用--overrides标志运行任务时,可以覆盖环境变量。 I use this all the time; 我经常用这个; you can either override an existing environmental variable (defined in task definition) or simply add a new one. 您可以覆盖现有的环境变量(在任务定义中定义),也可以只添加一个新变量。 --overrides accepts only JSON (no shorthand syntax); --overrides只接受JSON(没有简写语法); in your case, it would look like: 在你的情况下,它看起来像:

{
  "containerOverrides": [
    {
      "name": "containerNameFromTaskDefinition",
      "environment": [
        {
          "name": "BATCH_ID",
          "value": "sampleBatchId"
        }
      ]
    }
  ]
}

and the command: 和命令:

aws ecs run-task --cluster $cluster --task-definition process_data_1 --overrides {"containerOverrides":[...]} 

You can use --overrides to override even more things of course: https://docs.aws.amazon.com/cli/latest/reference/ecs/run-task.html 您可以使用--overrides来覆盖更多的东西: https--overrides

I may be misunderstanding the problem here, but could you just put the variable in your cli input .json and use sed to swap out the batch ID before you run the task? 我可能在这里误解了这个问题,但你可以把变量放在你的cli输入.json中,并在运行任务之前使用sed交换批处理ID吗? I've done this for application deployments when I need to swap out env vars. 当我需要换掉env变种时,我已经为应用程序部署做了这个。

First, put the variable you want to replace in your taskdef template file (like %BATCH_ID%) 首先,将要替换的变量放在taskdef模板文件中(如%BATCH_ID%)

Then do something like this: (NOTE: Bash script is off the top of my head and may need fixing) 然后做这样的事情:(注意:Bash脚本是我的头顶,可能需要修复)

sed 's/%BATCH_ID%/$BATCH_ID' generic_taskdef.json > process_data_$BATCH_ID.json 
aws ecs register-task-definition --cli-input-json file://process_data_$BATCH_ID.json
aws ecs run-task --cluster $cluster --task-definition process_data_$BATCH_ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用不同的端口在一台主机中基于相同任务定义运行多个ECS任务 - Running multiple ECS tasks based on same task definitions in one host, using different ports 从 Terraform 输入变量中获取 ECS 任务定义环境变量 - Take ECS Task Definition environment variables from Terraform input variables 如何在Node.js中访问ECS任务定义环境变量? - How to access ecs task definition environment variables in nodejs? 如何为 AWS ECS 任务定义提供环境变量? - how to provide environment variables to AWS ECS task definition? 如何使用相同的 ECS 任务定义将变量传递给 Docker 容器以驱动多个环境 - How to pass variables through to Docker container using the same ECS task definition to drive multiple environments 如何为任务提供不同环境变量ECS Terraform - How to provide tasks with different environment variables ECS Terraform AWS ecs任务定义变量 - AWS ecs task definition variables 同一 ecs 服务上的多个任务 - Multiple tasks on the same ecs service 使用 CloudFormation 在 ECS 服务上定义多个任务 - Multiple Tasks Definition on ECS Service using CloudFormation 如何通过ECS CLI和ECS任务指定敏感的环境变量? - How to specify sensitive environment variables with ECS CLI and ECS tasks?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM