【问题标题】:Cannot use the output of the inner (child ) template in the parent template in cloudformation无法在 cloudformation 的父模板中使用内部(子)模板的输出
【发布时间】:2017-12-06 00:57:13
【问题描述】:

我正在尝试在另一个中使用 cloudformation 堆栈的输出。我查看了一些示例,例如 https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/walkthrough-crossstackref.html

但它非常令人困惑,我无法在我的示例中使用它:

这是我所拥有的:我有一个 beanstalk.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 的名称,并将其分配给使用 beanstalk.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 存储桶的名称。我该怎么做?

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation


    【解决方案1】:

    导出参数需要有一个Export property 才能被其他堆栈访问:

    导出(可选)

    要为跨堆栈引用导出的资源输出的名称。

    注意

    以下限制适用于跨堆栈引用:

    • 对于每个 AWS 账户,导出名称在一个区域内必须是唯一的。
    • 您不能跨区域创建跨堆栈引用。
    • 您可以使用内部函数 Fn::ImportValue 仅导入已在同一区域内导出的值。

    • 对于输出,Export 的 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 有一个good example

    【讨论】:

    • 我在添加导出的第一个输出中更新了我的代码,在主模板输出中我添加了导入以获取值并显示它。仍然无法正常工作并出现以下错误:未找到名为 sampleEnvironment 的导出。有什么想法吗?
    【解决方案2】:

    我已经在 CFN 课程论坛上回复过

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

    您不需要使用导出/导入值,因为您使用的是嵌套堆栈。

    【讨论】:

      猜你喜欢
      • 2014-07-31
      • 1970-01-01
      • 1970-01-01
      • 2014-08-15
      • 2012-12-19
      • 2020-02-08
      • 1970-01-01
      • 1970-01-01
      • 2017-03-04
      相关资源
      最近更新 更多