Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EC2 Instance - cloudformation template -gives error "Template validation error: Template error: Unable to get mapping for RegionMap::us-west-2::AMI"

I have written a simple Cloudformation template to use "Mappings". Following is the code. I have logged into us-west-2a (west coast) and ran my template. I have 3 default subnets. The AMI I gave in the mapping section, was created by me:

I expected when I type us-west-2a as parameter, it should pick ami-fcccdf85 as per code below. But it says

Template error: Unable to get mapping for RegionMap::us-west-2::AMI"

 "RegionMap" : {
            "us-east-1"      : { "AMI" : "ami-48b4bf31" },
            "us-west-2a"      : { "AMI" : "ami-fcccdf85" },
            "us-west-1"      : { "AMI" : "ami-48b4bf31" }     
        }

Code:

{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "EC2 Head Node Instance ",
"Parameters": {
    "AvailabilityZone": {
        "Description": "Availablity Zone",
        "Type": "String"

    },
    "Region":{
        "Description": "Dev/Test/Prod regions",
        "Type": "String"
    },

    "Subnet": {
        "Description": "subnet to launch virtual server in",
        "Type": "AWS::EC2::Subnet::Id"
    }

},
"Mappings" : {
        "RegionMap" : {
            "us-east-1"      : { "AMI" : "ami-48b4bf31" },
            "us-west-2a"      : { "AMI" : "ami-fcccdf85" },
            "us-west-1"      : { "AMI" : "ami-48b4bf31" }     
        }
    },

"Resources": {
    "EC2Instance": {
        "Type": "AWS::EC2::Instance",
        "Properties": {
            "ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "AMI" ]},
            "SubnetId": {"Ref": "Subnet"},
            "AvailabilityZone": {"Ref": "AvailabilityZone"},
            "Tags": [
                {
                    "Key": "Name",
                    "Value": "Head Node in DEV region"
                }
            ]
            }
        }

},
"Outputs": {
    "InstanceId": {
        "Value": {"Ref": "EC2Instance"},
        "Description": "ID of virtual server"
    },

    "PublicIPAddress": {
        "Value": {"Fn::GetAtt": ["EC2Instance", "PublicIp"]},
        "Description": "public IP address of virtual server"
    }
   }
}

Any suggestions please?

like image 383
Jason Avatar asked Jul 12 '17 21:07

Jason


1 Answers

you typed "us-west-2a" remove the a should be "us-west-2" AMI are region specific and not AZ (availability zone) specific, in other words you have to specify a region.

like image 142
Antonio Aga Rossi Avatar answered Oct 16 '22 19:10

Antonio Aga Rossi