Skip to content

Commit 6eb1464

Browse files
authored
Enable bluegreen script to assume a role in the target account (#22)
* Enable bluegreen script to assume a role in the target account * Small fixes
1 parent 62f8299 commit 6eb1464

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

bluegreen.py

+34-7
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99

1010

1111
def main(argv):
12-
helptext = 'bluegreen.py -f <path to terraform project> -a <ami> -c <command> -t <timeout> -e <environment.tfvars> -i <inactive-desired> <path>'
12+
helptext = 'bluegreen.py -f <path to terraform project> -a <ami> -c <command> -t <timeout> -e <environment.tfvars> -i <inactive-desired> [-r <assume-role-arn>]'
1313

1414
try:
15-
opts, args = getopt.getopt(argv, "hsf:a:c:t:e:i:", ["folder=", "ami=", "command=", "timeout=", "environment=", "inactive-desired="])
15+
opts, args = getopt.getopt(argv, "hsf:a:c:t:e:i:r:", ["folder=", "ami=", "command=", "timeout=", "environment=", "inactive-desired=", "role-arn="])
1616
except getopt.GetoptError:
1717
print helptext
1818
sys.exit(2)
@@ -34,6 +34,8 @@ def main(argv):
3434
environment = arg
3535
elif opt in ("-i", "--inactive-desired"):
3636
inactiveDesired = arg
37+
elif opt in ("-r", "--role-arn"):
38+
assumeRoleArn = arg
3739
elif opt in ("-s"):
3840
stopScaling = True
3941
else:
@@ -62,6 +64,9 @@ def main(argv):
6264
if 'inactiveDesired' not in locals():
6365
inactiveDesired = 1
6466

67+
if 'assumeRoleArn' not in locals():
68+
assumeRoleArn = None
69+
6570
if 'stopScaling' not in locals():
6671
stopScaling = False
6772

@@ -73,6 +78,10 @@ def main(argv):
7378
agBlue = getTerraformOutput(projectPath, 'blue_asg_id')
7479
agGreen = getTerraformOutput(projectPath, 'green_asg_id')
7580

81+
# Get a boto3 session
82+
global awsSession
83+
awsSession = getBotoSession(assumeRoleArn)
84+
7685
# Retrieve autoscaling groups information
7786
info = getAutoscalingInfo(agBlue, agGreen)
7887

@@ -109,6 +118,24 @@ def main(argv):
109118
print 'Deactivating the autoscaling'
110119
stopAutoscaling(info, active, ami, command, projectPath, environment)
111120

121+
def getBotoSession(assumeRoleArn):
122+
if assumeRoleArn:
123+
sts_client = boto3.client('sts')
124+
125+
# Call the assume_role method of the STSConnection object and pass the role
126+
# ARN and a role session name.
127+
assumed_role_object = sts_client.assume_role(
128+
RoleArn = assumeRoleArn,
129+
RoleSessionName = "bluegreen"
130+
)
131+
132+
return boto3.Session(
133+
aws_access_key_id = assumed_role_object['Credentials']['AccessKeyId'],
134+
aws_secret_access_key = assumed_role_object['Credentials']['SecretAccessKey'],
135+
aws_session_token = assumed_role_object['Credentials']['SessionToken'],
136+
)
137+
else:
138+
return boto3.Session()
112139

113140
def getTerraformOutput(projectPath, output):
114141
process = subprocess.Popen('terraform output ' + output, shell=True, cwd=projectPath, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
@@ -122,7 +149,7 @@ def getTerraformOutput(projectPath, output):
122149

123150

124151
def getAutoscalingInfo(blue, green):
125-
client = boto3.client('autoscaling')
152+
client = awsSession.client('autoscaling')
126153
response = client.describe_auto_scaling_groups(
127154
AutoScalingGroupNames=[
128155
blue,
@@ -141,7 +168,7 @@ def getLoadbalancers(info, type):
141168

142169

143170
def getAmi(launchconfig):
144-
client = boto3.client('autoscaling')
171+
client = awsSession.client('autoscaling')
145172
response = client.describe_launch_configurations(
146173
LaunchConfigurationNames=[
147174
launchconfig,
@@ -152,7 +179,7 @@ def getAmi(launchconfig):
152179

153180

154181
def getLaunchconfigDate(launchconfig):
155-
client = boto3.client('autoscaling')
182+
client = awsSession.client('autoscaling')
156183
response = client.describe_launch_configurations(
157184
LaunchConfigurationNames=[
158185
launchconfig,
@@ -322,7 +349,7 @@ def updateAutoscaling(command, blueMax, blueMin, blueDesired, blueAMI, greenMax,
322349

323350

324351
def checkScalingStatus(elbs, albs, desiredInstanceCount):
325-
client = boto3.client('elb')
352+
client = awsSession.client('elb')
326353
for elb in elbs:
327354
response = client.describe_instance_health(
328355
LoadBalancerName=elb
@@ -334,7 +361,7 @@ def checkScalingStatus(elbs, albs, desiredInstanceCount):
334361
print 'ELB: ' + state['State']
335362
if state['State'] != 'InService':
336363
return False
337-
client = boto3.client('elbv2')
364+
client = awsSession.client('elbv2')
338365
for alb in albs:
339366
response = client.describe_target_health(
340367
TargetGroupArn=alb,

deploy-bluegreen.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ params:
1111
TF_VERSION: "0.11.7"
1212
AWS_DEFAULT_REGION: eu-west-1
1313
TF_ENVIRONMENT:
14+
ASSUME_ROLE_ARN:
1415

1516
inputs:
1617
- name: terraform-repo
@@ -45,4 +46,4 @@ run:
4546
terraform init
4647
terraform workspace select $TF_ENVIRONMENT
4748
# Deploy
48-
$WORKDIR/terraform-bluegreen/bluegreen.py -f $WORKDIR/terraform-repo/$TF_PROJECT_FOLDER -a $AMI_ID -c "apply -auto-approve" -t 500 -e $WORKDIR/terraform-repo/$TF_PROJECT_FOLDER/$TF_ENVIRONMENT.tfvars
49+
$WORKDIR/terraform-bluegreen/bluegreen.py -f $WORKDIR/terraform-repo/$TF_PROJECT_FOLDER -a $AMI_ID -c "apply -auto-approve" -t 500 -e $WORKDIR/terraform-repo/$TF_PROJECT_FOLDER/$TF_ENVIRONMENT.tfvars -r $ASSUME_ROLE_ARN

0 commit comments

Comments
 (0)