/
AWS.scala
490 lines (421 loc) · 21.4 KB
/
AWS.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
package magenta.tasks
import java.nio.ByteBuffer
import com.amazonaws.{AmazonClientException, AmazonWebServiceRequest, ClientConfiguration}
import com.amazonaws.auth.{AWSCredentialsProvider, AWSCredentialsProviderChain, BasicAWSCredentials}
import com.amazonaws.regions.{RegionUtils, Region => AwsRegion}
import com.amazonaws.retry.PredefinedRetryPolicies.SDKDefaultRetryCondition
import com.amazonaws.retry.{PredefinedRetryPolicies, RetryPolicy}
import com.amazonaws.services.autoscaling.{AmazonAutoScaling, AmazonAutoScalingClientBuilder}
import com.amazonaws.services.autoscaling.model.{Instance => ASGInstance, _}
import com.amazonaws.services.cloudformation.{AmazonCloudFormation, AmazonCloudFormationClientBuilder}
import com.amazonaws.services.cloudformation.model.{Stack => AmazonStack, Tag => CfnTag, _}
import com.amazonaws.services.ec2.{AmazonEC2, AmazonEC2ClientBuilder}
import com.amazonaws.services.ec2.model.{CreateTagsRequest, DescribeInstancesRequest, Tag => EC2Tag}
import com.amazonaws.services.elasticloadbalancing.{AmazonElasticLoadBalancing => ClassicELB, AmazonElasticLoadBalancingClientBuilder => ClassicELBBuilder}
import com.amazonaws.services.elasticloadbalancingv2.{AmazonElasticLoadBalancing => ApplicationELB, AmazonElasticLoadBalancingClientBuilder => ApplicationELBBuilder}
import com.amazonaws.services.elasticloadbalancing.model.{Instance => ELBInstance, _}
import com.amazonaws.services.elasticloadbalancingv2.model.{Tag => _, _}
import com.amazonaws.services.lambda.{AWSLambda, AWSLambdaClientBuilder}
import com.amazonaws.services.lambda.model.UpdateFunctionCodeRequest
import com.amazonaws.services.s3.model.{BucketLifecycleConfiguration, CreateBucketRequest}
import com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Rule
import com.amazonaws.services.s3.{AmazonS3, AmazonS3ClientBuilder}
import com.amazonaws.services.securitytoken.{AWSSecurityTokenService, AWSSecurityTokenServiceClientBuilder}
import com.amazonaws.services.securitytoken.model.GetCallerIdentityRequest
import com.gu.management.Loggable
import magenta.{App, DeployReporter, DeploymentPackage, KeyRing, NamedStack, Region, Stack, Stage, UnnamedStack}
import scala.annotation.tailrec
import scala.collection.JavaConverters._
object S3 extends AWS {
def makeS3client(keyRing: KeyRing, region: Region, config: ClientConfiguration = clientConfiguration): AmazonS3 =
AmazonS3ClientBuilder.standard()
.withCredentials(provider(keyRing))
.withClientConfiguration(config)
.withRegion(region.name)
.build()
/**
* Check (and if necessary create) that an S3 bucket exists for the account and region. Note that it is assumed that
* the clients and region provided all match up - i.e. they are all configured to the same region and use the same
* credentials.
* @param prefix The prefix for the bucket. The resulting bucket will be <prefix>-<accountNumber>-<region>.
* @param s3Client S3 client used to check and create the bucket and set a lifecycle policy if deleteAfterDays is set
* @param stsClient The STS client that is used to retrieve the account number
* @param region The region in which the bucket should be created
* @param deleteAfterDays If set then if this bucket is created then a lifecycle policy rule is created to delete
* objects uploaded to this bucket after the number of days specified
*
* @return
*/
def accountSpecificBucket(prefix: String, s3Client: AmazonS3, stsClient: AWSSecurityTokenService,
region: Region, reporter: DeployReporter, deleteAfterDays: Option[Int] = None): String = {
val callerIdentityResponse = stsClient.getCallerIdentity(new GetCallerIdentityRequest())
val accountNumber = callerIdentityResponse.getAccount
val bucketName = s"$prefix-$accountNumber-${region.name}"
if (!s3Client.doesBucketExist(bucketName)) {
reporter.info(s"Creating bucket for this account and region: $bucketName ${region.name}")
val createBucketRequest = region.name match {
case "us-east-1" => new CreateBucketRequest(bucketName) // this needs to be special cased as setting this explicitly blows up
case otherRegion => new CreateBucketRequest(bucketName, otherRegion)
}
s3Client.createBucket(createBucketRequest)
deleteAfterDays.foreach { days =>
val daysString = s"$days day${if(days==1) "" else "s"}"
reporter.info(s"Creating lifecycle rule on bucket that deletes objects after $daysString")
s3Client.setBucketLifecycleConfiguration(
bucketName,
new BucketLifecycleConfiguration().withRules(
new Rule()
.withId(s"Rule to delete objects after $daysString")
.withStatus(BucketLifecycleConfiguration.ENABLED)
.withExpirationInDays(days)
)
)
}
}
bucketName
}
}
object Lambda extends AWS {
def makeLambdaClient(keyRing: KeyRing, region: Region): AWSLambda =
AWSLambdaClientBuilder.standard()
.withCredentials(provider(keyRing))
.withClientConfiguration(clientConfiguration)
.withRegion(region.name)
.build()
def lambdaUpdateFunctionCodeRequest(functionName: String, buffer: ByteBuffer): UpdateFunctionCodeRequest = {
val request = new UpdateFunctionCodeRequest
request.withFunctionName(functionName)
request.withZipFile(buffer)
request
}
def lambdaUpdateFunctionCodeRequest(functionName: String, s3Bucket: String, s3Key: String): UpdateFunctionCodeRequest = {
new UpdateFunctionCodeRequest()
.withFunctionName(functionName)
.withS3Bucket(s3Bucket)
.withS3Key(s3Key)
}
}
object ASG extends AWS {
def makeAsgClient(keyRing: KeyRing, region: Region): AmazonAutoScaling =
AmazonAutoScalingClientBuilder.standard()
.withCredentials(provider(keyRing))
.withClientConfiguration(clientConfiguration)
.withRegion(region.name)
.build()
def desiredCapacity(name: String, capacity: Int, client: AmazonAutoScaling) =
client.setDesiredCapacity(
new SetDesiredCapacityRequest().withAutoScalingGroupName(name).withDesiredCapacity(capacity)
)
def maxCapacity(name: String, capacity: Int, client: AmazonAutoScaling) =
client.updateAutoScalingGroup(
new UpdateAutoScalingGroupRequest().withAutoScalingGroupName(name).withMaxSize(capacity))
def isStabilized(asg: AutoScalingGroup, asgClient: AmazonAutoScaling,
elbClient: ELB.Client): Either[String, Unit] = {
def matchCapacityAndState(states: List[String], desiredState: String, checkDescription: Option[String]): Either[String, Unit] = {
val descriptionWithPrecedingSpace = checkDescription.map(d => s" $d").getOrElse("")
if (states.size != asg.getDesiredCapacity)
Left(s"Number of$descriptionWithPrecedingSpace instances (${states.size}) and ASG desired capacity (${asg.getDesiredCapacity}) don't match")
else if (!states.forall(_ == desiredState))
Left(s"Only ${states.count(_ == desiredState)} of ${states.size}$descriptionWithPrecedingSpace instances $desiredState")
else
Right(())
}
(elbName(asg), elbTargetArn(asg)) match {
case (Some(name), None) => {
val elbHealth = ELB.instanceHealth(name, elbClient.classic)
matchCapacityAndState(elbHealth, "InService", Some("ELB"))
}
case (None, Some(arn)) => {
val elbHealth = ELB.targetInstancesHealth(arn, elbClient.application)
matchCapacityAndState(elbHealth, TargetHealthStateEnum.Healthy.toString, Some("Application ELB"))
}
case (None, None) => {
val instanceStates = asg.getInstances.asScala.toList.map(_.getLifecycleState)
matchCapacityAndState(instanceStates, LifecycleState.InService.toString, None)
}
case (Some(_), Some(_)) => Left(
"Don't know how to check for stability of an ASG associated with a classic and application load balancer")
}
}
def elbName(asg: AutoScalingGroup) = asg.getLoadBalancerNames.asScala.headOption
def elbTargetArn(asg: AutoScalingGroup) = asg.getTargetGroupARNs.asScala.headOption
def cull(asg: AutoScalingGroup, instance: ASGInstance, asgClient: AmazonAutoScaling, elbClient: ELB.Client) = {
ELB.deregister(elbName(asg), elbTargetArn(asg), instance, elbClient)
asgClient.terminateInstanceInAutoScalingGroup(
new TerminateInstanceInAutoScalingGroupRequest()
.withInstanceId(instance.getInstanceId).withShouldDecrementDesiredCapacity(true)
)
}
def refresh(asg: AutoScalingGroup, client: AmazonAutoScaling) =
client.describeAutoScalingGroups(
new DescribeAutoScalingGroupsRequest().withAutoScalingGroupNames(asg.getAutoScalingGroupName)
).getAutoScalingGroups.asScala.head
def suspendAlarmNotifications(name: String, client: AmazonAutoScaling) = client.suspendProcesses(
new SuspendProcessesRequest().withAutoScalingGroupName(name).withScalingProcesses("AlarmNotification")
)
def resumeAlarmNotifications(name: String, client: AmazonAutoScaling) = client.resumeProcesses(
new ResumeProcessesRequest().withAutoScalingGroupName(name).withScalingProcesses("AlarmNotification")
)
def groupForAppAndStage(pkg: DeploymentPackage, stage: Stage, stack: Stack, client: AmazonAutoScaling,
reporter: DeployReporter): AutoScalingGroup = {
case class ASGMatch(app:App, matches:List[AutoScalingGroup])
implicit class RichAutoscalingGroup(asg: AutoScalingGroup) {
def hasTag(key: String, value: String) = asg.getTags.asScala exists { tag =>
tag.getKey == key && tag.getValue == value
}
def matchApp(app: App, stack: Stack): Boolean = {
stack match {
case UnnamedStack => hasTag("Role", pkg.name) || hasTag("App", pkg.name)
case NamedStack(stackName) => hasTag("Stack", stackName) && hasTag("App", app.name)
}
}
}
def listAutoScalingGroups(nextToken: Option[String] = None): List[AutoScalingGroup] = {
val request = new DescribeAutoScalingGroupsRequest()
nextToken.foreach(request.setNextToken)
val result = client.describeAutoScalingGroups(request)
val autoScalingGroups = result.getAutoScalingGroups.asScala.toList
Option(result.getNextToken) match {
case None => autoScalingGroups
case token: Some[String] => autoScalingGroups ++ listAutoScalingGroups(token)
}
}
val groups = listAutoScalingGroups()
val filteredByStage = groups filter { _.hasTag("Stage", stage.name) }
val appToMatchingGroups = pkg.apps.flatMap { app =>
val matches = filteredByStage.filter(_.matchApp(app, stack))
if (matches.isEmpty) None else Some(ASGMatch(app, matches))
}
val appMatch:ASGMatch = appToMatchingGroups match {
case Seq() =>
reporter.fail(s"No autoscaling group found in ${stage.name} with tags matching package ${pkg.name}")
case Seq(onlyMatch) => onlyMatch
case firstMatch +: otherMatches =>
reporter.info(s"More than one app matches an autoscaling group, the first in the list will be used")
firstMatch
}
appMatch match {
case ASGMatch(_, List(singleGroup)) =>
reporter.verbose(s"Using group ${singleGroup.getAutoScalingGroupName} (${singleGroup.getAutoScalingGroupARN})")
singleGroup
case ASGMatch(app, groupList) =>
reporter.fail(s"More than one autoscaling group match for $app in ${stage.name} (${groupList.map(_.getAutoScalingGroupARN).mkString(", ")}). Failing fast since this may be non-deterministic.")
}
}
}
object ELB extends AWS {
case class Client(classic: ClassicELB, application: ApplicationELB)
def client(keyRing: KeyRing, region: Region): Client =
Client(classicClient(keyRing, region), applicationClient(keyRing, region))
def classicClient(keyRing: KeyRing, region: Region): ClassicELB =
ClassicELBBuilder.standard()
.withCredentials(provider(keyRing))
.withClientConfiguration(clientConfiguration)
.withRegion(region.name)
.build()
def applicationClient(keyRing: KeyRing, region: Region): ApplicationELB =
ApplicationELBBuilder.standard()
.withCredentials(provider(keyRing))
.withClientConfiguration(clientConfiguration)
.withRegion(region.name)
.build()
def targetInstancesHealth(targetARN: String, client: ApplicationELB): List[String] =
client.describeTargetHealth(new DescribeTargetHealthRequest().withTargetGroupArn(targetARN))
.getTargetHealthDescriptions.asScala.toList.map(_.getTargetHealth.getState)
def instanceHealth(elbName: String, client: ClassicELB): List[String] =
client.describeInstanceHealth(new DescribeInstanceHealthRequest(elbName))
.getInstanceStates.asScala.toList.map(_.getState)
def deregister(elbName: Option[String], elbTargetARN: Option[String], instance: ASGInstance, client: Client) = {
elbName.foreach(name =>
client.classic.deregisterInstancesFromLoadBalancer(
new DeregisterInstancesFromLoadBalancerRequest().withLoadBalancerName(name)
.withInstances(new ELBInstance().withInstanceId(instance.getInstanceId))))
elbTargetARN.foreach(arn =>
client.application.deregisterTargets(
new DeregisterTargetsRequest().withTargetGroupArn(arn)
.withTargets(new TargetDescription().withId(instance.getInstanceId))))
}
}
object EC2 extends AWS {
def makeEc2Client(keyRing: KeyRing, region: Region): AmazonEC2 = {
AmazonEC2ClientBuilder.standard()
.withCredentials(provider(keyRing))
.withClientConfiguration(clientConfiguration)
.withRegion(region.name)
.build()
}
def setTag(instances: List[ASGInstance], key: String, value: String, client: AmazonEC2) {
val request = new CreateTagsRequest().
withResources(instances map { _.getInstanceId } asJavaCollection).
withTags(new EC2Tag(key, value))
client.createTags(request)
}
def hasTag(instance: ASGInstance, key: String, value: String, client: AmazonEC2): Boolean = {
describe(instance, client).getTags.asScala exists { tag =>
tag.getKey == key && tag.getValue == value
}
}
def describe(instance: ASGInstance, client: AmazonEC2) = client.describeInstances(
new DescribeInstancesRequest().withInstanceIds(instance.getInstanceId)
).getReservations.asScala.flatMap(_.getInstances.asScala).head
def apply(instance: ASGInstance, client: AmazonEC2) = describe(instance, client)
}
object CloudFormation extends AWS {
sealed trait ParameterValue
case class SpecifiedValue(value: String) extends ParameterValue
case object UseExistingValue extends ParameterValue
sealed trait Template
case class TemplateBody(body: String) extends Template
case class TemplateUrl(url: String) extends Template
val CAPABILITY_IAM = "CAPABILITY_IAM"
def makeCfnClient(keyRing: KeyRing, region: Region): AmazonCloudFormation = {
AmazonCloudFormationClientBuilder.standard()
.withCredentials(provider(keyRing))
.withClientConfiguration(clientConfiguration)
.withRegion(region.name)
.build()
}
def validateTemplate(template: Template, client: AmazonCloudFormation) = {
val request = template match {
case TemplateBody(body) => new ValidateTemplateRequest().withTemplateBody(body)
case TemplateUrl(url) => new ValidateTemplateRequest().withTemplateURL(url)
}
client.validateTemplate(request)
}
def updateStack(name: String, template: Template, parameters: Map[String, ParameterValue],
client: AmazonCloudFormation) = {
val request = new UpdateStackRequest().withStackName(name).withCapabilities(CAPABILITY_IAM).withParameters(
parameters map {
case (k, SpecifiedValue(v)) => new Parameter().withParameterKey(k).withParameterValue(v)
case (k, UseExistingValue) => new Parameter().withParameterKey(k).withUsePreviousValue(true)
} toSeq: _*
)
val requestWithTemplate = template match {
case TemplateBody(body) => request.withTemplateBody(body)
case TemplateUrl(url) => request.withTemplateURL(url)
}
client.updateStack(requestWithTemplate)
}
def updateStackParams(name: String, parameters: Map[String, ParameterValue], client: AmazonCloudFormation) =
client.updateStack(
new UpdateStackRequest()
.withStackName(name)
.withCapabilities(CAPABILITY_IAM)
.withUsePreviousTemplate(true)
.withParameters(
parameters map {
case (k, SpecifiedValue(v)) => new Parameter().withParameterKey(k).withParameterValue(v)
case (k, UseExistingValue) => new Parameter().withParameterKey(k).withUsePreviousValue(true)
} toSeq: _*
)
)
def createStack(reporter: DeployReporter, name: String, maybeTags: Option[Map[String, String]], template: Template, parameters: Map[String, ParameterValue],
client: AmazonCloudFormation) = {
val request = new CreateStackRequest()
.withStackName(name)
.withCapabilities(CAPABILITY_IAM)
.withParameters(
parameters map {
case (k, SpecifiedValue(v)) => new Parameter().withParameterKey(k).withParameterValue(v)
case (k, UseExistingValue) => reporter.fail(s"Missing parameter value for parameter $k: all must be specified when creating a stack. Subsequent updates will reuse existing parameter values where possible.")
} toSeq: _*
)
val requestWithTemplate = template match {
case TemplateBody(body) => request.withTemplateBody(body)
case TemplateUrl(url) => request.withTemplateURL(url)
}
maybeTags.foreach { tags =>
val sdkTags = tags.map{ case (key, value) => new CfnTag().withKey(key).withValue(value) }
request.setTags(sdkTags.asJavaCollection)
}
client.createStack(requestWithTemplate)
}
def describeStack(name: String, client: AmazonCloudFormation) =
client.describeStacks(
new DescribeStacksRequest()
).getStacks.asScala.find(_.getStackName == name)
def describeStackEvents(name: String, client: AmazonCloudFormation) =
client.describeStackEvents(
new DescribeStackEventsRequest().withStackName(name)
)
def findStackByTags(tags: Map[String, String], reporter: DeployReporter, client: AmazonCloudFormation): Option[AmazonStack] = {
def tagsMatch(stack: AmazonStack): Boolean =
tags.forall { case (key, value) => stack.getTags.asScala.exists(t => t.getKey == key && t.getValue == value) }
@tailrec
def recur(nextToken: Option[String] = None, existingStacks: List[AmazonStack] = Nil): List[AmazonStack] = {
val request = new DescribeStacksRequest().withNextToken(nextToken.orNull)
val response = client.describeStacks(request)
val stacks = response.getStacks.asScala.foldLeft(existingStacks) {
case (agg, stack) if tagsMatch(stack) => stack :: agg
case (agg, _) => agg
}
Option(response.getNextToken) match {
case None => stacks
case token => recur(token, stacks)
}
}
recur() match {
case cfnStack :: Nil =>
reporter.verbose(s"Found stack ${cfnStack.getStackName} (${cfnStack.getStackId})")
Some(cfnStack)
case Nil =>
None
case multipleStacks =>
reporter.fail(s"More than one cloudformation stack match for $tags (matched ${multipleStacks.map(_.getStackName).mkString(", ")}). Failing fast since this may be non-deterministic.")
}
}
}
object STS extends AWS {
def makeSTSclient(keyRing: KeyRing, region: Region): AWSSecurityTokenService = {
AWSSecurityTokenServiceClientBuilder.standard()
.withCredentials(provider(keyRing))
.withClientConfiguration(clientConfiguration)
.withRegion(region.name)
.build()
}
}
trait AWS extends Loggable {
lazy val accessKey = Option(System.getenv.get("aws_access_key")).getOrElse{
sys.error("Cannot authenticate, 'aws_access_key' must be set as a system property")
}
lazy val secretAccessKey = Option(System.getenv.get("aws_secret_access_key")).getOrElse{
sys.error("Cannot authenticate, aws_secret_access_key' must be set as a system property")
}
lazy val envCredentials = new BasicAWSCredentials(accessKey, secretAccessKey)
def provider(keyRing: KeyRing): AWSCredentialsProvider = new AWSCredentialsProviderChain(
new AWSCredentialsProvider {
def refresh() {}
def getCredentials = keyRing.apiCredentials.get("aws") map {
credentials => new BasicAWSCredentials(credentials.id,credentials.secret)
} get
},
new AWSCredentialsProvider {
def refresh() {}
def getCredentials = envCredentials
}
)
/* A retry condition that logs errors */
class LoggingRetryCondition extends SDKDefaultRetryCondition {
private def exceptionInfo(e: Throwable): String = {
s"${e.getClass.getName} ${e.getMessage} Cause: ${Option(e.getCause).map(e => exceptionInfo(e))}"
}
override def shouldRetry(originalRequest: AmazonWebServiceRequest, exception: AmazonClientException, retriesAttempted: Int): Boolean = {
val willRetry = super.shouldRetry(originalRequest, exception, retriesAttempted)
if (willRetry) {
logger.warn(s"AWS SDK retry $retriesAttempted: ${Option(originalRequest).map(_.getClass.getName)} threw ${exceptionInfo(exception)}")
} else {
logger.warn(s"Encountered fatal exception during AWS API call", exception)
Option(exception.getCause).foreach(t => logger.warn(s"Cause of fatal exception", t))
}
willRetry
}
}
val clientConfiguration = new ClientConfiguration().
withRetryPolicy(new RetryPolicy(
new LoggingRetryCondition(),
PredefinedRetryPolicies.DEFAULT_BACKOFF_STRATEGY,
20,
false
))
val clientConfigurationNoRetry = new ClientConfiguration().withRetryPolicy(PredefinedRetryPolicies.NO_RETRY_POLICY)
}