【问题标题】:AWS Cloudformation: Take an AMI as a parameter, otherwise fallback to using a MapAWS Cloudformation:将 AMI 作为参数,否则回退到使用 Map
【发布时间】:2017-04-08 00:45:56
【问题描述】:

我想通过以下方式设置 Cloudformation 模板:

1) 检查用户的参数。如果提供,请使用它。

2) 如果没有提供参数,则回退到使用如下映射:

  AWSRegionArch2AMI:
    eu-central-1:
      HVM64: ami-d11dc4ff
    us-east-1:
      HVM64: ami-a13749a2
    us-west-1:
      HVM64: ami-fdd8428a

这里的重要部分是每个区域的默认值;我需要跨区域使用模板,所以我需要有特定于区域的默认值。

有谁知道这样做的好方法吗?

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation


    【解决方案1】:

    您可以使用Conditions 完成此操作。基本上,您可以根据您定义的检查定义一个真或假的变量,然后您可以根据该值创建模板派生。

    以下是使用该 AMI 选择方法创建单个 EC2 实例的示例模板:

    {
        "AWSTemplateFormatVersion": "2010-09-09",
        "Description": "Example mostly pulled from http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/conditions-sample-templates.html",
        "Mappings": {
            "AWSRegionArch2AMI": {
                "eu-central-1": {"HVM64": "ami-d11dc4ff"},
                "us-east-1": {"HVM64": "ami-a13749a2"},
                "us-west-1": {"HVM64": "ami-fdd8428a"}
            }
        },
        "Parameters": {
            "AMI": {
                "Description": "AMI to use.",
                "AllowedPattern": "(ami-[0-9a-f]{8}){0,1}",
                "Default": "",
                "Type": "String"
            }
        },
        "Conditions": {
            "UseDefaultAMI": {
                "Fn::Equals": [
                    {"Ref": "AMI"},
                    ""
                ]
            }
        },
        "Resources": {
            "EC2Instance": {
                "Type": "AWS::EC2::Instance",
                "Properties": {
                    "ImageId": {
                        "Fn::If": [
                            "UseDefaultAMI",
                            {"Fn::FindInMap": [
                                    "AWSRegionArch2AMI",
                                    {"Ref": "AWS::Region"},
                                    "HVM64"
                            ]},
                            {"Ref": "AMI"}
                        ]
                    },
                    "InstanceType": "m3.medium"
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-12-10
      • 1970-01-01
      • 1970-01-01
      • 2020-02-13
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多