簡體   English   中英

無法在cloudformation的父模板中使用內部(子)模板的輸出

[英]Cannot use the output of the inner (child ) template in the parent template in cloudformation

我試圖在另一個中使用cloudformation堆棧的輸出。 我看了一些例子,例如https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/walkthrough-crossstackref.html

但這很令人困惑,在我的示例中我無法使其工作:

這是我所擁有的:我有一個beantalk.json模板,並且輸出在資源部分中創建的sampleEnvironment:

{
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
    "sampleApplication": {
        "Type": "AWS::ElasticBeanstalk::Application",
        "Properties": {
            "Description": "AWS Elastic Beanstalk Sample Application",
            "ApplicationName": "app-name-test"
        }
    },
    "sampleApplicationVersion": {
        "Type": "AWS::ElasticBeanstalk::ApplicationVersion",
        "Properties": {
            "ApplicationName": {
                "Ref": "sampleApplication"
            },
            "Description": "AWS ElasticBeanstalk Sample Application Version",
            "SourceBundle": {
                "S3Bucket": "test-war",
                "S3Key": "deployment.war"
            }
        }
    },
    "sampleConfigurationTemplate": {
        "Type": "AWS::ElasticBeanstalk::ConfigurationTemplate",
        "Properties": {
            "ApplicationName": {
                "Ref": "sampleApplication"
            },
            "Description": "AWS ElasticBeanstalk Sample Configuration Template",
            "OptionSettings": [{
                    "Namespace": "aws:autoscaling:asg",
                    "OptionName": "MinSize",
                    "Value": "2"
                },
                {
                    "Namespace": "aws:autoscaling:asg",
                    "OptionName": "MaxSize",
                    "Value": "3"
                },
                {
                    "Namespace": "aws:elasticbeanstalk:environment",
                    "OptionName": "EnvironmentType",
                    "Value": "LoadBalanced"
                }
            ],
            "SolutionStackName": "64bit Amazon Linux 2017.03 v2.6.1 running Tomcat 8 Java 8"
        }
    },
    "sampleEnvironment": {
        "Type": "AWS::ElasticBeanstalk::Environment",
        "Properties": {
            "ApplicationName": {
                "Ref": "sampleApplication"
            },
            "Description": "AWS ElasticBeanstalk Sample Environment",
            "TemplateName": {
                "Ref": "sampleConfigurationTemplate"
            },
            "VersionLabel": {
                "Ref": "sampleApplicationVersion"
            },
            "EnvironmentName": "test-dep-env-name"
        }
    }
},
 "Outputs": {
    "applicationName11": {
       "Description": "The application chosen by user is :",
       "Value": {
        "Ref": "sampleEnvironment"
      },
   "Export" : {
    "Name" : {"Ref": "sampleEnvironment"} 
    }
 }
}

現在我的問題開始了。 我需要引用在beanstalk.json中創建的sampleEnvironment的名稱,並將其分配給使用beantalk.json模板的主模板中資源部分中s3的名稱。 這是我的主要臨時代碼:

{
"Parameters": {
    "appName1": {
        "Description": "enter the app name",
        "Type": "String",
        "Default": "bn-test-jun"
    },
    "appEnv1": {
        "Description": "enter the app name",
        "Type": "String",
        "Default": "bn-test-jun"
    }
},
"Resources": {
    "CodeDeployEC2InstancesStack": {
        "Type": "AWS::CloudFormation::Stack",
        "Properties": {
            "TemplateURL": "https://s3.amazonaws.com/url...../beanstalk.json",
            "TimeoutInMinutes": "60"
        }
    },
    "myS3": {
        "Type": "AWS::S3::Bucket",
        "Properties": {
            "AccessControl": "PublicRead",
            "BucketName": "name of the environment returned as an output sth like Outputs.EnvironmentName"
        }
    }
}
 ,
 "Outputs":{
  "app":{
  "Description": "The application chosen by user is :",
     "Value": {
             "Fn::ImportValue" : "sampleEnvironment" 
     }
   }
 }
  }

現在您看到在bucketName部分中,我被卡住了。 我需要將beanstalk.json中創建的環境的名稱分配給將要創建的s3存儲桶的名稱。 我怎樣才能做到這一點?

我在CFN課程論壇上回答了

https://acloud.guru/forums/aws-advanced-cloudformation/discussion/-KoAxjlT_ZtSAdrlg1lp/cannot_use_the_output_of_the_i

您不需要使用“導出/導入”值,因為您正在使用嵌套堆棧。

export參數需要具有Export屬性 ,其他堆棧才能訪問:

導出(可選)

要為跨堆棧引用導出的資源輸出的名稱。

注意

以下限制適用於跨堆棧引用:

  • 對於每個AWS賬戶,導出名稱在一個區域內必須唯一。
  • 您不能跨區域創建跨堆棧引用。
  • 您可以使用內部函數Fn :: ImportValue僅導入在同一區域內已導出的值。

  • 對於輸出,導出的Name屬性的值不能使用依賴於資源的Ref或GetAtt函數。 同樣,ImportValue函數不能包含依賴於資源的Ref或GetAtt函數。

  • 如果另一個堆棧引用了其中一個輸出,則無法刪除該堆棧。

  • 您不能修改或刪除另一個堆棧引用的輸出值。

您可以使用內部函數來自定義導出的“名稱”值。 以下示例使用Fn :: Join函數

因此添加Export屬性:

"Outputs": {
  "applicationName11": {
    "Description": "The application chosen by user is :",
    "Value": {
        "Ref": "sampleEnvironment"
    },
    "Export" : {
      "Name" : {
        "Fn::Join" : [ "-", [ { "Ref" : "AWS::StackName" }, {"Ref": "something"} ] ]
        }
     }
  }
}

然后,您可以根據需要通過Fn :: ImportValue導入它。 AWS有一個很好的例子

暫無
暫無

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

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