【问题标题】:ws ec2 create-launch-template-version passing variables into json arrayaws ec2 create-launch-template-version 将变量传递到 json 数组
【发布时间】:2022-06-13 09:33:27
【问题描述】:

我的笔记本电脑上有 4 个环境变量,ami_id、instance_type、key_name 和 security_group_ids。我正在尝试使用这些变量创建启动模板版本,但我不知道如何将它们正确传递到 JSON 数组中

aws ec2 create-launch-template-version --launch-template-id lt-xxx --launch-template-data '{"ImageId":"$ami_id", "InstanceType": "$instance_type", "KeyName": "$key_name", "SecurityGroupIds": ["$security_group_ids"]}'

An error occurred (InvalidAMIID.Malformed) when calling the CreateLaunchTemplateVersion operation: The image ID '$ami_id' is not valid. The expected format is ami-xxxxxxxx or ami-xxxxxxxxxxxxxxxxx.

【问题讨论】:

标签: json bash variables aws-cli


【解决方案1】:

使用 here-document 允许您在扩展 shell 变量的同时将一些可读文本输入变量,如下所示:

#!/bin/sh

ami_id=ami1234
instance_type=t3.nano
key_name=key1
security_group_ids=sg123,sg456

template_data=$(cat <<EOF
{
  "ImageId":"$ami_id",
  "InstanceType": "$instance_type",
  "KeyName": "$key_name",
  "SecurityGroupIds": ["$security_group_ids"]
}
EOF
)

echo "$template_data"

然后您可以使用jq 测试 JSON 语法:

./template.sh  | jq -c
{"ImageId":"ami1234","InstanceType":"t3.nano","KeyName":"key1","SecurityGroupIds":["sg123,sg456"]}

【讨论】:

    猜你喜欢
    • 2022-12-27
    • 1970-01-01
    • 2020-07-24
    • 2016-11-07
    • 2012-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-02
    相关资源
    最近更新 更多