Amazon Web Services (AWS) - List Amazon Machine Images (AMI) using Terraform

by
Jeremy Canfield |
Updated: August 31 2023
| Amazon Web Services (AWS) articles
Let's say you have the following files on your Terraform server.
├── required_providers.tf
├── amazon_machine_images (directory)
│ ├── data.tf
│ ├── outputs.tf
│ ├── provider.tf
required_providers.tf will almost always have this.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}
Let's say provider.tf has the following. In this example, the "default" profile in /home/username/.aws/config and /home/username/.aws/credentials is being used. This assumes you have setup Terraform as described in Amazon Web Services (AWS) - Getting Started with Terraform.
provider "aws" {
alias = "default"
profile = "default"
region = "default"
}
And data.tf contains something like this.
data "aws_ami" "most-recent-ecs-optimized-image" {
most_recent = true
filter {
name = "name"
values = ["amzn-ami*ecs-optimized"]
}
}
The terraform refresh command should return something like this, the ID of the most recent ECS optimized image.
data.aws_ami.most-recent-ecs-optimized-image: Read complete after 1s [id=ami-012310613ab7ed9d0]
Did you find this article helpful?
If so, consider buying me a coffee over at