【问题标题】:Use cloud formation to create an Ubuntu 16.04 EC2 instance for different regions?使用云形成为不同区域创建一个 Ubuntu 16.04 EC2 实例?
【发布时间】:2017-12-18 09:18:35
【问题描述】:

我想简单地使用 Amazon 的 Cloud Formation 工具来创建一个带有 Ubuntu 16.04 映像的 EC2 实例。

这应该很简单,只是图像 ID 似乎会因机器的区域和架构类型而有很大差异。

例如,这是他们 Lamp 示例中提供的示例代码的一小部分,用于创建运行 Amazon 操作系统的 EC2 实例:

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Parameters": {
        "InstanceType": {
            "Description": "WebServer EC2 instance type",
            "Type": "String",
            "Default": "t2.small",
            "AllowedValues": [
                "t1.micro",
                "t2.nano",
                "t2.micro",
                "t2.small",
                "t2.medium",
                "t2.large",
                "m1.small",
                "m1.medium",
                "m1.large",
                "m1.xlarge",
                "m2.xlarge",
                "m2.2xlarge",
                "m2.4xlarge",
                "m3.medium",
                "m3.large",
                "m3.xlarge",
                "m3.2xlarge",
                "m4.large",
                "m4.xlarge",
                "m4.2xlarge",
                "m4.4xlarge",
                "m4.10xlarge",
                "c1.medium",
                "c1.xlarge",
                "c3.large",
                "c3.xlarge",
                "c3.2xlarge",
                "c3.4xlarge",
                "c3.8xlarge",
                "c4.large",
                "c4.xlarge",
                "c4.2xlarge",
                "c4.4xlarge",
                "c4.8xlarge",
                "g2.2xlarge",
                "g2.8xlarge",
                "r3.large",
                "r3.xlarge",
                "r3.2xlarge",
                "r3.4xlarge",
                "r3.8xlarge",
                "i2.xlarge",
                "i2.2xlarge",
                "i2.4xlarge",
                "i2.8xlarge",
                "d2.xlarge",
                "d2.2xlarge",
                "d2.4xlarge",
                "d2.8xlarge",
                "hi1.4xlarge",
                "hs1.8xlarge",
                "cr1.8xlarge",
                "cc2.8xlarge",
                "cg1.4xlarge"
            ],
            "ConstraintDescription": "must be a valid EC2 instance type."
        }
    },
    "Mappings": {
        "AWSInstanceType2Arch": {
            "t1.micro": {
                "Arch": "PV64"
            },
            "t2.nano": {
                "Arch": "HVM64"
            },
            "t2.micro": {
                "Arch": "HVM64"
            },
            "t2.small": {
                "Arch": "HVM64"
            },
            "t2.medium": {
                "Arch": "HVM64"
            },
            "t2.large": {
                "Arch": "HVM64"
            },
            "m1.small": {
                "Arch": "PV64"
            },
            "m1.medium": {
                "Arch": "PV64"
            },
            "m1.large": {
                "Arch": "PV64"
            },
            "m1.xlarge": {
                "Arch": "PV64"
            },
            "m2.xlarge": {
                "Arch": "PV64"
            },
            "m2.2xlarge": {
                "Arch": "PV64"
            },
            "m2.4xlarge": {
                "Arch": "PV64"
            },
            "m3.medium": {
                "Arch": "HVM64"
            },
            "m3.large": {
                "Arch": "HVM64"
            },
            "m3.xlarge": {
                "Arch": "HVM64"
            },
            "m3.2xlarge": {
                "Arch": "HVM64"
            },
            "m4.large": {
                "Arch": "HVM64"
            },
            "m4.xlarge": {
                "Arch": "HVM64"
            },
            "m4.2xlarge": {
                "Arch": "HVM64"
            },
            "m4.4xlarge": {
                "Arch": "HVM64"
            },
            "m4.10xlarge": {
                "Arch": "HVM64"
            },
            "c1.medium": {
                "Arch": "PV64"
            },
            "c1.xlarge": {
                "Arch": "PV64"
            },
            "c3.large": {
                "Arch": "HVM64"
            },
            "c3.xlarge": {
                "Arch": "HVM64"
            },
            "c3.2xlarge": {
                "Arch": "HVM64"
            },
            "c3.4xlarge": {
                "Arch": "HVM64"
            },
            "c3.8xlarge": {
                "Arch": "HVM64"
            },
            "c4.large": {
                "Arch": "HVM64"
            },
            "c4.xlarge": {
                "Arch": "HVM64"
            },
            "c4.2xlarge": {
                "Arch": "HVM64"
            },
            "c4.4xlarge": {
                "Arch": "HVM64"
            },
            "c4.8xlarge": {
                "Arch": "HVM64"
            },
            "g2.2xlarge": {
                "Arch": "HVMG2"
            },
            "g2.8xlarge": {
                "Arch": "HVMG2"
            },
            "r3.large": {
                "Arch": "HVM64"
            },
            "r3.xlarge": {
                "Arch": "HVM64"
            },
            "r3.2xlarge": {
                "Arch": "HVM64"
            },
            "r3.4xlarge": {
                "Arch": "HVM64"
            },
            "r3.8xlarge": {
                "Arch": "HVM64"
            },
            "i2.xlarge": {
                "Arch": "HVM64"
            },
            "i2.2xlarge": {
                "Arch": "HVM64"
            },
            "i2.4xlarge": {
                "Arch": "HVM64"
            },
            "i2.8xlarge": {
                "Arch": "HVM64"
            },
            "d2.xlarge": {
                "Arch": "HVM64"
            },
            "d2.2xlarge": {
                "Arch": "HVM64"
            },
            "d2.4xlarge": {
                "Arch": "HVM64"
            },
            "d2.8xlarge": {
                "Arch": "HVM64"
            },
            "hi1.4xlarge": {
                "Arch": "HVM64"
            },
            "hs1.8xlarge": {
                "Arch": "HVM64"
            },
            "cr1.8xlarge": {
                "Arch": "HVM64"
            },
            "cc2.8xlarge": {
                "Arch": "HVM64"
            }
        },
        "AWSRegionArch2AMI": {
            "us-east-1": {
                "PV64": "ami-2a69aa47",
                "HVM64": "ami-6869aa05",
                "HVMG2": "ami-1f12e965"
            },
            "us-west-2": {
                "PV64": "ami-7f77b31f",
                "HVM64": "ami-7172b611",
                "HVMG2": "ami-5c9b6124"
            },
            "us-west-1": {
                "PV64": "ami-a2490dc2",
                "HVM64": "ami-31490d51",
                "HVMG2": "ami-7291a112"
            },
            "eu-west-1": {
                "PV64": "ami-4cdd453f",
                "HVM64": "ami-f9dd458a",
                "HVMG2": "ami-b411c5cd"
            },
            "eu-west-2": {
                "PV64": "NOT_SUPPORTED",
                "HVM64": "ami-886369ec",
                "HVMG2": "NOT_SUPPORTED"
            },
            "eu-central-1": {
                "PV64": "ami-6527cf0a",
                "HVM64": "ami-ea26ce85",
                "HVMG2": "ami-be40f2d1"
            },
            "ap-northeast-1": {
                "PV64": "ami-3e42b65f",
                "HVM64": "ami-374db956",
                "HVMG2": "ami-3efd2c58"
            },
            "ap-northeast-2": {
                "PV64": "NOT_SUPPORTED",
                "HVM64": "ami-2b408b45",
                "HVMG2": "NOT_SUPPORTED"
            },
            "ap-southeast-1": {
                "PV64": "ami-df9e4cbc",
                "HVM64": "ami-a59b49c6",
                "HVMG2": "ami-3e91ed5d"
            },
            "ap-southeast-2": {
                "PV64": "ami-63351d00",
                "HVM64": "ami-dc361ebf",
                "HVMG2": "ami-84a142e6"
            },
            "ap-south-1": {
                "PV64": "NOT_SUPPORTED",
                "HVM64": "ami-ffbdd790",
                "HVMG2": "ami-25ffbe4a"
            },
            "us-east-2": {
                "PV64": "NOT_SUPPORTED",
                "HVM64": "ami-f6035893",
                "HVMG2": "NOT_SUPPORTED"
            },
            "ca-central-1": {
                "PV64": "NOT_SUPPORTED",
                "HVM64": "ami-730ebd17",
                "HVMG2": "NOT_SUPPORTED"
            },
            "sa-east-1": {
                "PV64": "ami-1ad34676",
                "HVM64": "ami-6dd04501",
                "HVMG2": "NOT_SUPPORTED"
            },
            "cn-north-1": {
                "PV64": "ami-77559f1a",
                "HVM64": "ami-8e6aa0e3",
                "HVMG2": "NOT_SUPPORTED"
            },
            "cn-northwest-1": {
                "PV64": "ami-80707be2",
                "HVM64": "ami-cb858fa9",
                "HVMG2": "NOT_SUPPORTED"
            }
        }
    },
    "Resources": {
        "WebServerInstance": {
            "Type": "AWS::EC2::Instance",
            "Properties": {
                "ImageId": {
                    "Fn::FindInMap": [
                        "AWSRegionArch2AMI",
                        {
                            "Ref": "AWS::Region"
                        },
                        {
                            "Fn::FindInMap": [
                                "AWSInstanceType2Arch",
                                {
                                    "Ref": "InstanceType"
                                },
                                "Arch"
                            ]
                        }
                    ]
                },
                "InstanceType": {
                    "Ref": "InstanceType"
                }
            }
        }
    }
}

如何为 Ubuntu 16.04 做类似的事情? 有没有更简单的方法?

【问题讨论】:

    标签: amazon-web-services amazon-ec2 amazon-cloudformation


    【解决方案1】:

    在 Cloudformation 中,您可以通过 Mappings 进行查找。因此,要创建适用于任何区域的脚本,您需要在区域和 AMI id 之间创建映射。

    您可以从list of cloud images 获取 AMI 列表。

    如果您不需要选择不同类型的 AMI(列出的 16.04 的每个区域有 4 种不同的 AMI),您可以简单地在地图中包含您想要的类型,或者创建一个具有多个值的地图,如果这就是你需要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-02
      • 2019-05-11
      • 1970-01-01
      • 2014-08-22
      • 2015-07-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多