簡體   English   中英

如何引用現有資源

[英]How to refer existing resources

我使用了cloudformation模板來創建MyAPIMyAPIGetMethodMyAPIDeployment

現在,我有一個單獨的腳本,我將用它來創建階段。 像這樣的東西。 該腳本拋出一個錯誤,它不知道我在DeploymentIdRestApiId使用了什么資源。 確切的錯誤是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
                }]
            }
        }
    }
}

為了更好地理解為什么這不起作用是因為您無法在CloudFormation模板中引用不屬於它的資源。 由於資源MyAPIMyAPIGetMethodMyAPIDeployment不是創建階段的模板的一部分,因此您無法引用它們。 您只能引用參數,其他資源,條件,映射等,但不能參考在不同堆棧中創建的資源。

我的建議是在您創建MyAPIMyAPIGetMethodMyAPIDeployment的示例模​​板上創建階段,或者使用DeploymentIdRestApiId值所需的信息創建必要的參數,然后使用Ref

我從你的模板中注意到的另一個錯誤是DeploymentIdRestApiId所期望的值是對應的id而不是arn的,所以請確保使用id而不是你在上面的代碼片段中嘗試的arn。 另請注意,沒有為AWS::ApiGateway::RestApiAWS::ApiGateway::RestApiAWS::ApiGateway::RestApi 從這個資源中返回值的唯一函數是Ref ,它將返回它們的id。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM