0
0
TerraformHow-ToBeginner · 3 min read

How to Use data aws_ami in Terraform for AMI Lookup

Use the data "aws_ami" resource in Terraform to look up an Amazon Machine Image (AMI) by filters like name or owner. This lets you dynamically find the latest AMI without hardcoding its ID, making your infrastructure flexible and up to date.
📐

Syntax

The data "aws_ami" block lets Terraform query AWS for an AMI matching your criteria. You specify filters like most_recent to get the latest AMI, owners to limit by owner ID, and filter blocks to match AMI properties such as name or tags.

Key parts:

  • most_recent: true or false to get the newest AMI
  • owners: list of AWS account IDs or self or amazon
  • filter: blocks with name and values to match AMI attributes
  • id: the resulting AMI ID found
terraform
data "aws_ami" "example" {
  most_recent = true
  owners      = ["amazon"]

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

Example

This example shows how to find the latest Amazon Linux 2 AMI and use its ID to launch an EC2 instance. It demonstrates dynamic AMI lookup without hardcoding the AMI ID.

terraform
provider "aws" {
  region = "us-east-1"
}

data "aws_ami" "amazon_linux" {
  most_recent = true
  owners      = ["amazon"]

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

resource "aws_instance" "example" {
  ami           = data.aws_ami.amazon_linux.id
  instance_type = "t2.micro"

  tags = {
    Name = "ExampleInstance"
  }
}
Output
Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Outputs: aws_instance.example.id = "i-0abcd1234efgh5678"
⚠️

Common Pitfalls

Common mistakes when using data aws_ami include:

  • Not specifying owners, which can cause errors or unexpected AMIs.
  • Using overly broad or incorrect filter values, returning no results or wrong AMIs.
  • Forgetting most_recent = true when you want the latest AMI, causing Terraform to pick an older one.
  • Hardcoding AMI IDs instead of using data aws_ami, which reduces flexibility.

Example of a wrong filter and the fix:

terraform
data "aws_ami" "wrong" {
  owners = ["amazon"]

  filter {
    name   = "name"
    values = ["wrong-ami-name"]
  }
}

# Fix:
data "aws_ami" "correct" {
  most_recent = true
  owners      = ["amazon"]

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

Quick Reference

Tips for using data aws_ami:

  • Always specify owners to limit AMI search scope.
  • Use most_recent = true to get the latest AMI.
  • Use filters with name and values to target specific AMI versions.
  • Reference the AMI ID with data.aws_ami.NAME.id in resources.

Key Takeaways

Use data aws_ami to dynamically find AMIs instead of hardcoding IDs.
Always specify owners and use filters to narrow down the AMI search.
Set most_recent = true to get the latest AMI version automatically.
Reference the AMI ID with data.aws_ami.NAME.id when creating resources.
Check filters carefully to avoid no results or wrong AMI matches.