【问题标题】:How to refer existing resources如何引用现有资源
【发布时间】:2018-11-11 05:54:20
【问题描述】:

我使用cloudformation模板创建了MyAPI、MyAPIGetMethod、MyAPIDeployment。

现在,我有一个单独的脚本,我将使用它来创建阶段。像这样的东西。该脚本抛出一个错误,它不知道我在DeploymentId 和RestApiId 中使用的是什么资源。确切的错误是Template validation error: Template error: instance of Fn::GetAtt references undefined resource MyAPIDeployment

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Parameters" : {
        "EnvType": {
            "Type": "String",
            "Default": "test",
            "AllowedValues": ["test", "prod"],
            "Description": "Select what stage need to be created"
        }
    },
    "Conditions":{
        "CreateProdStage" : {"Fn::Equals": [{"Ref":"EnvType"}, "prod"]},
        "CreateTestStage" : {"Fn::Equals": [{"Ref":"EnvType"}, "test"]}
    },
    "Resources": {
        "TestStage" : {
            "Type" : "AWS::ApiGateway::Stage",
            "Condition":"CreateTestStage",
            "Properties" : {
                "DeploymentId" : {"Fn::GetAtt" : ["MyAPIDeployment", "Arn"]},
                "Description" : "Test Stage",
                "RestApiId" : {"Fn::GetAtt" : ["MyAPI", "Arn"]},
                "StageName" : "test"
            }
        },
        "ProdStage" : {
            "Type" : "AWS::ApiGateway::Stage",
            "Condition":"CreateProdStage",
            "Properties" : {
                "DeploymentId" : {"Fn::GetAtt" : ["MyAPIDeployment", "Arn"]},
                "Description" : "Prod Stage",
                "RestApiId" : {"Fn::GetAtt" : ["MyAPI", "Arn"]},
                "StageName" : "prod",
                "MethodSettings":[{
                    "CachingEnabled":"true",
                    "HttpMethod":"GET",
                    "ResourcePath":"/",
                    "CacheTtlInSeconds":300,
                    "ThrottlingBurstLimit" : 2000,
                    "ThrottlingRateLimit" : 1000
                }]
            }
        }
    }
}

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation


    【解决方案1】:

    为了更好地理解为什么这不起作用是因为您无法引用 CloudFormation 模板中不属于该模板的资源。由于资源MyAPI、MyAPIGetMethod、MyAPIDeployment 不是创建阶段的模板的一部分,因此您无法引用它们。您只能引用参数、其他资源、条件、映射等,但不能引用在不同堆栈中创建的资源。

    我的建议是在您创建MyAPI、MyAPIGetMethod、MyAPIDeployment 的示例模板上创建阶段,或者使用DeploymentId 和RestApiId 所需的信息创建必要的参数值,然后使用Ref。

    我从您的模板中注意到的另一个错误是 DeploymentId 和 RestApiId 的预期值是对应的 id 而不是 arn,因此请确保使用 id 而不是您尝试在上面的sn-p。另请注意,没有为 AWS::ApiGateway::RestApi、AWS::ApiGateway::RestApi 和 AWS::ApiGateway::RestApi 定义函数 GetAtt。从这个资源返回值的唯一函数是Ref,它会返回它们的ID。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-20
      • 2021-06-19
      • 1970-01-01
      • 1970-01-01
      • 2020-01-23
      • 2011-04-13
      相关资源
      最近更新 更多