代码之家  ›  专栏  ›  技术社区  ›  Hamed Minaee

无法在cloudformation的父模板中使用内部(子)模板的输出

  •  0
  • Hamed Minaee  · 技术社区  · 9 年前

    我试图在另一个应用程序中使用cloudformation堆栈的输出。我看了一些例子,比如 https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/walkthrough-crossstackref.html

    这是我的:我有一个豆茎。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"} 
        }
     }
    }
    

    {
    "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 bucket的名称。我该怎么做?

    2 回复  |  直到 9 年前
        1
  •  3
  •   Adrian Cantrill    9 年前

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

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

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

        2
  •  0
  •   Raf    9 年前

    导出参数需要有一个 Export property 其他堆栈可以访问:

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

    笔记

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

    • 对于每个AWS帐户,导出名称在区域内必须是唯一的。
    • 不能跨区域创建跨堆栈引用。
    • 对于输出,导出的Name属性的值不能使用依赖于资源的Ref或GetAtt函数。类似地,ImportValue函数不能包含依赖于资源的Ref或GetAtt函数。

    • 不能修改或删除另一个堆栈引用的输出值。

    Fn::连接函数 .

    所以添加

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

    然后,您可以在必要时通过 Fn::导入值 good example .