获取AWS实例的最新AMI ID [英] Get latest AMI ID for AWS instance
问题描述
我想在为自动缩放架构创建ec2实例时获取AWS Linux计算机的最新ami id。
I wanted to fetch the latest ami id for AWS Linux machine while creating an ec2 instance for an autoscaling architecture.
我正在尝试 aws
cli来获取图像类型,但是如果我使用 describe-images
命令,它将打印出很多信息。
I was trying the aws
cli to get the images types, but it would print out a lot of information if I used the describe-images
command.
我的要求是仅获取映像ID,以便可以使用它来创建启动配置或使用最新的ami-id启动实例。
My requirement was to get only the image id, so that I could use it to create a launch configuration or launch an instance with the latest ami-id.
推荐答案
最近鲜为人知的功能是使用AWS Systems Manager参数存储查询最新的Amazon Linux AMI ID | AWS Compute Blog 。
A little-known recent feature is the ability to Query for the latest Amazon Linux AMI IDs using AWS Systems Manager Parameter Store | AWS Compute Blog.
名称空间由两部分组成:
The namespace is made up of two parts:
- 参数存储前缀(树):
/ aws / service / ami-amazon-linux-latest /
- AMI名称别名:(例如) amzn-ami-hvm-x86_64-gp2
- Parameter Store Prefix (tree):
/aws/service/ami-amazon-linux-latest/
- AMI name alias: (example) amzn-ami-hvm-x86_64-gp2
这些:
aws ec2 describe-images --owners amazon --filters "Name=name,Values=amzn*" --query 'sort_by(Images, &CreationDate)[].Name'
Get-EC2ImageByName -Name amzn* | Sort-Object CreationDate | Select-Object Name
可以更改为:
aws ssm get-parameters --names /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2 --region us-east-1
Get-SSMParameter -Name /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2 -region us-east-1
此外,它可以在 CloudFormation模板中使用:
Plus, it can be used in a CloudFormation template:
# Use public Systems Manager Parameter
Parameters :
LatestAmiId :
Type : 'AWS::SSM::Parameter::Value'
Default: ‘/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2’
Resources :
Instance :
Type : 'AWS::EC2::Instance'
Properties :
ImageId : !Ref LatestAmiId
这篇关于获取AWS实例的最新AMI ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!