Skip to content

Instantly share code, notes, and snippets.

@jeffnelson
Created March 1, 2018 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffnelson/23e53e76797ffd24875d5b246a685c3e to your computer and use it in GitHub Desktop.
Save jeffnelson/23e53e76797ffd24875d5b246a685c3e to your computer and use it in GitHub Desktop.
groovy script using aws java sdk to tell aws target groups to switch targets between blue and green
@Grab('com.amazonaws:aws-java-sdk-ec2:1.11.281')
@Grab('com.amazonaws:aws-java-sdk-elasticloadbalancingv2:1.11.281')
import groovy.json.JsonOutput
import com.amazonaws.services.ec2.AmazonEC2
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder
import com.amazonaws.services.ec2.model.Filter
import com.amazonaws.services.ec2.model.DescribeInstancesRequest
import com.amazonaws.services.ec2.model.DescribeInstancesResult
import com.amazonaws.services.elasticloadbalancingv2.AmazonElasticLoadBalancing
import com.amazonaws.services.elasticloadbalancingv2.AmazonElasticLoadBalancingClientBuilder
import com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetGroupsRequest
import com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetGroupsResult
import com.amazonaws.services.elasticloadbalancingv2.model.TargetDescription
import com.amazonaws.services.elasticloadbalancingv2.model.DeregisterTargetsRequest
import com.amazonaws.services.elasticloadbalancingv2.model.RegisterTargetsRequest
import com.amazonaws.services.elasticloadbalancingv2.model.DescribeTagsRequest
import com.amazonaws.services.elasticloadbalancingv2.model.DescribeTagsResult
import com.amazonaws.auth.EC2ContainerCredentialsProviderWrapper
def hasTags(tagDesc, appName, env, role) {
return tagDesc.tags.count { it.key == 'AppName' && it.value == appName } >= 1 &&
tagDesc.tags.count { it.key == 'Env' && it.value == env } >= 1 &&
tagDesc.tags.count { it.key == 'Role' && it.value == role } >= 1
}
def registerTargets(elbClient, targetGroupArn, targetIds) {
elbClient.registerTargets(new RegisterTargetsRequest()
.withTargetGroupArn(targetGroupArn)
.withTargets(targetIds.collect { new TargetDescription().withId(it).withPort(80) }))
}
def deregisterTargets(elbClient, targetGroupArn, targetIds) {
elbClient.deregisterTargets(new DeregisterTargetsRequest()
.withTargetGroupArn(targetGroupArn)
.withTargets(targetIds.collect { new TargetDescription().withId(it) }))
}
def appName = args[0]
def env = args[1]
def oldColor = args[2]
def newColor = args[3]
AmazonEC2 ec2Client = AmazonEC2ClientBuilder.standard()
.withCredentials(new EC2ContainerCredentialsProviderWrapper())
.withRegion('us-east-1')
.build()
AmazonElasticLoadBalancing elbClient = AmazonElasticLoadBalancingClientBuilder.standard()
.withCredentials(new EC2ContainerCredentialsProviderWrapper())
.withRegion('us-east-1')
.build()
try {
DescribeInstancesResult newColorInstancesResponse = ec2Client.describeInstances(new DescribeInstancesRequest().withFilters(
new Filter().withName("tag:AppName").withValues(appName),
new Filter().withName("tag:Env").withValues(env),
new Filter().withName("tag:Color").withValues(newColor)
))
DescribeInstancesResult oldColorInstancesResponse = ec2Client.describeInstances(new DescribeInstancesRequest().withFilters(
new Filter().withName("tag:AppName").withValues(appName),
new Filter().withName("tag:Env").withValues(env),
new Filter().withName("tag:Color").withValues(oldColor)
))
DescribeTargetGroupsResult tgResponse = elbClient.describeTargetGroups(new DescribeTargetGroupsRequest())
def tgArns = tgResponse.targetGroups.collect { it.targetGroupArn }
DescribeTagsResult tgTags = elbClient.describeTags(new DescribeTagsRequest().withResourceArns(tgArns))
def trafficTgArns = tgTags.tagDescriptions.findAll { hasTags(it, appName, env, '80') }
assert trafficTgArns.size() == 1
trafficTgArns.each { tagDesc ->
println 'traffic...'
println "${tagDesc.resourceArn}"
tagDesc.tags.each { tag -> println "${tag.key}=${tag.value}" }
}
def testingTgArns = tgTags.tagDescriptions.findAll { hasTags(it, appName, env, '8443') }
assert testingTgArns.size() == 1
testingTgArns.each { tagDesc ->
println 'testing...'
println "${tagDesc.resourceArn}"
tagDesc.tags.each { tag -> println "${tag.key}=${tag.value}" }
}
def trafficTgArn = trafficTgArns[0].resourceArn
def newColorInstanceIds = newColorInstancesResponse.reservations.collect {
it.instances.collect { instance -> instance.instanceId }
}.flatten()
def testingTgArn = testingTgArns[0].resourceArn
def oldColorInstanceIds = oldColorInstancesResponse.reservations.collect {
it.instances.collect { instance -> instance.instanceId }
}.flatten()
println "registering $newColor instances with traffic target group..."
registerTargets(elbClient, trafficTgArn, newColorInstanceIds)
println "deregistering $oldColor instances from traffic target group..."
deregisterTargets(elbClient, trafficTgArn, oldColorInstanceIds)
println "registering $oldColor instances with testing target group..."
registerTargets(elbClient, testingTgArn, oldColorInstanceIds)
println "deregistering $newColor instances from testing target group..."
deregisterTargets(elbClient, testingTgArn, newColorInstanceIds)
println 'color swap completed successfully'
} catch (Exception e) {
println 'color swap failed due to some exception...'
e.printStackTrace()
System.exit(1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment