Skip to content

Commit d9cc963

Browse files
authored
Merge pull request #685 from buildkite/use-sar
Import Lambda based autoscaler from the Severless Application Repository
2 parents 49ea172 + a23c6ee commit d9cc963

File tree

3 files changed

+46
-159
lines changed

3 files changed

+46
-159
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,15 @@ create-stack: build/aws-stack.yml env-STACK_NAME
146146
--stack-name $(STACK_NAME) \
147147
--disable-rollback \
148148
--template-body "file://$(PWD)/build/aws-stack.yml" \
149-
--capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM \
149+
--capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM CAPABILITY_AUTO_EXPAND \
150150
--parameters "$$(cat config.json)"
151151

152152
update-stack: build/aws-stack.yml env-STACK_NAME
153153
aws cloudformation update-stack \
154154
--output text \
155155
--stack-name $(STACK_NAME) \
156156
--template-body "file://$(PWD)/build/aws-stack.yml" \
157-
--capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM \
157+
--capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM CAPABILITY_AUTO_EXPAND \
158158
--parameters "$$(cat config.json)"
159159

160160

packer/linux/conf/bin/bk-install-elastic-stack.sh

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,7 @@ else
133133
BUILDKITE_AGENT_GIT_MIRRORS_PATH=""
134134
fi
135135

136-
# If the agent token path is set, use that instead of BUILDKITE_AGENT_TOKEN
137-
if [[ -n "${BUILDKITE_AGENT_TOKEN_PATH}" ]] ; then
138-
BUILDKITE_AGENT_TOKEN="$(aws ssm get-parameter --name "${BUILDKITE_AGENT_TOKEN_PATH}" --with-decryption --query Parameter.Value --output text)"
139-
fi
136+
BUILDKITE_AGENT_TOKEN="$(aws ssm get-parameter --name "${BUILDKITE_AGENT_TOKEN_PATH}" --with-decryption --query Parameter.Value --output text)"
140137

141138
cat << EOF > /etc/buildkite-agent/buildkite-agent.cfg
142139
name="${BUILDKITE_STACK_NAME}-${INSTANCE_ID}-%spawn"

templates/aws-stack.yml

Lines changed: 43 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
AWSTemplateFormatVersion: "2010-09-09"
33
Description: "Buildkite stack %v"
44

5+
Transform: AWS::Serverless-2016-10-31
6+
57
Metadata:
68
AWS::CloudFormation::Interface:
79
ParameterGroups:
810
- Label:
911
default: Buildkite Configuration
1012
Parameters:
11-
- BuildkiteAgentToken
1213
- BuildkiteAgentTokenParameterStorePath
1314
- BuildkiteAgentTokenParameterStoreKMSKey
15+
- BuildkiteAgentToken
1416
- BuildkiteQueue
1517

1618
- Label:
@@ -104,7 +106,7 @@ Parameters:
104106
Default: "stable"
105107

106108
BuildkiteAgentToken:
107-
Description: Buildkite agent registration token
109+
Description: Buildkite agent registration token. Deprecated, use BuildkiteAgentTokenParameterStorePath instead.
108110
Type: String
109111
NoEcho: true
110112
Default: ""
@@ -507,11 +509,12 @@ Conditions:
507509
UseECR:
508510
!Not [ !Equals [ !Ref ECRAccessPolicy, "none" ] ]
509511

510-
UseSSMAgentToken:
512+
UseCustomerManagedParameterPath:
511513
!Not [ !Equals [ !Ref BuildkiteAgentTokenParameterStorePath, "" ] ]
512-
513514
UseCustomerManagedKeyForParameterStore:
514515
!Not [ !Equals [ !Ref BuildkiteAgentTokenParameterStoreKMSKey, "" ] ]
516+
CreateAgentTokenParameter:
517+
!Equals [ !Ref BuildkiteAgentTokenParameterStorePath, "" ]
515518

516519
HasVariableSize:
517520
!Not [ !Equals [ !Ref MaxSize, !Ref MinSize ] ]
@@ -559,28 +562,6 @@ Mappings:
559562
poweruser : { Policy: 'arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryPowerUser' }
560563
full : { Policy: 'arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryFullAccess' }
561564

