Created
May 2, 2016 20:15
-
-
Save leogrim/d0573acebbb92505b9c03362bf6d9d9a to your computer and use it in GitHub Desktop.
ELB Autoregistration by Martin Raison
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// retrieve instance id using AWS instance metadata and Play! WS API | |
val request = WS.url("http://169.254.169.254/latest/meta-data/instance-id") | |
val instanceId = Await.result(request.get(), 1 minute).body // careful when blocking | |
// set up AWS EC2 and ELB clients | |
val EC2Client = new AmazonEC2Client | |
val ELBClient = new AmazonElasticLoadBalancingClient | |
val region = Region.getRegion(Regions.<your_region>) | |
EC2Client.setRegion(region) | |
ELBClient.setRegion(region) | |
// retrieve instance's ELB tag | |
val request = new DescribeTagsRequest(Seq( | |
new Filter("key",Seq("ELB")), | |
new Filter("resource-id",Seq(instanceId)) | |
)) | |
val result = EC2Client.describeTags(request) | |
val ELBName = result.getTags.head.getValue // assuming tag is there | |
// register to ELB | |
def registerToLoadBalancer() = { | |
val instance = new Instance(instanceId) | |
val request = new RegisterInstancesWithLoadBalancerRequest(ELBName, Seq(instance)) | |
ELBClient.registerInstancesWithLoadBalancer(request) | |
} | |
// deregister from ELB | |
def deregisterFromLoadBalancer() = { | |
val instance = new Instance(instanceId) | |
val request = new DeregisterInstancesFromLoadBalancerRequest(ELBName, Seq(instance)) | |
ELBClient.deregisterInstancesFromLoadBalancer(request) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment