About aws_ami values

Hi ,

I hope everyone ıs doing great!

data “aws_ami” “app_ami” {
most_recent = true
owners = [“amazon”]

 filter {
     name = "name"
     values = ["amzn2-ami-hvm*"]
 }

}

I couldn’t see where the ami value in the filter, which starts with “amzn2-ami-hvm”, is listed where on google search? Where can I find where this value is listed and what I can replace it with? Could you please gıve the related link ıf you know?

Regards
Elif

This expression searches through records that look like this

Architecture        : x86_64
BlockDeviceMappings : {/dev/xvda}
BootMode            :
CreationDate        : 2022-03-02T21:09:55.000Z
DeprecationTime     : 2024-03-02T21:09:55.000Z
Description         : Amazon Linux 2 AMI 2.0.20220218.1 x86_64 HVM gp2
EnaSupport          : True
Hypervisor          : xen
ImageId             : ami-08754599965c30981
ImageLocation       : amazon/amzn2-ami-hvm-2.0.20220218.1-x86_64-gp2
ImageOwnerAlias     : amazon
ImageType           : machine
ImdsSupport         :
KernelId            :
Name                : amzn2-ami-hvm-2.0.20220218.1-x86_64-gp2
OwnerId             : 137112412989
Platform            :
PlatformDetails     : Linux/UNIX
ProductCodes        : {}
Public              : True
RamdiskId           :
RootDeviceName      : /dev/xvda
RootDeviceType      : ebs
SriovNetSupport     : simple
State               : available
StateReason         :
Tags                : {}
TpmSupport          :
UsageOperation      : RunInstances
VirtualizationType  : hvm

In the above example, you can see that the name is amzn2-ami-hvm-2.0.20220218.1-x86_64-gp2, so that matches amzn2-ami-hvm*
The results are sorted in descending order of CreationDate and the first from the sorted list is chosen by most_recent = true

You use it like this

resource "aws_instance" "my_instance" {
  ami           = data.aws_ami.app_ami.image_id

Hi Alistair,

Thank you very much for your respond But could you please share wıth me what ıs the resource of this information. what can be the other ImageOwnerAlias and ImageLocations values? I would like to see any resource about this matter.

Owner is usually an AWS account ID. Some well known accounts have aliases, like Amazon’s own. Generally if you are searching for an AMI it will be from Amazon, unless you have created your own custom AMIs, then those will be in your own account.

As for filters, you can query a number of different combinations of the above properties. The names, you just get to know with experience.

See describe-images — AWS CLI 2.0.34 Command Reference which is what this data source is doing under the hood.

So to build the correct filter you have to have some idea of what you are looking for, e.g. linux/windows - if linux then amazon, ubuntu, redhat etc - it goes on

1 Like

Thank you very much for your valuable informations Alistair. It was very usefull!

Regards
Best wishes.
Elif

@Alistair_KodeKloud Is there anywhere to see the “name” of the ami’s? I’m trying to see in the aws console but not having much luck. Just starting a terraform udemy course :smiley:

name isn’t shown directly in the console when choosing AMI to launch. Only the description and image id

If you search Public images for an ID under AMIs, you can see more details…

1 Like