562-
LambdaBucket:
563-
us-east-1 : { Bucket: "buildkite-lambdas" }
564-
us-east-2 : { Bucket: "buildkite-lambdas-us-east-2" }
565-
us-west-1 : { Bucket: "buildkite-lambdas-us-west-1" }
566-
us-west-2 : { Bucket: "buildkite-lambdas-us-west-2" }
567-
af-south-1 : { Bucket: "buildkite-lambdas-af-south-1" }
568-
ap-east-1 : { Bucket: "buildkite-lambdas-ap-east-1" }
569-
ap-south-1 : { Bucket: "buildkite-lambdas-ap-south-1" }
570-
ap-northeast-2 : { Bucket: "buildkite-lambdas-ap-northeast-2" }
571-
ap-northeast-1 : { Bucket: "buildkite-lambdas-ap-northeast-1" }
572-
ap-southeast-2 : { Bucket: "buildkite-lambdas-ap-southeast-2" }
573-
ap-southeast-1 : { Bucket: "buildkite-lambdas-ap-southeast-1" }
574-
ca-central-1 : { Bucket: "buildkite-lambdas-ca-central-1" }
575-
eu-central-1 : { Bucket: "buildkite-lambdas-eu-central-1" }
576-
eu-west-1 : { Bucket: "buildkite-lambdas-eu-west-1" }
577-
eu-west-2 : { Bucket: "buildkite-lambdas-eu-west-2" }
578-
eu-south-1 : { Bucket: "buildkite-lambdas-eu-south-1" }
579-
eu-west-3 : { Bucket: "buildkite-lambdas-eu-west-3" }
580-
eu-north-1 : { Bucket: "buildkite-lambdas-eu-north-1" }
581-
me-south-1 : { Bucket: "buildkite-lambdas-me-south-1" }
582-
sa-east-1 : { Bucket: "buildkite-lambdas-sa-east-1" }
583-
584565
# Generated from Makefile via build/mappings.yml
585566
AWSRegion2AMI: { linuxamd64: !Ref ImageId, linuxarm64: !Ref ImageId, windows: !Ref ImageId }
586567

@@ -672,6 +653,14 @@ Resources:
672653
SubnetId: !Ref Subnet1
673654
RouteTableId: !Ref Routes
674655

656+
BuildkiteAgentTokenParameter:
657+
Type: AWS::SSM::Parameter
658+
Condition: CreateAgentTokenParameter
659+
Properties:
660+
Name: !Sub "/${AWS::StackName}/buildkite/agent-token"
661+
Type: String
662+
Value: !Ref BuildkiteAgentToken
663+
675664
# Allow ec2 instances to assume a role and be granted the IAMPolicies
676665
IAMInstanceProfile:
677666
Type: AWS::IAM::InstanceProfile
@@ -714,17 +703,16 @@ Resources:
714703
- kms:Decrypt
715704
Resource: !Sub arn:aws:kms:${AWS::Region}:${AWS::AccountId}:key/${BuildkiteAgentTokenParameterStoreKMSKey}
716705
- !Ref 'AWS::NoValue'
717-
- !If
718-
- UseSSMAgentToken
719-
- PolicyName: ReadAgentToken
720-
PolicyDocument:
721-
Version: '2012-10-17'
722-
Statement:
723-
- Effect: Allow
724-
Action:
725-
- ssm:GetParameter
726-
Resource: !Sub arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter${BuildkiteAgentTokenParameterStorePath}
727-
- !Ref 'AWS::NoValue'
706+
- PolicyName: ReadAgentToken
707+
PolicyDocument:
708+
Version: '2012-10-17'
709+
Statement:
710+
- Effect: Allow
711+
Action: ssm:GetParameter
712+
Resource:
713+
!Sub
714+
- arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter${ParameterPath}
715+
- ParameterPath: !If [ UseCustomerManagedParameterPath, !Ref BuildkiteAgentTokenParameterStorePath, !Ref BuildkiteAgentTokenParameter ]
728716
AssumeRolePolicyDocument:
729717
Statement:
730718
- Effect: Allow
@@ -923,8 +911,7 @@ Resources:
923911
$Env:BUILDKITE_STACK_VERSION="%v"
924912
$Env:BUILDKITE_SCALE_IN_IDLE_PERIOD="${ScaleInIdlePeriod}"
925913
$Env:BUILDKITE_SECRETS_BUCKET="${LocalSecretsBucket}"
926-
$Env:BUILDKITE_AGENT_TOKEN="${BuildkiteAgentToken}"
927-
$Env:BUILDKITE_AGENT_TOKEN_PATH="${BuildkiteAgentTokenParameterStorePath}"
914+
$Env:BUILDKITE_AGENT_TOKEN_PATH="${AgentTokenPath}"
928915
$Env:BUILDKITE_AGENTS_PER_INSTANCE="${AgentsPerInstance}"
929916
$Env:BUILDKITE_AGENT_TAGS="${BuildkiteAgentTags}"
930917
$Env:BUILDKITE_AGENT_TIMESTAMP_LINES="${BuildkiteAgentTimestampLines}"
@@ -947,6 +934,7 @@ Resources:
947934
</powershell>
948935
- {
949936
LocalSecretsBucket: !If [ CreateSecretsBucket, !Ref ManagedSecretsBucket, !Ref SecretsBucket ],
937+
AgentTokenPath: !If [ UseCustomerManagedParameterPath, !Ref BuildkiteAgentTokenParameterStorePath, !Ref BuildkiteAgentTokenParameter ],
950938
}
951939
- !Sub
952940
- |
@@ -964,8 +952,7 @@ Resources:
964952
BUILDKITE_STACK_VERSION=%v \
965953
BUILDKITE_SCALE_IN_IDLE_PERIOD=${ScaleInIdlePeriod} \
966954
BUILDKITE_SECRETS_BUCKET="${LocalSecretsBucket}" \
967-
BUILDKITE_AGENT_TOKEN="${BuildkiteAgentToken}" \
968-
BUILDKITE_AGENT_TOKEN_PATH="${BuildkiteAgentTokenParameterStorePath}" \
955+
BUILDKITE_AGENT_TOKEN_PATH="${AgentTokenPath}" \
969956
BUILDKITE_AGENTS_PER_INSTANCE="${AgentsPerInstance}" \
970957
BUILDKITE_AGENT_TAGS="${BuildkiteAgentTags}" \
971958
BUILDKITE_AGENT_TIMESTAMP_LINES="${BuildkiteAgentTimestampLines}" \
@@ -987,6 +974,7 @@ Resources:
987974
--==BOUNDARY==--
988975
- {
989976
LocalSecretsBucket: !If [ CreateSecretsBucket, !Ref ManagedSecretsBucket, !Ref SecretsBucket ],
977+
AgentTokenPath: !If [ UseCustomerManagedParameterPath, !Ref BuildkiteAgentTokenParameterStorePath, !Ref BuildkiteAgentTokenParameter ],
990978
}
991979

