小编310*_*981的帖子

AWS Lambda 使用 python boto3 列出 EC2 实例 ID

我正在尝试使用 python boto3 列出 EC2 实例 id。我是Python新手。

下面的代码工作正常

import boto3
region = 'ap-south-1'
ec2 = boto3.client('ec2', region_name=region)

def lambda_handler(event, context):
    print('Into DescribeEc2Instance')
    instances = ec2.describe_instances(Filters=[{'Name': 'instance-type', 'Values': ["t2.micro", "t3.micro"]}])
    print(instances)
Run Code Online (Sandbox Code Playgroud)

输出是

START RequestId: bb4e9b27-db8e-49fe-85ef-e26ae53f1308 Version: $LATEST
Into DescribeEc2Instance
{'Reservations': [{'Groups': [], 'Instances': [{'AmiLaunchIndex': 0, 'ImageId': 'ami-052c08d70def62', 'InstanceId': 'i-0a22a6209740df', 'InstanceType': 't2.micro', 'KeyName': 'testserver', 'LaunchTime': datetime.datetime(2020, 11, 12, 8, 11, 43, tzinfo=tzlocal()), 'Monitoring': {'State': 'disabled'}
Run Code Online (Sandbox Code Playgroud)

现在,为了从上面的输出中删除实例 ID,我添加了下面的代码(最后 2 行),但由于某种原因它不起作用。

import boto3
region = 'ap-south-1'
instance = []
ec2 = boto3.client('ec2', region_name=region)

    def …
Run Code Online (Sandbox Code Playgroud)

python amazon-web-services boto3 aws-lambda

3
推荐指数
1
解决办法
9197
查看次数

标签 统计

amazon-web-services ×1

aws-lambda ×1

boto3 ×1

python ×1