Reputation: 28901
I want to simply use Amazon's Cloud Formation tools to create an EC2 instance with the Ubuntu 16.04 image.
This should be simple enough, except that the image ID seems to vary greatly depending upon the region and architecture type of the machine.
For example, this is a small portion of the example code that is provided in their Lamp example, to create a EC2 instance running the Amazon operating system:
{
"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"
}
}
}
}
}
How can I do something similar for Ubuntu 16.04? Is there an easier way?
Upvotes: 0
Views: 316
Reputation: 37480
In Cloudformation you can do a lookup via Mappings. So to create a script that will work in any region, you need to create a mapping between the region and AMI id.
You can get the list of the AMIs from the list of cloud images.
If you don't need the ability to select different types of AMIs (there are 4 different AMIs for each region for 16.04 listed) you can simply include the type you want in your map, or create a map with multiple values if that's what you need.
Upvotes: 1