-1

I am getting error in my below snippet to start a EC2 instance in lambda function with the handler as lambda_function.start_handler

import boto3
   region = 'us-east-2'
   instances = ['i-04b301372b916390f']
   def start_handler(event, context):
    ec2 =boto3.client('ec2',region_name=region)
    ec2.start_instances(InstanceIds=instances)
    print('started your instances: ') + 
    str(instances)

Getting below error:

Syntax error in module 'lambda_function': unindent does not match any outer indentation level (lambda_function.py, line 8)
SBylemans
  • 1,716
  • 13
  • 27
  • Consistent indentation matters in Python: https://docs.python.org/2.0/ref/indentation.html – vahdet Oct 19 '18 at 11:34
  • 1
    Possible duplicate of [IndentationError: unindent does not match any outer indentation level](https://stackoverflow.com/questions/492387/indentationerror-unindent-does-not-match-any-outer-indentation-level) – nbwoodward Oct 19 '18 at 12:53

2 Answers2

2

Indentation matters in Python as there's no curly braces to keep the code together. The code after your definition declaration should be indented by 4 spaces:

import boto3
region = 'us-east-2'
instances = ['i-04b301372b916390f']
def start_handler(event, context):
    ec2 =boto3.client('ec2',region_name=region)
    ec2.start_instances(InstanceIds=instances)
    print('started your instances: ' + str(instances))

Another suggestion: give instances and region as an argument, because now you're relying on a global variable to be present.

def start_handler(event, context, instances, region):
    ec2 =boto3.client('ec2',region_name=region)
    ec2.start_instances(InstanceIds=instances)
    print('started your instances: ' + str(instances))

EDIT And as mentioned by ALTR put str(instances) on same line as print( ... + and inside the brackets of the print.

SBylemans
  • 1,716
  • 13
  • 27
0

Put the str(instances) on the 7th line after the +, not on the next line