如何使用带Boto3的python在AWS中获取免费的AMI [英] How to get free-tier AMI in AWS using python with boto3

查看:81
本文介绍了如何使用带Boto3的python在AWS中获取免费的AMI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在python中构建一个函数,以在特定区域中创建一个新的ec2实例.为了使该功能正常工作,我需要指定此新实例的AMI.问题在于同一AMI(例如Amazon Linux)在不同区域中具有不同的ID,而我无法在另一个区域实例中使用一个区域的映像.

I'm trying to build a function in python that creates a new ec2 instance in a specific region. For the function to work I need to specify the AMI of this new instance. The problem is that the same AMI (for example Amazon Linux) has a different id in different regions and I can't use an image of one region in another region instance.

我不明白如何在特定区域获得此AMI ID

and I can't understand how do I get this AMI id in this specific region

def create_instance(region):
        ec2 = boto3.resource('ec2', region)
        instances = ec2.create_instances(InstanceType='t2.micro',
                                         MinCount=1, MaxCount=1,
                                         ImageId='AMI-id') # What do I put here?

就目前而言,除了AMI是Linux和免费层之外,什么才是真正重要的,所以也许搜索一个特定的已知免费层Linux AMI即可.

For now, it's not really important what the AMI is besides the fact that it is Linux and free-tier, So maybe searching a specific known free-tier Linux AMI will work.

我知道您可以使用describe_images()函数获取所有AMI,但是如何仅过滤那些Linux(可能是特定版本)和自由层

I know you can get all AMI using describe_images() function but how do I filter only those that are Linux (Could be a specific version) and free-tier

boto3.client('ec2').describe_images(Filters["""What do I write here to get only linux free-tier AMI"""])

推荐答案

AWS System Manager在/aws/service/ami-amazon-linux-latest/amzn2-ami-中维护AWS Linux 2 AMI的精选列表.hvm-x86_64-gp2

AWS System Manager maintains the curated list of AWS Linux 2 AMIs at /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2

这是CLI调用:

$ aws ssm get-parameters --names /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2 --region us-east-1

{
    "Parameters": [
        {
            "Name": "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2",
            "Type": "String",
            "Value": "ami-0323c3dd2da7fb37d",
            "Version": 27,
            "LastModifiedDate": 1586395100.713,
            "ARN": "arn:aws:ssm:us-east-1::parameter/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2"
        }
    ],
    "InvalidParameters": []
}

您应该能够在Python中使用

You should be able to do the same in Python with SSM BOTO3 API.

这篇关于如何使用带Boto3的python在AWS中获取免费的AMI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