0

I have created the below python script in AWS Lambda, to list all the stopped instances, which works well. now, I wants to extend the functionality by starting the instances which were stopped.

Script:

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)

Now am appending the following code to start the instances:

ec2.start_instances(InstanceIds=instance.id)

I am getting error[ec2.ServiceResource' object has no attribute 'start_instances], because (InstanceIds=' ') expects list here, but my instance is of type <class 'boto3.resources.factory.ec2.Instance'> How do i convert, so that I can input list to start_instances method.

Thanks in Advance!!

Please find below, my updated script after getting answer, now this script will start the stopped instances automatically.

Updated script

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) 

Caution Please exercise care if you are copy pasting this script as this will turn-on instances which may cost you. (This script is working, TESTED!)

asked Mar 28, 2018 at 15:20

2 Answers 2

1

ec2.instances is the higher level resource and start_instance is low level client, to use the client function, you need create the client with boto3.client('ec2')

client = boto3.client('ec2')
response = client.start_instances(
 InstanceIds=[
 'string',
 ],
)
answered Mar 28, 2018 at 16:13
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to start each instance in the loop:

ec2.start_instances(InstanceIds=[instance.id])

If you want to start all instances outside the loop: Use list comprehension to generate a list of instance ids and pass it to start_instances

instance_ids = [instance.id for instance in instances]
ec2.start_instances(InstanceIds=instance_ids)
answered Mar 28, 2018 at 15:29

4 Comments

I tried converting it to list, Its not working.. since the instance.id is of type <class 'boto3.resources.factory.ec2.Instance'> which is not getting converted into list.
Are you sure? instance.id is a string.
When I checked the type of instance.id It shows this <class 'boto3.resources.factory.ec2.Instance'> and I also tried both of your suggestions. I am getting error ec2.ServiceResource' object has no attribute 'start_instances'
Thanks for your effort @helloV !!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.