6

Problem: I am using terraform to get a list of AMI for a specific OS - ubuntu 20.08

I have checked different examples link

When I use the script this does not give me list of AMI

Script

data "aws_ami" "ubuntu" {
    most_recent = true

    filter {
        name   = "name"
        values = ["ubuntu/images/hvm-ssd/ubuntu-xenial-20.08-amd64-server-*"]
    }
    
    filter {
        name = "virtualization - type"
        values = ["hvm"]
    }

    owners = ["AWS"]
}

I have referred the below link as well

How are data sources used in Terraform?

Output:

[ec2-user@ip-172-31-84-148 ~]$ terraform plan
provider.aws.region
  The region where AWS operations will take place. Examples
  are us-east-1, us-west-2, etc.

  Enter a value: us-east-1

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.std_ami: Refreshing state...

------------------------------------------------------------------------

No changes. Infrastructure is up-to-date.

This means that Terraform did not detect any differences between your configuration and real physical resources that exist. As a result, no actions need to be performed.

i am not sure where am I going wrong i have checked a lot of links some i have listed below.

James Z
  • 12,209
  • 10
  • 24
  • 44
sudlo
  • 75
  • 1
  • 1
  • 11
  • 1
    There is no 20.08 and Xenial refers to 16.04. Did you want to use the latest Ubuntu LTS? This is 20.04 and is code named `focal`. So the AMI you are searching for should be `ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*`. Canonical also publish a list of AMIs at http://cloud-images.ubuntu.com/locator/ec2/. – ydaetskcoR Sep 15 '20 at 09:44
  • @ydaetskcoR Yes i need the latest sorry I am not that good in terraform.. kindly guide me >> I am looking for the list o AMI + ubuntu 20.08 – sudlo Sep 15 '20 at 09:45

2 Answers2

17

Your data should be:

data "aws_ami" "ubuntu" {

    most_recent = true

    filter {
        name   = "name"
        values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
    }

    filter {
        name = "virtualization-type"
        values = ["hvm"]
    }

    owners = ["099720109477"]
}

output "test" {
  value = data.aws_ami.ubuntu
}

The owner of Ubuntu is not AWS, and the image is ubuntu-focal-20.04-amd64-server-, not ubuntu-xenial-20.08-amd64-server-.

The above results in (us-east-1):

 {
  "architecture" = "x86_64"
  "arn" = "arn:aws:ec2:us-east-1::image/ami-0dba2cb6798deb6d8"
  "block_device_mappings" = [
    {
      "device_name" = "/dev/sda1"
      "ebs" = {
        "delete_on_termination" = "true"
        "encrypted" = "false"
        "iops" = "0"
        "snapshot_id" = "snap-0f06f1549ff7327c9"
        "volume_size" = "8"
        "volume_type" = "gp2"
      }
      "no_device" = ""
      "virtual_name" = ""
    },
    {
      "device_name" = "/dev/sdb"
      "ebs" = {}
      "no_device" = ""
      "virtual_name" = "ephemeral0"
    },
    {
      "device_name" = "/dev/sdc"
      "ebs" = {}
      "no_device" = ""
      "virtual_name" = "ephemeral1"
    },
  ]
  "creation_date" = "2020-09-08T00:55:25.000Z"
  "description" = "Canonical, Ubuntu, 20.04 LTS, amd64 focal image build on 2020-09-07"
  "filter" = [
    {
      "name" = "name"
      "values" = [
        "ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*",
      ]
    },
    {
      "name" = "virtualization-type"
      "values" = [
        "hvm",
      ]
    },
  ]
  "hypervisor" = "xen"
  "id" = "ami-0dba2cb6798deb6d8"
  "image_id" = "ami-0dba2cb6798deb6d8"
  "image_location" = "099720109477/ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-20200907"
  "image_type" = "machine"
  "most_recent" = true
  "name" = "ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-20200907"
  "owner_id" = "099720109477"
  "owners" = [
    "099720109477",
  ]
  "product_codes" = []
  "public" = true
  "root_device_name" = "/dev/sda1"
  "root_device_type" = "ebs"
  "root_snapshot_id" = "snap-0f06f1549ff7327c9"
  "sriov_net_support" = "simple"
  "state" = "available"
  "state_reason" = {
    "code" = "UNSET"
    "message" = "UNSET"
  }
  "tags" = {}
  "virtualization_type" = "hvm"
}
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • i get the message as the following No changes. Infrastructure is up-to-date. This means that Terraform did not detect any differences between your configuration and real physical resources that exist. As a result, no actions need to be performed. – sudlo Sep 15 '20 at 13:59
  • @sudlo `data` does not change anything. It just fetches a list of AMIs. Thus it will not change your infrastructure. – Marcin Sep 15 '20 at 21:15
  • I agree/// let me work on this and i will get back to you . the only thing which i need to figure out create an output.. – sudlo Sep 16 '20 at 02:04
  • your answer is perfect.... no doubts about it ... thank you .. I am talking about the output which you have put in.. .i have a created an output.tf file.. – sudlo Sep 16 '20 at 03:40
  • @sudlo Hi. I updated the answer showing how to output what I did. To accept the answer you have to click a "tick" button under upvote/downvote buttons. – Marcin Sep 16 '20 at 03:48
  • 2
    Probably worth mentioning how to validate that OwnerID 099720109477 really belongs to Canonical. You can do it by querying ssm `aws --region ap-southeast-2 ssm get-parameters --names /aws/service/canonical/meta/publisher-id`. Response should include `"Value": "099720109477",` – ElektryczneNozyce Jul 24 '21 at 03:26
1

The right way that works for me is to use "amazon" as owners. Otherwise using 099720109477 as the owner incurs a charge for me

data "aws_ami" "ubuntu22" {
  most_recent = true
  owners      = ["amazon"]
  
  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-*"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }
}
  • This is exactly the filter I was looking for. I prefer Amazon as an owner because they also make all of their AMIs available via public SSM parameters, which is very helpful when building AMI pipelines and such. – Michael Reilly Aug 15 '23 at 16:38