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

ご挨拶

AWS Lambdaで以下のPythonスクリプトを作成して、停止したすべてのインスタンスを一覧表示します。これは正常に機能します。ここで、停止したインスタンスを起動して機能を拡張したいと思います。

脚本:

region ='us-east-1'
ec2 = boto3.resource('ec2',region)
def lambda_handler(event, context):
    instances = ec2.instances.filter(Filters=[{'Name': 'instance-state-name', 'Values': ['stopped']}])
    for instance in instances:
        print('Ec2 Instances which are stopped: ', 'Instance ID: ', instance.id, 'Instance state: ', instance.state, 'Instance type: ',instance.instance_type)

現在、インスタンスを開始するために次のコードを追加しています。

ec2.start_instances(InstanceIds=instance.id)

(InstanceIds = '')はここにリストを期待しているため、エラー[ec2.ServiceResource 'オブジェクトには属性' start_instances]がありませんが、インスタンスのタイプは<class 'boto3.resources.factory.ec2.Instance'>How do i convertであるため、start_instancesメソッドにリストを入力できます。

前もって感謝します!!

以下に、回答を得た後の更新されたスクリプトを見つけてください。このスクリプトは、停止したインスタンスを自動的に開始します。

更新されたスクリプト

region ='us-east-1'
ec2 = boto3.resource('ec2',region)
client = boto3.client('ec2',region)
def lambda_handler(event, context):
    instances = ec2.instances.filter(Filters=[{'Name': 'instance-state-name', 'Values': ['stopped']}])
    for instance in instances:
        print('Ec2 Instances which are stopped: ', 'Instance ID: ', instance.id, 'Instance state: ', instance.state, 'Instance type: ',instance.instance_type)
        instance_ids = [instance.id for instance in instances]
        response = client.start_instances(InstanceIds=[instance.id])
        print('Lambda have started these instances', instance.id)  

注意このスクリプトをコピーして貼り付ける場合は、インスタンスがオンになり、コストがかかる可能性があるため、注意してください。(このスクリプトは機能しています、テスト済みです!)

Sudharsan Sivasankaran

ec2.instancesは高レベルのリソースでstart_instanceは低レベルのクライアントです。クライアント関数を使用するには、次のコマンドクライアントを作成する必要があります。boto3.client('ec2')

client = boto3.client('ec2')
response = client.start_instances(
    InstanceIds=[
        'string',
    ],
)

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

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

編集
0

コメントを追加

0

関連記事

分類Dev

AWS EC2 Boto3の「start_instances()はキーワード引数のみを受け入れる」エラー

分類Dev

AWS Lambda to list EC2 instance id using python boto3

分類Dev

Python AWS Lambda Certificates

分類Dev

Redirect all outbound SSH traffic through single IP from bunch of AWS EC2 instances

分類Dev

AWS: Adding Correct Inbound Security Groups to RDS and EC2 Instances

分類Dev

Adding name to EC2 instances when deploying AWS::EKS::Nodegroup in CloudFormation

分類Dev

Setting AWS IAM policy for restricting user access to certain EC2 instances

分類Dev

Aws lambda describe instances time out

分類Dev

Amazon AWS EC2 GoLang

分類Dev

robomongo aws ec2 ubuntu

分類Dev

CircleCI deployment to AWS EC2

分類Dev

Certbot renewal error - AWS EC2

分類Dev

Spontaneous shutdowns in AWS EC2 instance

分類Dev

Installing Postgresql 9.6 on AWS - EC2

分類Dev

Run Elasticsearch on AWS EC2 with Docker

分類Dev

AWS Lambda aws.ec2()が値を返さない(NodeJS)

分類Dev

Establecer etiqueta en AWS :: EC2 :: Instances BlockStorage del volumen raíz a través de CloudFormation

分類Dev

How to run SQLAlchemy on AWS Lambda in Python

分類Dev

Python 2.7 AWS Lambda Support will end in 2020?

分類Dev

AWS Lambda, retries on timeout, Python SDK

分類Dev

tesseract-ocr works on EC2, not lambda

分類Dev

EC2 vs Elastic Beanstalk vs Lambda

分類Dev

Make Lambda access EC2 port through a VPC

分類Dev

AWS EC2 : upgrade from AMI 1 to AMI 2

分類Dev

Hazelcast AWS ec2自動検出

分類Dev

Troubleshooting ssh login failure for AWS EC2 using powershell

分類Dev

AWS IAM: Allow EC2 instance to stop itself

分類Dev

AWS Application in Private EC2 and HTTP traffic

分類Dev

AWS ec2 - Disk uuid of volume added from a snapshot

Related 関連記事

  1. 1

    AWS EC2 Boto3の「start_instances()はキーワード引数のみを受け入れる」エラー

  2. 2

    AWS Lambda to list EC2 instance id using python boto3

  3. 3

    Python AWS Lambda Certificates

  4. 4

    Redirect all outbound SSH traffic through single IP from bunch of AWS EC2 instances

  5. 5

    AWS: Adding Correct Inbound Security Groups to RDS and EC2 Instances

  6. 6

    Adding name to EC2 instances when deploying AWS::EKS::Nodegroup in CloudFormation

  7. 7

    Setting AWS IAM policy for restricting user access to certain EC2 instances

  8. 8

    Aws lambda describe instances time out

  9. 9

    Amazon AWS EC2 GoLang

  10. 10

    robomongo aws ec2 ubuntu

  11. 11

    CircleCI deployment to AWS EC2

  12. 12

    Certbot renewal error - AWS EC2

  13. 13

    Spontaneous shutdowns in AWS EC2 instance

  14. 14

    Installing Postgresql 9.6 on AWS - EC2

  15. 15

    Run Elasticsearch on AWS EC2 with Docker

  16. 16

    AWS Lambda aws.ec2()が値を返さない(NodeJS)

  17. 17

    Establecer etiqueta en AWS :: EC2 :: Instances BlockStorage del volumen raíz a través de CloudFormation

  18. 18

    How to run SQLAlchemy on AWS Lambda in Python

  19. 19

    Python 2.7 AWS Lambda Support will end in 2020?

  20. 20

    AWS Lambda, retries on timeout, Python SDK

  21. 21

    tesseract-ocr works on EC2, not lambda

  22. 22

    EC2 vs Elastic Beanstalk vs Lambda

  23. 23

    Make Lambda access EC2 port through a VPC

  24. 24

    AWS EC2 : upgrade from AMI 1 to AMI 2

  25. 25

    Hazelcast AWS ec2自動検出

  26. 26

    Troubleshooting ssh login failure for AWS EC2 using powershell

  27. 27

    AWS IAM: Allow EC2 instance to stop itself

  28. 28

    AWS Application in Private EC2 and HTTP traffic

  29. 29

    AWS ec2 - Disk uuid of volume added from a snapshot

ホットタグ

アーカイブ