Bernd Kuhlens portfolio
For this blog here i have an ec2 instance. To avoid costs, i don’t have a loadbalancer, just an ASG with a desired size of 1 instance. Sometimes the DB-connection can become buggy in which case the easiest automated fix is to simply restart this instance. Without LB that’s not totally trivial so I drop a note here.
Here’s the lambda code i found on the internet, make sure you choose python 2.7 (and not python 3.x)
import boto3
region = ‘us-west-1’
instances = [‘i-12345cb6de4f78g9h’, ‘i-08ce9b2d7eccf6d26’]
def lambda_handler(event, context):
ec2 = boto3.client(‘ec2’, region_name=region)
ec2.stop_instances(InstanceIds=instances)
print ‘stopped your instances: ‘ + str(instances)