Skip to content

Instantly share code, notes, and snippets.

@andreswebs
Forked from anthonygrees/AWS Images.md
Created December 1, 2023 15:08
Show Gist options
  • Save andreswebs/8b2d433067a17d4ba28326ee955d8060 to your computer and use it in GitHub Desktop.
Save andreswebs/8b2d433067a17d4ba28326ee955d8060 to your computer and use it in GitHub Desktop.
How to find AWS AMI's for Terraform

Centos

aws ec2 describe-images \
    --owners aws-marketplace \
    --filters '[
        {"Name": "name",                "Values": ["CentOS Linux 7*"]},
        {"Name": "virtualization-type", "Values": ["hvm"]},
        {"Name": "architecture",        "Values": ["x86_64"]},
        {"Name": "image-type",          "Values": ["machine"]}
    ]' \
    --query 'sort_by(Images, &CreationDate)[-1]' \
    --region us-east-1 \
    --output json

Table format

aws ec2 describe-images \
      --owners aws-marketplace \
      --filters Name=product-code,Values=aw0evgkw8e5c1q413zgy5pjce \
      --query 'Images[*].[CreationDate,Name,ImageId]' \
      --filters "Name=name,Values=CentOS Linux 7*" \
      --region us-west-2 \
      --output table \
  | sort -r
data "aws_ami" "linux-node" {
  most_recent = true
  owners      = ["aws-marketplace"]
  filter {
    name   = "name"
    values = ["CentOS Linux 7*"]
  }

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

  filter {
    name   = "architecture"
    values = ["x86_64"]
  }

  filter {
    name   = "image-type"
    values = ["machine"]
  }
}

Windows

aws ec2 describe-images \
    --owners amazon \
    --filters '[
        {"Name": "name",                "Values": ["Windows_Server-2016-English-Full-Containers-*"]},
        {"Name": "virtualization-type", "Values": ["hvm"]}
    ]' \
    --query 'sort_by(Images, &CreationDate)[-1]' \
    --region us-east-1 \
    --output json
aws ec2 describe-images \
    --owners amazon \
    --filters '[
        {"Name": "name",                "Values": ["Windows_Server-2019-English-Core-Containers*"]},
        {"Name": "virtualization-type", "Values": ["hvm"]}
    ]' \
    --query 'sort_by(Images, &CreationDate)[-1]' \
    --region us-east-1 \
    --output json
 aws ec2 describe-images \
    --owners amazon \
    --query 'Images[*].[CreationDate,Name,ImageId]' \
    --filters "Name=name,Values=Windows_Server-2019*" \
    --region us-east-1 \
    --output table \
  | sort -r
 aws ec2 describe-images \
    --owners amazon \
    --query 'Images[*].[CreationDate,Name,ImageId]' \
    --filters "Name=name,Values=Windows_Server-2019-English-Full-Containers*" \
    --region us-east-1 \
    --output table \
  | sort -r
data "aws_ami" "win-node" {
  most_recent = true
  owners      = ["amazon"]
  filter {
    name   = "name"
    values = ["Windows_Server-2019-English-Core-Containers*"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }
}
data "aws_ami" "workstation" {
  most_recent = true
  owners      = ["amazon"]

  filter {
    name   = "name"
    values = ["Windows_Server-2016-English-Full-Containers-*"]
  }

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

RHEL

 aws ec2 describe-images \
    --owners 309956199498 \
    --query 'Images[*].[CreationDate,Name,ImageId]' \
    --filters "Name=name,Values=RHEL-7.?*GA*" \
    --region us-east-1 \
    --output table \
  | sort -r
data "aws_ami" "linux-node" {
  most_recent = true
  owners      = ["309956199498"]
  filter {
    name   = "name"
    values = ["RHEL-7.*GA*"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment