Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@AlainODea
Last active February 24, 2024 05:08
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save AlainODea/5b6466a5df07a6ee0c92e34f60f24c65 to your computer and use it in GitHub Desktop.
Save AlainODea/5b6466a5df07a6ee0c92e34f60f24c65 to your computer and use it in GitHub Desktop.
Terraform: Latest Amazon Linux 2 encrypted AMI (Terraform v0.11.3, aws provider v1.60.0)
resource "aws_ami_copy" "amazon-linux-2-encrypted" {
name = "${data.aws_ami.amazon-linux-2.name}-encrypted"
description = "${data.aws_ami.amazon-linux-2.description} (encrypted)"
source_ami_id = "${data.aws_ami.amazon-linux-2.id}"
source_ami_region = "${var.region}"
encrypted = true
tags {
ImageType = "encrypted-amzn2-linux"
}
}
data "aws_ami" "amazon-linux-2" {
most_recent = true
filter {
name = "owner-alias"
values = ["amazon"]
}
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-ebs"]
}
}
@gurpalw
Copy link

gurpalw commented Oct 9, 2019

you now need to set owners = "amazon" instead of using the owner-alias.
https://www.terraform.io/docs/providers/aws/d/ami.html

@AlainODea
Copy link
Author

you now need to set owners = "amazon" instead of using the owner-alias.
https://www.terraform.io/docs/providers/aws/d/ami.html

Thank you. This was done for Terraform v0.11.3 and aws provider v1.60.0. I've updated the title to show that constraint now. I didn't anticipate that this would be broken by future releases. I appreciate you identifying this gap.

@steadysupply
Copy link

And now you need to set owners = ["amazon"]

@AlainODea
Copy link
Author

AlainODea commented Aug 6, 2020

@steadysupply wrote:

And now you need to set owners = ["amazon"]

Not for Terraform v0.11.3 and aws provider v1.60.0, which this was written for. I'm guessing your comment is intended for other people hitting this as a search result.

Can you explain when this changed for their benefit?

@ArseniiPetrovich
Copy link

The least I can say is it is the fact for 14 Terraform

@TimoDJatomika
Copy link

thanks.

@raboley
Copy link

raboley commented Apr 24, 2021

Thanks! This worked for me as of today everything latest.

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

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

@djsd123
Copy link

djsd123 commented Aug 23, 2022

If using CDKTF python

               amazon_linux_2 = ec2.DataAwsAmi(self, "amazon-linux-2",
                                        most_recent=True,
                                        owners=["amazon"],
                                        filter=[
                                                ec2.DataAwsAmiFilter(
                                                                     name="name",
                                                                     values=["amzn2-ami-hvm-*-x86_64-ebs"]
                                                                     )
                                                 ]
                                        )

@laksgreen
Copy link

laksgreen commented May 20, 2023

data "aws_ami" "base_ami" {
  most_recent      = true
  owners           = ["amazon"]
 
  filter {
    name   = "name"
    values = ["al2023-ami-2023.*-x86_64"]
  }
 
  filter {
    name   = "architecture"
    values = ["x86_64"]
  }
 
  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }
 
}

al2023-ami-2023.*-x86_64 => this one is enough of you and x86_64 and hvm added additionally,

I got the info from: https://www.thelinuxfaq.com/937-how-can-i-get-latest-ami-id-for-aws-instance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment