How to find latest Linux ami for Aws ec2

Below are given the ways to find latest ec2 Linux Ami using Aws-cli, Terraform and Cloudformation.

1. To find latest Linux Ami using Aws-cli ————

echo “Finding latest Linux Image”
image_ID=$(aws ec2 describe-images \
–owners amazon \
–filters Name=name,Values=amzn2-ami-hvm-* \
–query ‘sort_by(Images,&CreationDate)[-1].ImageId’ \
–output text)

echo $image_ID

2. To find latest Linux Ami using Terraform ————-

ami=data.aws_ami.linux_ami.id

——————————–

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

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

3. To find latest Linux Ami using Cloudformation ————-

“Parameters”: {

“LinuxLatestAmiId” : {
“Type” : “AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>”,
“Default”: “/aws/service/ami-amazon-linux-latest/
amzn2-ami-hvm-x86_64-gp2”
}
}
—————————————–

“ImageId” : { “Ref” : “LInuxLatestAmiId” }

0 Answers

  • #244
    Up
    0
    Down

    Below are given the ways to find latest ec2 Linux Ami using Aws-cli, Terraform and Cloudformation.

    1. To find latest Linux Ami using Aws-cli ————

    echo “Finding latest Linux Image”
    image_ID=$(aws ec2 describe-images \
    –owners amazon \
    –filters Name=name,Values=amzn2-ami-hvm-* \
    –query ‘sort_by(Images,&CreationDate)[-1].ImageId’ \
    –output text)

    echo $image_ID

    2. To find latest Linux Ami using Terraform ————-

    ami=data.aws_ami.linux_ami.id

    ——————————–

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

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

    3. To find latest Linux Ami using Cloudformation ————-

    “Parameters”: {

    “LinuxLatestAmiId” : {
    “Type” : “AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>”,
    “Default”: “/aws/service/ami-amazon-linux-latest/
    amzn2-ami-hvm-x86_64-gp2”
    }
    }
    —————————————–

    “ImageId” : { “Ref” : “LInuxLatestAmiId” }

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.