没有根据条件生成输出

时间:2019-09-20 08:11:33

标签: amazon-web-services amazon-cloudformation

在我的cf模板中,我定义了一组条件,并且在资源部分中也调用了这些条件,但是当我尝试使用条件生成输出时,这些条件将无法正常工作。

{
    "AWSTemplateFormatVersion": "2010-09-09",

    "Description": "CloudFormation to Deploy EMR clusters",

    "Parameters": {
        "Applications": {
            "Default": "Core Hadoop",
            "Description": "Installed Applications",
            "Type": "String",
            "AllowedValues" : ["Core Hadoop","HBase"],
            "ConstraintDescription": "Must be valid Applications"
        },
        "awsRegion": {
            "Default" : "<my-region>",
            "Description": "awsRegion",
            "Type": "String"
        },
        "Ec2KeyName": {
            "Default": "<my-key.pem>",
            "Description": "Ec2KeyName",
            "Type": "String"
        }
    },
    "Resources": {
        "EMRCluster1": {
            "Type" : "AWS::EMR::Cluster",
            "Condition" : "CH", 
            "Properties" : {
                "Applications" :  [
                    {
                        "Name": "Hadoop"
                    },
                    {
                        "Name": "Hive"
                    },
                    {
                        "Name": "Hue"
                    },
                    {
                      "Name": "Pig"
                    },
                    {
                      "Name": "Mahout"
                    },
                    {
                      "Name": "Tez"
                    }
                ]
            }
        }
        "EMRCluster2": {
            "Type" : "AWS::EMR::Cluster",
            "Condition" : "HB", 
            "Properties" : {
                "Applications": [
                    {
                        "Name": "Hadoop"
                    },
                    {
                        "Name": "Hive"
                    },
                    {
                        "Name": "Hue"
                    },
                    {
                        "Name": "HBase"
                    },
                    {
                        "Name": "Phoenix"
                    },
                    {
                       "Name": "ZooKeeper"
                    }
                ]
            }
        }
    }

    "Conditions" : {
    "CH" : {"Fn::Equals" : [{"Ref" : "Applications"}, "Core Hadoop"]},
    "HB" : {"Fn::Equals" : [{"Ref" : "Applications"}, "HBase"]}
    },
    "Outputs": {
    "MasterPublicDnsName": {
        "Condition" : "CH",
        "Value": {
        "Fn::GetAtt": [
        "EMRCluster1",
        "MasterPublicDNS"
        ]
        },
        "Description": "MasterPublicDNS for cluster"
    },
    "MasterPublicDnsName": {
        "Condition" : "HB",
        "Value": {
        "Fn::GetAtt": [
        "EMRCluster2",
        "MasterPublicDNS"
        ]
        },
        "Description": "MasterPublicDNS for cluster"
    }
    }

预期:如果我在参数中选择“ CH”,则它应提供cluster1 masterdns,如果我选择“ HB”,则应提供cluster2的masterdns

实际:如果我选择“ HB”作为输出的最后一部分,它将给我cluster2的masterdns,但是如果我选择cloudformation中的“ CH”输出部分给了我“找不到输出”。

有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

该问题可能存在,因为两个输出使用相同的名称。由于具有重复键的JSON行为是不确定的,因此实现语言可以选择在这种情况下的行为方式。大概第二次使用MasterPublicDnsName作为输出名称时,您将覆盖名字,这与您看到的行为一致。

您可以选择使用两个不同的名称,但这可能会使跨堆栈引用难以使用,或者在输出的值中使用Fn::If语句:

"Fn::If": [condition_name, value_if_true, value_if_false]

或者您的情况:

"Fn::If": ["CH", {"Fn::GetAtt": ["EMRCluster1", "MasterPublicDNS"]}, {"Fn::GetAtt": ["EMRCluster2", "MasterPublicDNS"]}]

如果您需要两个以上的选项,则需要嵌套Fn::If语句