Problem with data definition in VM creation with Ubuntu 16.04 in AWS

755 views
Skip to first unread message

Nick Haddock

unread,
Sep 23, 2016, 5:55:00 AM9/23/16
to Terraform
Hi,

Hope you guys can help with this issue.

Using OSX Yosemite - Terraform 0.7.3 - region eu-west-1

I have this configured in my site.tf test file to define the AMI I want to use for the build - as below :

provider "aws" {
    region 
= "eu-west-1"
}


data 
"aws_ami" "ubuntu" {
  most_recent 
= true
  filter 
{
    name 
= "name"
    values 
= ["ubuntu/images/hvm-instance/ubuntu-xenial-16.04-amd64-server-*"]
  
}
  filter 
{
    name 
= "virtualization-type"
    values 
= ["paravirtual"]
  
}
  filter 
{
    name 
= "image-id"
    values 
= ["ami-280b715b"]
}
  owners 
= ["099720109477"] # Canonical
}


I have looked at the AWS site below that shows the filters we can use in this data declaration


Now according to the Canonical AMI available site , there are four potential AMI's I can use, one of which supports paravirtual:

Here is the link:

The Ami I want is the one I have shown above - so I can run it with a m1.small - however type , I get the following error:

$terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but
will 
not be persisted to local or remote state storage.


data
.aws_ami.ubuntu: Refreshing state...
Error refreshing state: 1 error(s) occurred:


* data.aws_ami.ubuntu: Your query returned no results. Please change your filters and try again.

For information - if I add the Ami definition directly into the aws_instance resource definition as below , it works fine:

resource "aws_instance" "rtorrent" {
    
#ami = "${data.aws_ami.ubuntu.id}"
    ami 
= "ami-280b715b"
    instance_type 
= "m1.small"
    tags 
{
        
Name = "test"
    
}
    key_name 
= "my-key-name"
    vpc_security_group_ids 
= ["sg-1234567"]
    subnet_id 
= "subnet-1234567"
}




Also if I change the instance_type to m3.medium, change the virtualisation_type to "hvm", and exclude the Ami image_id filter - it also works. That is because it finds one of the other images which supports those parameters , and it all works.

However , this is for testing , and I only want to use a m1.small.

Any thoughts on why the filtering is not working or if there are other settings I can be trying - all would be gratefully received, as Google has produced no answers to this one as yet.

Thanks in advance

Nick

Nick Haddock

unread,
Oct 3, 2016, 5:04:03 AM10/3/16
to Terraform
Basically after a lot of digging around, it is the mix of Virtualization type (hvm/paravirtual) , the AMI used, and the directory pointed to for 16.04 images that has to be correct.

Here is a URL showing the types of virtual type image supported https://aws.amazon.com/amazon-linux-ami/instance-type-matrix/

Also I used the base Amazon tools to dig deeper into each type of Ubuntu AMI to find out what they support. Everything now makes sense, and why I had to use an m3 VM to get it to work.

Here is the aws command I used to find out more detailed information on the base Ubuntu images, then match them to the correct VM type - modified from another issue found on Ubuntu help forums.

name=$(\
   aws --region eu-west-1 ec2 describe-images --owners 099720109477 \
       --filters Name=root-device-type,Values=ebs \
Name=architecture,Values=x86_64 \
Name=name,Values='*hvm-ssd/ubuntu-xenial-16.04*' \
| awk -F ': ' '/"Name"/ { print $2 | "sort" }' \
| tr -d '",' | tail -1)
aws --region eu-west-1 ec2 describe-images --owners 099720109477         --filters Name=name,Values="$name"     | awk -F ': ' '/"ImageId"/ { print $2 }' | tr -d '",'

So the corrected part of the data definition - should now look like this.

data "aws_ami" "ubuntu" {
  most_recent 
= true
  filter 
{
    name 
= "name"

    values 
= ["ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"]
  
}
  filter 
{
    name 
= "virtualization-type"
    values 
= ["hvm"]
  
}
  filter 
{
    name 
= "image-id"
    values 
= ["ami-844e0bf7"]
}
  owners 
= ["099720109477"] # Canonical
}

Hope this saves others time, as it wasted a few of my hours ;-)
Reply all
Reply to author
Forward
0 new messages