Skip to content

Commit cc93588

Browse files
author
James Healy
authored
Merge pull request #751 from buildkite/disable-az-rebalancing
The agent autoscaling group should never rebalance availability zones
2 parents b65b7be + db1040c commit cc93588

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

templates/aws-stack.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,61 @@ Resources:
939939
AutoScalingReplacingUpdate:
940940
WillReplace: true
941941

942+
AsgProcessSuspenderRole:
943+
Type: AWS::IAM::Role
944+
Properties:
945+
AssumeRolePolicyDocument:
946+
Version: 2012-10-17
947+
Statement:
948+
- Effect: Allow
949+
Principal:
950+
Service:
951+
- lambda.amazonaws.com
952+
Action:
953+
- sts:AssumeRole
954+
ManagedPolicyArns:
955+
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
956+
Policies:
957+
- PolicyName: AsgProcessModification
958+
PolicyDocument:
959+
Version: 2012-10-17
960+
Statement:
961+
- Effect: Allow
962+
Action:
963+
- 'autoscaling:SuspendProcesses'
964+
Resource: !Sub arn:${AWS::Partition}:autoscaling:${AWS::Region}:${AWS::AccountId}:autoScalingGroup:*:autoScalingGroupName/${AgentAutoScaleGroup}
965+
966+
AzRebalancingSuspenderFunction:
967+
Type: AWS::Lambda::Function
968+
Properties:
969+
Description: 'Disables AZ Rebalancing on the agent ASG'
970+
Code:
971+
ZipFile: |
972+
import cfnresponse
973+
import boto3
974+
def handler(event, context):
975+
try:
976+
if event['RequestType'] == 'Delete':
977+
cfnresponse.send(event, context, cfnresponse.SUCCESS, {}, "CustomResourcePhysicalID")
978+
else:
979+
client = boto3.client('autoscaling')
980+
props = event['ResourceProperties']
981+
response = client.suspend_processes(AutoScalingGroupName=props['AutoScalingGroupName'], ScalingProcesses=['AZRebalance'])
982+
cfnresponse.send(event, context, cfnresponse.SUCCESS, {}, "CustomResourcePhysicalID")
983+
except BaseException as err:
984+
print('ERROR: ', err)
985+
cfnresponse.send(event, context, cfnresponse.FAILED, {}, "CustomResourcePhysicalID")
986+
Handler: index.handler
987+
Role: !GetAtt AsgProcessSuspenderRole.Arn
988+
Runtime: 'python3.7'
989+
990+
AzRebalancingSuspender:
991+
Type: AWS::CloudFormation::CustomResource
992+
Version: 1.0
993+
Properties:
994+
ServiceToken: !GetAtt AzRebalancingSuspenderFunction.Arn
995+
AutoScalingGroupName: !Ref AgentAutoScaleGroup
996+
942997
SecurityGroup:
943998
Type: AWS::EC2::SecurityGroup
944999
Condition: CreateSecurityGroup

0 commit comments

Comments
 (0)