automate-start-stop-server / start-server.py
start-server.py
Raw
import boto3

def lambda_handler(event, context):
    try:
        client = boto3.client('ec2')
        response = client.describe_instances(
            Filters=[
                {
                    'Name': 'instance-state-name',
                    'Values': [
                        'stopped',
                    ]
                },
            ],
        )
        if (len(response['Reservations']) != 0):
            instance_id = response['Reservations'][0]['Instances'][0]['InstanceId']

            start_response = client.start_instances(
                InstanceIds=[
                    instance_id,
                ]
            )
            print(start_response)
        else:
            print('No stopped instance')
    except:
        print('Unable to start instance with id {}'.format(instance_id))

'''if __name__ == "__main__":
    lambda_handler()'''