Как динамически изменить размер экземпляра ec2 с помощью функции AWS Lambda с Boto3 - PullRequest
0 голосов
/ 27 декабря 2018

Как изменить тип экземпляра для нескольких экземпляров с помощью лямбда-функции Python без жесткого кодирования идентификаторов экземпляров.

import boto3

client = boto3.client('ec2')

# Insert your Instance ID here
my_instance = <<some code here to fetch instance IDs of all instances 
filtering all t2.micro instance types>>

# Stop the instance
client.stop_instances(InstanceIds=[my_instance])
waiter=client.get_waiter('instance_stopped')
waiter.wait(InstanceIds=[my_instance])

# Change the instance type
client.modify_instance_attribute(InstanceId=my_instance, 
Attribute='instanceType', Value='m3.xlarge')

# Start the instance
client.start_instances(InstanceIds=[my_instance])

1 Ответ

0 голосов
/ 27 декабря 2018

Просто укажите список, например:

import boto3

client = boto3.client('ec2')

# Get t2.micro instances
response = client.describe_instances(
   Filters=[{'Name': 'instance-type','Values':['t2.micro']}]
)

instances = [i['InstanceId'] for i in r['Instances'] for r in response['Reservations']]

# Stop the instances
client.stop_instances(InstanceIds=instances)
waiter=client.get_waiter('instance_stopped')
waiter.wait(InstanceIds=instances)

# Change the instance type
for i in instances:
    client.modify_instance_attribute(InstanceId=i, Attribute='instanceType', Value='m3.xlarge')

# Start the instance
client.start_instances(InstanceIds=instances)

Я не проверял код, поэтому сначала проверьте его!

...