将多个参数从外部文件传递到云信息模板,并使用ref值

Dwa*_*ior 3 amazon-web-services aws-cloudformation aws-cli

尝试使用下面的cli命令创建一个cloudformation堆栈时出现以下错误.

aws cloudformation create-stack --stack-name subodh-local-stack --template-url s3URL/template.json --parameters s3URL/params.json
Run Code Online (Sandbox Code Playgroud)

错误:awscli.argprocess.ParamError:解析参数'--parameters'时出错:无法检索https://s3.amazonaws.com/ //params.json:收到非200状态代码403 2017-08-18 01: 32:31,309 - MainThread - awscli.clidriver - DEBUG - 以rc 255退出

template.json:

{
    "AWSTemplateFormatVersion": "2010-09-09",

    "Resources": {
        "Type": "AWS::Lambda::Function",
        "Properties": {
            "FunctionName": {
                "Ref": "LambdaFunctionName"
            },
            "Handler": {
                "Ref": "LambdaHandler"
            },
            "Role": {
                "Ref": "LambdaExecutionRoleArn"
            },
            "Code": {
                "S3Bucket": {
                    "Ref": "LambdaSourceBucket"
                },
                "S3Key": {
                    "Ref": "LambdaSourceKey"
                }
            },
            "SubnetID": {
                "Ref": "LambdaSubnetID"
            },
            "Runtime": "nodejs4.3",
            "Timeout": "25",
            "MemorySize": "128",
            "VpcConfig": "vpc-2323454f",
            "securityGroupID": "sg-0sdfs17g"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

params.json:

[
        {
            "ParameterKey": "LambdaFunctionName",
            "ParameterValue": "hello-world"
        },
        {
            "ParameterKey": "LambdaHandler",
            "ParameterValue": "index.handler"
        },
        {
            "ParameterKey": "LambdaExecutionRoleArn",
            "ParameterValue": "arn:aws:iam::312345678910:role/LambdaExecuteRole"
        },
        {
            "ParameterKey": "LambdaSourceBucket",
            "ParameterValue": "test-lambda-functions"
        },
        {
            "ParameterKey": "LambdaSourceKey",
            "ParameterValue": "helloworld.zip"
        },
        {
            "ParameterKey": "LambdaSubnetID",
            "ParameterValue": "subnet-1113121f,subnet-fer333ex"
        }
]
Run Code Online (Sandbox Code Playgroud)

将命令更新为:

aws cloudformation create-stack --stack-name test-local-stack --template-body file://c:/cli/aws/template.json --parameters file://c:/cli/aws/params.json
Run Code Online (Sandbox Code Playgroud)

我收到错误

调用CreateStack操作时发生错误(ValidationError):模板格式错误:[/ Resources/Type]资源定义格式错误

我正在尝试使用Ref函数来引用在堆栈创建期间从参数文件传递的参数.

有人可以让我知道我做错了什么吗?

olp*_*lpa 10

首先看,问题与参数无关.错误消息是"模板格式错误:[/ Resources/Type]资源定义格式错误",我认为这是错误的:

"Resources": {
  "Type": "AWS::Lambda::Function",
  ...
}
Run Code Online (Sandbox Code Playgroud)

你想要的是:

"Resources": {
  "YourResourceName": { 
    "Type": "AWS::Lambda::Function",
    ...
  }
}
Run Code Online (Sandbox Code Playgroud)


Dwa*_*ior 9

对于其他任何人如何使用 CF 模板使用外部参数文件并使用 Ref 调用值:

主模板如下所示:

{
"AWSTemplateFormatVersion": "2010-09-09",
"Parameters": {
    "LambdaFunctionName": {
        "Description": "Lambda Function name",
        "Type": "String"
    }
...},
"Resources": {
    "LambdaFunction": {
        "Type": "AWS::Lambda::Function",
        "Properties": {
            "FunctionName": {
                "Ref": "LambdaFunctionName"
            }
        },
    ...}
}
Run Code Online (Sandbox Code Playgroud)

}

参数 json 文件应如下所示:

[
    {
        "ParameterKey":"LambdaFunctionName",
        "ParameterValue":"hello-world"
    },
    ....
]
Run Code Online (Sandbox Code Playgroud)

感谢@olpa 引导我朝着正确的方向前进。

  • 但你没有告诉如何将cf与参数文件关联起来!我猜使用 --parameters 选项。 (3认同)