ban*_*ong 21 amazon-ec2 amazon-web-services centos7 amazon-ami
我已经通过登录AWS站点启动EC2实例,点击"启动"按钮并遵循禁止的步骤.现在我想从Ansible脚本启动实例,为此我想(我想)需要我想要启动的图像的AMI ID.
问题是我正在从"市场"发布图像,但我找不到AMI ID.特别是我正在使用Centos 7图像.这很容易在网页界面找到,只是去市场搜索"centos",我想要的图像是第一个找到的图像,但提供的图像信息似乎不包括我的AMI ID需要从脚本启动它.解决方法是手动启动映像,然后在检查正在运行的映像时,会给出AMI ID.但有没有更容易找到它的方法?
Ant*_*ace 42
CentOS将他们的AMI产品代码发布到他们的维基.维基为最新的CentOS 7 AMI提供以下信息:
aws-marketplaceaw0evgkw8e5c1q413zgy5pjce使用此信息,我们可以使用AWS CLI 查询describe-images:
例:
aws ec2 describe-images \
--owners 'aws-marketplace' \
--filters 'Name=product-code,Values=aw0evgkw8e5c1q413zgy5pjce' \
--query 'sort_by(Images, &CreationDate)[-1].[ImageId]' \
--output 'text'
Run Code Online (Sandbox Code Playgroud)
输出:
ami-6d1c2007
Run Code Online (Sandbox Code Playgroud)
此查询返回单个AMI ID,通过按创建日期对集合进行排序,然后选择集合中的最后一个(最新)元素来选择.
根据CentOS维基,multiple AMI ids may be associated with a product key所以虽然此查询目前只返回一个AMI,因为当前只存在一个与此产品匹配的产品...将来如果为此产品代码创建了新的AMI,则此查询将返回.
小智 7
如果您正在寻找所有Centos7图像
$ 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
| 2018-06-13T15:58:14.000Z| CentOS Linux 7 x86_64 HVM EBS ENA 1805_01-b7ee8a69-ee97-4a49-9e68-afaee216db2e-ami-77ec9308.4 | ami-3ecc8f46 |
| 2018-05-17T09:30:44.000Z| CentOS Linux 7 x86_64 HVM EBS ENA 1804_2-b7ee8a69-ee97-4a49-9e68-afaee216db2e-ami-55a2322a.4 | ami-5490ed2c |
| 2018-04-04T00:11:39.000Z| CentOS Linux 7 x86_64 HVM EBS ENA 1803_01-b7ee8a69-ee97-4a49-9e68-afaee216db2e-ami-8274d6ff.4 | ami-0ebdd976 |
| 2017-12-05T14:49:18.000Z| CentOS Linux 7 x86_64 HVM EBS 1708_11.01-b7ee8a69-ee97-4a49-9e68-afaee216db2e-ami-95096eef.4 | ami-b63ae0ce |
Run Code Online (Sandbox Code Playgroud)
向其添加类似查询可用于RedHat
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
| 2018-03-23T20:42:08.000Z | RHEL-7.5_HVM_GA-20180322-x86_64-1-Hourly2-GP2 | ami-6871a115 |
| 2017-08-08T15:37:31.000Z | RHEL-7.4_HVM_GA-20170808-x86_64-2-Hourly2-GP2 | ami-c998b6b2 |
| 2017-07-24T15:44:39.000Z | RHEL-7.4_HVM_GA-20170724-x86_64-1-Hourly2-GP2 | ami-cdc999b6 |
| 2016-10-26T22:32:29.000Z | RHEL-7.3_HVM_GA-20161026-x86_64-1-Hourly2-GP2 | ami-b63769a1 |
| 2015-11-12T21:06:58.000Z | RHEL-7.2_HVM_GA-20151112-x86_64-1-Hourly2-GP2 | ami-2051294a |
| 2015-02-25T20:24:23.000Z | RHEL-7.1_HVM_GA-20150225-x86_64-1-Hourly2-GP2 | ami-12663b7a |
| 2015-02-09T22:54:40.000Z | RHEL-7.0_HVM_GA-20150209-x86_64-1-Hourly2-GP2 | ami-60a1e808 |
| 2014-10-17T20:29:24.000Z | RHEL-7.0_HVM_GA-20141017-x86_64-1-Hourly2-GP2 | ami-a8d369c0 |
| 2014-05-28T19:17:11.000Z | RHEL-7.0_GA_HVM-x86_64-3-Hourly2
| ami-785bae10 |
Run Code Online (Sandbox Code Playgroud)
我使用此处的其他答案作为在 Terraform 中测试 AMI 查找的好方法。
使用...
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
Run Code Online (Sandbox Code Playgroud)
...让我有机会尝试并犯错我的查找
data "aws_ami" "centos" {
most_recent = true
owners = ["aws-marketplace"]
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "architecture"
values = ["x86_64"]
}
filter {
name = "image-type"
values = ["machine"]
}
filter {
name = "name"
values = ["CentOS Linux 7*"]
}
}
resource "aws_launch_configuration" "launch_configuration" {
name_prefix = "${var.name}-"
image_id = "${data.aws_ami.centos.image_id}"
instance_type = "t2.nano"
iam_instance_profile = "${aws_iam_instance_profile.instance_profile.name}"
security_groups = ["${aws_security_group.lc_security_group.id}"]
user_data = "${data.template_file.user_data.rendered}"
lifecycle {
create_before_destroy = true
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21373 次 |
| 最近记录: |