【问题标题】:AWS CLI- What is the syntax for assigning multiple values on a parameterAWS CLI-在参数上分配多个值的语法是什么
【发布时间】:2020-11-21 20:46:39
【问题描述】:

例如,我有一个要求这些参数的 CF 模板

----- cftemplate.yaml -----
...
Parameters:
  **Subnet:
    Description: Subnet for the Instance
    Type: 'AWS::EC2::Subnet::Id'
  SecurityGroups: 
    Description: Security Group for Instance
    Type: 'List<AWS::EC2::SecurityGroup::Id>'**
...
Resources:
 EC2Instance:
   Type: AWS::EC2::Instance
   Properties:
...
    **SubnetId: !Ref Subnet
    SecurityGroupIds: !Ref SecurityGroups**
...
----- cftemplate.yaml -----

为了部署堆栈,我使用以下命令:

aws cloudformation create-stack --stack-name StackName --template-body file://cftemplate.yaml --parameters file://params.json

params.json 包含的位置:

----- params.json ----- 
[
        {
            "ParameterKey":"Subnet",
            "ParameterValue":"subnet-11111111"
        },
        {
            "ParameterKey":"SecurityGroups",
            "ParameterValue":"sg-111111111",
            "ParameterValue":"sg-222222222"
        } 
]
----- params.json -----

现在,我的目标是消除 .json 文件的使用。有谁知道应该达到与上述命令相同效果的命令的简写语法?似乎在网上的文档中找不到这个。提前致谢!

【问题讨论】:

    标签: json amazon-web-services yaml amazon-cloudformation aws-cli


    【解决方案1】:

    命令行等效项将是(为了澄清而重新格式化):

    aws cloudformation create-stack \
        --stack-name StackName \
        --template-body file://cftemplate.yaml \
        --parameters ParameterKey=Subnet,ParameterValue=subnet-11111111 ParameterKey=SecurityGroups,ParameterValue=sg-111111111\\,sg-222222222
    

    在上面注意空格和逗号很重要。

    我使用自己的参数和沙盒帐户验证了命令:

    aws cloudformation create-stack --stack-name StackName --template-body file://instance.yaml --parameters ParameterKey=Subnet,ParameterValue=subnet-0ae6ce0f9bbf52251 ParameterKey=SecurityGroups,ParameterValue=sg-06d2a3e9c8aa99620\\,sg-004d23d188ec1146f
    

    这是正确的并导致开始部署堆栈的过程:

    {
        "StackId": "arn:aws:cloudformation:us-east-1:xxxxxx:stack/StackName/61fbacd0-d3b0-11ea-970a-0ad23187ddb2"
    }
    

    【讨论】:

    • @darren 嗨。不知道你是什么意思?
    • 使用命令:aws cloudformation create-stack --stack-name StackName --template-body file://cftemplate.yaml --parameters ParameterKey=Subnet,ParameterValue=subnet-11111111 ParameterKey=SecurityGroups,ParameterValue=sg-111111111,sg-222222222 将导致:Parameter validation failed: Invalid type for parameter Parameters[4].ParameterValue, value: ['sg-111111111', 'sg-222222222'], type: &lt;class 'list'&gt;, valid types: &lt;class 'str'&gt; 不过,我认为我们只需要为此找到正确的语法。
    • 根据操作系统,您可能需要将参数放在单引号或双引号中。
    • @Marcin 酷!!!你的命令有效!我第一次测试时出错了,因为我使用了很多参数。非常感谢您的复制。赞!!
    • 还发现这个语法也可以接受:aws cloudformation create-stack --stack-name StackName --template-body file://cftemplate.yaml --parameters ParameterKey=Subnet,ParameterValue=subnet-11111111 ParameterKey=SecurityGroups,ParameterValue='sg-111111111,sg-222222222'
    【解决方案2】:

    来自 cli 文档,

    $ aws cloudformation create-stack help
    
    ...
    
    "--parameters" (list)
    
       A list of "Parameter" structures that specify input parameters for
       the stack. For more information, see the Parameter data type.
    
    Shorthand Syntax:
    
       ParameterKey=string,ParameterValue=string,UsePreviousValue=boolean,ResolvedValue=string ...
    
    JSON Syntax:
    
       [
         {
           "ParameterKey": "string",
           "ParameterValue": "string",
           "UsePreviousValue": true|false,
           "ResolvedValue": "string"
         }
         ...
       ]
    
    ...
    
    

    列表元素由空格分隔。

    【讨论】:

      【解决方案3】:

      你可能想看看rain cli。它是由 cloudformation 团队的人开发的,它比 aws cli 好得多。

      https://github.com/aws-cloudformation/rain

      【讨论】:

        猜你喜欢
        • 2011-09-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多