如何运行 AWS ECS 任务覆盖环境变量 [英] How to run AWS ECS Task overriding environment variables
问题描述
要通过 CLI 覆盖环境变量,我们可以根据 AWS ECS 命令行参考.
如何在命令行中传递名称值对(结构或 JSON)?
<预><代码>[{ "name" : "NAME", "value" : "123" },{名称":日期",值":1234-12-12"},{名称":脚本",值":123456"}]我正在寻找一种使用 AWS ECS CLI 覆盖上述环境变量的方法.类似的东西:
aws ecs run-task --overrides <<这里只是环境变量>>--任务定义...
文档不清楚.我用谷歌搜索但无济于事.
您必须提供 --overrides
选项.
<代码>{容器覆盖":[{"name": "字符串",命令":[字符串",...],环境": [{"name": "字符串",值":字符串"}...]}...],taskRoleArn":字符串"}
您必须指定容器的 name
以获取环境覆盖,并指定一个 environment
键值对列表.
您可以在参数中指定 JSON 文档,或将文件路径参数传递给任务.我将展示两种方式.
内联传递 JSON
您的命令看起来像这样(填写值 CONTAINER_NAME_FROM_TASK
).
aws ecs run-task --overrides '{ "containerOverrides": [ { "name": "CONTAINER_NAME_FROM_TASK", "environment": [ { "name": "NAME", "value": "123" }, { "name": "DATE", "value": "1234-12-12" }, { "name": "SCRIPT", "value": "123456" } ] } ] }' --task-定义 (...)
虽然看起来很丑,编辑起来很烦人.它也仅适用于 Unix-y 系统,并且需要在 Windows 中转义引号.
或者,您可以将文件路径传递给 AWS CLI,并让它从文件加载您的覆盖 JSON.
传递文件路径参数
创建一个文件,我们称之为overrides.json
,并将相同的JSON放入其中:
<代码>{容器覆盖":[{"name": "CONTAINER_NAME_FROM_TASK",环境": [{"name": "NAME",值":123"}, {名称日期",值":1234-12-12"}, {"name": "脚本",值":123456"}]}]}
然后,假设您的文件位于当前目录:
aws ecs run-task --overrides file://overrides.json --task-definition (..)
如果您的文件在文件系统的其他位置,并且您使用的是 Linux/Unix-y 系统:
aws ecs run-task --overrides file:///path/to/overrides.json --task-definition (..)
如果您的文件位于文件系统中的其他位置,而您是在 Windows 中执行此操作:
aws ecs run-task --overrides file://DRIVE_LETTER:path ooverrides.json --task-definition (..)
To override environment variables via CLI we may use --overrides (structure)
according to AWS ECS Commandline Reference.
How to pass name value pairs (structure or JSON) in command line?
[
{ "name" : "NAME", "value" : "123" },
{ "name" : "DATE", "value" : "1234-12-12" },
{ "name" : "SCRIPT", "value" : "123456" }
]
I'm looking for a way to override above environment variables using AWS ECS CLI. Something like:
aws ecs run-task --overrides <<just environment vars here>> --task-definition ...
Documentation is not clear. I googled but couldn't help.
You have to provide a JSON document as documented under the --overrides
option.
{
"containerOverrides": [
{
"name": "string",
"command": ["string", ...],
"environment": [
{
"name": "string",
"value": "string"
}
...
]
}
...
],
"taskRoleArn": "string"
}
You have to specify the name
of the container to get the environment override, and specify a list of environment
key-value pairs.
You can specify the JSON document in-line with your argument or pass a file path argument to the task. I will show both ways.
Passing JSON in-line
Your command would look like this (fill in the value CONTAINER_NAME_FROM_TASK
).
aws ecs run-task --overrides '{ "containerOverrides": [ { "name": "CONTAINER_NAME_FROM_TASK", "environment": [ { "name": "NAME", "value": "123" }, { "name": "DATE", "value": "1234-12-12" }, { "name": "SCRIPT", "value": "123456" } ] } ] }' --task-definition (...)
That does look rather ugly though, and would be annoying to edit. It also only works on Unix-y systems and would require quote escaping in Windows.
So alternatively, you can pass a file path to the AWS CLI and have it load your override JSON from a file.
Passing a file path argument
Create a file, let's call it overrides.json
, and put the same JSON into it:
{
"containerOverrides": [{
"name": "CONTAINER_NAME_FROM_TASK",
"environment": [{
"name": "NAME",
"value": "123"
}, {
"name": "DATE",
"value": "1234-12-12"
}, {
"name": "SCRIPT",
"value": "123456"
}]
}]
}
Then, assuming your file is in the current directory:
aws ecs run-task --overrides file://overrides.json --task-definition (..)
If your file is elsewhere in the filesystem and you're on a Linux/Unix-y system:
aws ecs run-task --overrides file:///path/to/overrides.json --task-definition (..)
If your file is elsewhere in the filesystem and you're doing this in Windows:
aws ecs run-task --overrides file://DRIVE_LETTER:path ooverrides.json --task-definition (..)
这篇关于如何运行 AWS ECS 任务覆盖环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!