Reputation: 595
I prepared lambda function, I am using python boto3 module, code below. Now I am facing error message "errorMessage": "start_instances() only accepts keyword arguments."
. What could be the problem, I was using this automation, and for two weeks I am facing this error.
import boto3
def lambda_handler(event, context):
client = boto3.client('ec2')
ec2_regions = [region['RegionName'] for region in client.describe_regions()['Regions']]
for region in ec2_regions:
ec2 = boto3.resource('ec2',region_name=region)
instances = ec2.instances.filter(Filters=[{'Name': 'instance-state-name', 'Values': ['stopped']}])
StoppedInstances = [instance.id for instance in instances]
for i in StoppedInstances:
startingInstances = ec2.instances.start(i)
print(startingInstances)
print(ec2_regions)
Updated version
import boto3
def lambda_handler(event, context):
client = boto3.client('ec2')
#region = 'eu-west-1'
ec2_regions = [region['RegionName'] for region in client.describe_regions()['Regions']]
for region in ec2_regions:
ec2 = boto3.resource('ec2',region_name=region)
instances = ec2.instances.filter(Filters=[{'Name': 'instance-state-name', 'Values': ['stopped']}])
StoppedInstances = [instance.id for instance in instances]
print(StoppedInstances)
print(ec2_regions)
client.start_instances(InstanceIds=StoppedInstances)
Lambda role config
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"ec2:DescribeInstances",
"ec2:StartInstances",
"ec2:StopInstances",
"ec2:DescribeRegions",
"ec2:DescribeInstanceStatus"
],
"Resource": "*"
}
]
}
Corrected and working code below:
import boto3
def lambda_handler(event, context):
client = boto3.client('ec2')
#region = 'eu-west-1'
ec2_regions = [region['RegionName'] for region in client.describe_regions()['Regions']]
for region in ec2_regions:
client = boto3.client('ec2',region_name=region)
ec2 = boto3.resource('ec2',region_name=region)
instances = ec2.instances.filter(Filters=[{'Name': 'instance-state-name', 'Values': ['stopped']}])
StoppedInstances = [instance.id for instance in instances]
for i in StoppedInstances:
client.start_instances(InstanceIds=[i])
Upvotes: 1
Views: 2022
Reputation: 238587
I think you could try the following.
Instead
for i in StoppedInstances:
startingInstances = ec2.instances.start(i)
Can use start_instances:
client.start_instances(InstanceIds=StoppedInstances)
Upvotes: 1