【问题标题】:Passing multiple parameters from external file to cloudformation template and using values with ref将多个参数从外部文件传递到 cloudformation 模板并使用带有 ref 的值
【发布时间】:2018-01-26 17:07:50
【问题描述】:

尝试使用以下 cli 命令创建 cloudformation 堆栈时出现以下错误。

aws cloudformation create-stack --stack-name subodh-local-stack --template-url s3URL/template.json --parameters s3URL/params.json

错误:awscli.argprocess.ParamError:解析参数“--parameters”时出错:无法检索https://s3.amazonaws.com///params.json:收到非200状态码403 2017-08-18 01:32:31,309 - MainThread - awscli.clidriver - 调试 - 使用 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"
        }
    }
}

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"
        }
]

将命令更新为:

aws cloudformation create-stack --stack-name test-local-stack --template-body file://c:/cli/aws/template.json --parameters file://c:/cli/aws/params.json

我得到错误

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

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

谁能告诉我我做错了什么?

【问题讨论】:

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


    【解决方案1】:

    乍一看,问题与参数无关。错误信息是“模板格式错误:[/Resources/Type]资源定义格式错误”,我认为这是错误的:

    "Resources": {
      "Type": "AWS::Lambda::Function",
      ...
    }
    

    你想要的是:

    "Resources": {
      "YourResourceName": { 
        "Type": "AWS::Lambda::Function",
        ...
      }
    }
    

    【讨论】:

    • 我更新了同样的内容,使用 "Resources": {"LambdaFunction":{...}} 并得到以下错误 - "ClientError: An error occurred (ValidationError) when call the CreateStack operation:模板格式错误:模板资源块中未解决的资源依赖项 [LambdaSourceBucket、LambdaSourceKey、LambdaHandler、LambdaSubnetId、LambdaFunctionName、LambdaExecutionRoleArn] 2017-08-18 09:54:00,982 - MainThread - awscli.clidriver - DEBUG - Exiting with rc 255
    • 确实如此。您还需要在模板的Parameters 对象中声明输入参数。见docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/…
    • 感谢您的提示;我不得不在字里行间阅读以理解它的含义。我的问题现在已经解决了。
    【解决方案2】:

    对于其他正在研究如何将外部参数文件与 CF 模板一起使用并使用 Ref 调用值的人:

    主模板如下所示:

    {
    "AWSTemplateFormatVersion": "2010-09-09",
    "Parameters": {
        "LambdaFunctionName": {
            "Description": "Lambda Function name",
            "Type": "String"
        }
    ...},
    "Resources": {
        "LambdaFunction": {
            "Type": "AWS::Lambda::Function",
            "Properties": {
                "FunctionName": {
                    "Ref": "LambdaFunctionName"
                }
            },
        ...}
    }
    

    }

    参数json文件应该是这样的:

    [
        {
            "ParameterKey":"LambdaFunctionName",
            "ParameterValue":"hello-world"
        },
        ....
    ]
    

    感谢@olpa 为我指引正确的方向。

    【讨论】:

    • 但是您没有告诉如何将cf与参数文件相关联!我猜使用 --parameters 选项。
    • 嗨 Apurva,是的,这是正确的。它在原始帖子中。
    猜你喜欢
    • 2021-05-28
    • 1970-01-01
    • 2020-06-02
    • 2018-09-03
    • 2020-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多