FDS
FDS

Reputation: 31

Lambda function to start EC2 instance based on tag

I have created a python Lambda Function in AWS to start some EC2 instances based on a TAG that's being deployed to them. It checks if the instance is stopped and only run on them.

import boto3
import logging
ec2 = boto3.resource('ec2')
def lambda_handler(event, context):
    filters = [{
        'Name': 'tag:STARTUP',
        'Values': ['YES']
    },
    {
        'Name': 'instance-state-name', 
        'Values': ['stopped']
    }]
instances = instances.filter(Filters=filters)
stoppedInstances = [instance.id for instance in instances]
if len(stoppedInstances) > 0:
    startingUp = instances.filter(instances).start()

When I try to run it, I get the following error:

START RequestId: XXX Version: $LATEST
filter() takes 1 positional argument but 2 were given: TypeError
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 17, in lambda_handler
    startingUp = ec2.instances.filter(instances).start()
TypeError: filter() takes 1 positional argument but 2 were given

Although everywhere I look the FILTER variable is able to handle more than one argument but somehow I am only able to use one?

I'm using Python 3.6 runtime and I am using the same role as other function that works correctly to start servers (based only on time).

Can you please advise? Thank you!

Upvotes: 1

Views: 3289

Answers (1)

FDS
FDS

Reputation: 31

This did the trick @ last line! Thank you for your comments that pointed me to the right direction :)

startingUp = ec2.instances.filter(InstanceIds=stoppedInstances).start()

Upvotes: 2

Related Questions