我遇到了一个要求,我需要获得特定区域的最新amazon-ecs优化图像的EC2 ami id。是否有任何脚本或api可用于选择创建新群集时提供的ami-id?
因此,一旦他们用最新的ami更新控制台,我就不需要更新我的脚本了。
答案 0 :(得分:1)
我们可以使用AWS命令行界面(CLI)来获取ami-id。 api describe-images
描述了您可以使用的图像。发现amazon-ecs优化图像的所有者ID为591542846629
。
以下脚本返回区域ap-northeast-1
中最新的amazon-ecs优化图像ID。
name=$(aws --region ap-northeast-1 ec2 describe-images \
--owners 591542846629| awk -F ': ' '/"Name"/ { print $2 | "sort" }' \
| tr -d '",' | grep -v rc |tail -1)
ami_id=$(aws --region ap-northeast-1 ec2 describe-images --owners 591542846629 \
--filters Name=name,Values="$name" \
| awk -F ': ' '/"ImageId"/ { print $2 }' | tr -d '",')
echo $ami_id
答案 1 :(得分:1)
我安装了jq,使搜索和排序更容易,更可靠:
Public Sub Insertplayer()
dbconn.ConnectionString = "server='" & My.Settings.ip & "';port=3306; user id='" & My.Settings.uid & "';password='" & My.Settings.pwd & "'; database= '" & My.Settings.db & "';"
dbcomm.Connection = dbconn
fname = imageurltxt.Text
Dim Content As Byte() = ImageToStream(fname)
Try
dbconn.Open()
Dim query As String
query = "INSERT INTO `player`(`ID`, `First_Name`, `Last_Name`, `Gender`, `Date_of_Birth`, `Civil_Status`, `Nationality`, `Address`, `Status', `Picture`) VALUES('" + TextBox7.Text + "','" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox9.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + TextBox8.Text + "', @Pic)"
dbcomm.Parameters.AddWithValue("@Pic", Content)
dbcomm.ExecuteNonQuery()
dbcomm = New MySqlCommand(query, dbconn)
dbread = dbcomm.ExecuteReader
dbread.Close()
Catch ex As Exception
MsgBox(ex.Message)
Finally
dbconn.Close()
End Try
End Sub
Private Function ImageToStream(ByVal fileName As String) As Byte()
Dim stream As New MemoryStream()
tryagain:
Try
Dim image As New Bitmap(fileName)
image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg)
Catch ex As Exception
GoTo tryagain
End Try
Return stream.ToArray()
End Function
如果此所有者添加了更多图片类型,我也会使用基本名称匹配。
答案 2 :(得分:1)
还有一个纯粹使用AWS CLI,因为前面的所有示例都依赖于 jq 和/或管道。主要取自Jason Haines的评论here。
aws ec2 describe-images --owner amazon --filters 'Name=name,Values=amzn-ami-????.??.?-amazon-ecs-optimized' 'Name=state,Values=available' --query 'sort_by(Images, &CreationDate) | [-1].ImageId' --output text
答案 3 :(得分:1)
结合@romaninsh和@Derek的答案
aws ssm get-parameters --names /aws/service/ecs/optimized-ami/amazon-linux/recommended | \
jq -r .Parameters[].Value | jq -r .image_id
答案 4 :(得分:1)
以下是使用boto3的方法
pom.xml
现在from operator import itemgetter
# ECS Optimized AMI to use, different per region. ID changes when an updated AMI is released.
# https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-optimized_AMI.html
AMI_NAME = 'amzn-ami-????.??.?-amazon-ecs-optimized'
session = boto3.Session()
ec2_client = self.session.client('ec2', region_name='ca-central-1')
ecs_amis = ec2_client.describe_images(
Filters=[
{'Name': 'name', 'Values': [AMI_NAME, ]},
{'Name': 'state', 'Values': ['available', ]}
]
)
ecs_amis = ecs_amis.get('Images')
ecs_amis = sorted(ecs_amis, key=itemgetter('CreationDate'), reverse=True)
ami_id = ecs_amis[0]['ImageId']
包含根据CreationDate的最新ami。干杯!
答案 5 :(得分:1)
一个衬里只获得在脚本中使用的AMI ID:
aws ssm get-parameters --names /aws/service/ecs/optimized-ami/amazon-linux/recommended/image_id --region us-east-1 --query 'Parameters[0].Value'
答案 6 :(得分:0)
使用元数据和日期打印出所有AMI更安全,然后选择。 AWS CLI限制返回的AMI数量。
这是我的,它与上面类似,但有一些过滤器,我也可以互换替换一些选项。
# Owners
owners=amazon
owners=aws-marketplace
owners=591542846629 # AMZN ECS
owners=microsoft
# AMI Regions
region=us-west-2
region=eu-central-1
# Amazon AMI - ECS
owners=591542846629
type="amazon-ecs-optimized"
aws ec2 describe-images \
--region ${region} \
--filters "Name=virtualization-type,Values=hvm" "Name=root-device-type,Values=ebs" \
--owners "${owners}" | jq -r '.Images[] | "\(.CreationDate) \(.Name) \(.ImageId)"' | grep "${type}" | sort -r | head
答案 7 :(得分:0)
现在可以通过SSM参数存储API
完成例如:
this.preferences.controls['station'].setValue( {
stationID: 0,
code: 'UTC',
description: 'UTC',
isActive: true,
modifiedBy: '',
modifiedOn: '',
});
答案 8 :(得分:0)
这一行仅打印出指定区域的最新 ECS 优化 AMI ID。
aws ssm get-parameters --names /aws/service/ecs/optimized-ami/amazon-linux/recommended --query "Parameters[].Value" --output text --region us-west-1 | jq -r '.image_id'