992980
AgentAutoScaleGroup:
@@ -1114,117 +1102,19 @@ Resources:
11141102
ToPort: 22
11151103
CidrIp: 0.0.0.0/0
11161104

1117-
AutoscalingLambdaExecutionRole:
1118-
Type: AWS::IAM::Role
1119-
Condition: HasVariableSize
1120-
Properties:
1121-
PermissionsBoundary: !If [ SetInstanceRolePermissionsBoundaryARN, !Ref InstanceRolePermissionsBoundaryARN, !Ref "AWS::NoValue" ]
1122-
Path: "/"
1123-
AssumeRolePolicyDocument:
1124-
Version: '2012-10-17'
1125-
Statement:
1126-
- Effect: Allow
1127-
Principal:
1128-
Service:
1129-
- lambda.amazonaws.com
1130-
Action:
1131-
- sts:AssumeRole
1132-
ManagedPolicyArns:
1133-
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
1134-
Policies:
1135-
- PolicyName: AutoScalingGroups
1136-
PolicyDocument:
1137-
Version: '2012-10-17'
1138-
Statement:
1139-
- Effect: Allow
1140-
Action:
1141-
- autoscaling:DescribeAutoScalingGroups
1142-
- autoscaling:SetDesiredCapacity
1143-
Resource: '*'
1144-
- PolicyName: WriteCloudwatchMetrics
1145-
PolicyDocument:
1146-
Version: '2012-10-17'
1147-
Statement:
1148-
- Effect: Allow
1149-
Action:
1150-
- cloudwatch:PutMetricData
1151-
Resource: '*'
1152-
- !If
1153-
- UseCustomerManagedKeyForParameterStore
1154-
- PolicyName: DecryptAgentToken
1155-
PolicyDocument:
1156-
Version: '2012-10-17'
1157-
Statement:
1158-
- Effect: Allow
1159-
Action:
1160-
- kms:Decrypt
1161-
Resource: !Sub arn:aws:kms:${AWS::Region}:${AWS::AccountId}:key/${BuildkiteAgentTokenParameterStoreKMSKey}
1162-
- !Ref 'AWS::NoValue'
1163-
- !If
1164-
- UseSSMAgentToken
1165-
- PolicyName: ReadAgentToken
1166-
PolicyDocument:
1167-
Version: '2012-10-17'
1168-
Statement:
1169-
- Effect: Allow
1170-
Action:
1171-
- ssm:GetParameter
1172-
Resource: !Sub arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter${BuildkiteAgentTokenParameterStorePath}
1173-
- !Ref 'AWS::NoValue'
1174-
1175-
# This mirrors the group that would be created by the lambda, but enforces
1176-
# a retention period and also ensures it's removed when the stack is removed
1177-
AutoscalingLogGroup:
1178-
Type: "AWS::Logs::LogGroup"
1179-
Condition: HasVariableSize
1180-
Properties:
1181-
LogGroupName: !Join ["/", ["/aws/lambda", !Ref AutoscalingFunction]]
1182-
RetentionInDays: 1
1183-
1184-
AutoscalingFunction:
1185-
Type: AWS::Lambda::Function
1105+
Autoscaling:
1106+
Type: AWS::Serverless::Application
11861107
Condition: HasVariableSize
11871108
Properties:
1188-
Code:
1189-
S3Bucket: { 'Fn::FindInMap': [LambdaBucket, !Ref 'AWS::Region', 'Bucket'] }
1190-
S3Key: "buildkite-agent-scaler/v1.1.1/handler.zip"
1191-
Role: !GetAtt AutoscalingLambdaExecutionRole.Arn
1192-
Timeout: 120
1193-
Handler: handler
1194-
Runtime: go1.x
1195-
MemorySize: 128
1196-
Environment:
1197-
Variables:
1198-
BUILDKITE_AGENT_TOKEN: !If [ UseSSMAgentToken, !Ref 'AWS::NoValue', !Ref BuildkiteAgentToken ]
1199-
BUILDKITE_AGENT_TOKEN_SSM_KEY: !Ref BuildkiteAgentTokenParameterStorePath
1200-
BUILDKITE_QUEUE: !Ref BuildkiteQueue
1201-
AGENTS_PER_INSTANCE: !Ref AgentsPerInstance
1202-
CLOUDWATCH_METRICS: "1"
1203-
DISABLE_SCALE_IN: "1"
1204-
ASG_NAME: !Ref AgentAutoScaleGroup
1205-
MIN_SIZE: !Ref MinSize
1206-
MAX_SIZE: !Ref MaxSize
1207-
SCALE_OUT_FACTOR: !Ref ScaleOutFactor
1208-
INCLUDE_WAITING: !Ref ScaleOutForWaitingJobs
1209-
LAMBDA_TIMEOUT: "50s"
1210-
LAMBDA_INTERVAL: "10s"
1211-
1212-
AutoscalingLambdaScheduledRule:
1213-
Type: "AWS::Events::Rule"
1214-
Condition: HasVariableSize
1215-
Properties:
1216-
Description: "ScheduledRule"
1217-
ScheduleExpression: "rate(1 minute)"
1218-
State: ENABLED
1219-
Targets:
1220-
- Arn: !GetAtt AutoscalingFunction.Arn
1221-
Id: "AutoscalingFunction"
1222-
1223-
PermissionForEventsToInvokeAutoscalingLambda:
1224-
Type: "AWS::Lambda::Permission"
1225-
Condition: HasVariableSize
1226-
Properties:
1227-
FunctionName: !Ref AutoscalingFunction
1228-
Action: "lambda:InvokeFunction"
1229-
Principal: "events.amazonaws.com"
1230-
SourceArn: !GetAtt AutoscalingLambdaScheduledRule.Arn
1109+
Location:
1110+
ApplicationId: arn:aws:serverlessrepo:us-east-1:172840064832:applications/buildkite-agent-scaler
1111+
SemanticVersion: '1.1.1'
1112+
Parameters:
1113+
BuildkiteAgentTokenParameter: !If [ UseCustomerManagedParameterPath, !Ref BuildkiteAgentTokenParameterStorePath, !Ref BuildkiteAgentTokenParameter ]
1114+
BuildkiteQueue: !Ref BuildkiteQueue
1115+
AgentsPerInstance: !Ref AgentsPerInstance
1116+
MinSize: !Ref MinSize
1117+
MaxSize: !Ref MaxSize
1118+
AgentAutoScaleGroup: !Ref AgentAutoScaleGroup
1119+
ScaleOutFactor: !Ref ScaleOutFactor
1120+
ScaleOutForWaitingJobs: !Ref ScaleOutForWaitingJobs

0 commit comments

Comments
 (0)