AWS Lambda to list EC2 instance id using python boto3

31031981

I m trying to list out EC2 instance id using python boto3. I m new to python.

Below Code is working fine

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)

Output is

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'}

Now to strip instance id from above output, I have added below code(last 2 lines) and for some reason its not working.

import boto3
region = 'ap-south-1'
instance = []
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)
        for ins_id in instances['Instances']:
                print(ins_id['InstanceId'])

Error is

{
  "errorMessage": "'Instances'",
  "errorType": "KeyError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 10, in lambda_handler\n    for ins_id in instances['Instances']:\n"
  ]
}
luk2302

The loop iteration should be

for ins_id in instances['Reservations'][0]['Instances']:

since you have a Reservation key at the top level, then an array and objects in the array with the Instances key which itself is yet another array that you then actually iterate.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

AWS Lambda boto3 : Instance launch from lambda boto3 python eroor

分類Dev

AWS: Publish SNS message for Lambda function via boto3 (Python2)

分類Dev

EC2のDjangoでboto3がAWS_ACCESS_KEY_IDを認識しないのはなぜですか?

分類Dev

AWS:boto3(Python2)を介してLambda関数のSNSメッセージを公開する

分類Dev

How to get service account list from AWS account using boto3?

分類Dev

AWS Python SDK Boto3

分類Dev

List the SNS topic using boto3

分類Dev

Spontaneous shutdowns in AWS EC2 instance

分類Dev

python boto3 attach/replace IAM role to ec2

分類Dev

spamd is using a LOT of CPU credits on my AWS EC2 instance

分類Dev

how to pass AWS lambda 1 output as lambda 2's input using BOTO 3

分類Dev

How to enable MultiAZ for an RDS Instance using Boto3?

分類Dev

AWS Lambda Python S3とboto3、なぜエラーが発生するのかわかりません

分類Dev

I am not able to copy S3 file to EC2 instance using Userdata in CloudFormation

分類Dev

AWS IAM: Allow EC2 instance to stop itself

分類Dev

AWS EC2, command line display instance type

分類Dev

'Launch More like this' AWS EC2 instance

分類Dev

AWS ec2 instance not open from some ip

分類Dev

Redirect URL for Google API client on AWS ec2 instance?

分類Dev

Crontab failing to run on AWS ec2 Ubuntu Instance

分類Dev

Crontab failing to run on AWS ec2 Ubuntu Instance

分類Dev

What is EC2 previous-instance-id?

分類Dev

Using Boto3 and Python How Make a Folder Public

分類Dev

RDS connectivity in Python using Boto3 and IAM Role

分類Dev

NoSuchKey in resize images using python on AWS (lambda + s3)

分類Dev

AWS Lambda boto3:lambda boto3 pythoneroorからのインスタンスの起動

分類Dev

AWS Lambda boto3:lambda boto3 pythoneroorからのインスタンスの起動

分類Dev

AWS Lambda + python:ec2.start_instances(InstancesIds = ??)

分類Dev

Troubleshooting ssh login failure for AWS EC2 using powershell

Related 関連記事

  1. 1

    AWS Lambda boto3 : Instance launch from lambda boto3 python eroor

  2. 2

    AWS: Publish SNS message for Lambda function via boto3 (Python2)

  3. 3

    EC2のDjangoでboto3がAWS_ACCESS_KEY_IDを認識しないのはなぜですか?

  4. 4

    AWS:boto3(Python2)を介してLambda関数のSNSメッセージを公開する

  5. 5

    How to get service account list from AWS account using boto3?

  6. 6

    AWS Python SDK Boto3

  7. 7

    List the SNS topic using boto3

  8. 8

    Spontaneous shutdowns in AWS EC2 instance

  9. 9

    python boto3 attach/replace IAM role to ec2

  10. 10

    spamd is using a LOT of CPU credits on my AWS EC2 instance

  11. 11

    how to pass AWS lambda 1 output as lambda 2's input using BOTO 3

  12. 12

    How to enable MultiAZ for an RDS Instance using Boto3?

  13. 13

    AWS Lambda Python S3とboto3、なぜエラーが発生するのかわかりません

  14. 14

    I am not able to copy S3 file to EC2 instance using Userdata in CloudFormation

  15. 15

    AWS IAM: Allow EC2 instance to stop itself

  16. 16

    AWS EC2, command line display instance type

  17. 17

    'Launch More like this' AWS EC2 instance

  18. 18

    AWS ec2 instance not open from some ip

  19. 19

    Redirect URL for Google API client on AWS ec2 instance?

  20. 20

    Crontab failing to run on AWS ec2 Ubuntu Instance

  21. 21

    Crontab failing to run on AWS ec2 Ubuntu Instance

  22. 22

    What is EC2 previous-instance-id?

  23. 23

    Using Boto3 and Python How Make a Folder Public

  24. 24

    RDS connectivity in Python using Boto3 and IAM Role

  25. 25

    NoSuchKey in resize images using python on AWS (lambda + s3)

  26. 26

    AWS Lambda boto3:lambda boto3 pythoneroorからのインスタンスの起動

  27. 27

    AWS Lambda boto3:lambda boto3 pythoneroorからのインスタンスの起動

  28. 28

    AWS Lambda + python:ec2.start_instances(InstancesIds = ??)

  29. 29

    Troubleshooting ssh login failure for AWS EC2 using powershell

ホットタグ

アーカイブ