Module: Ufo::AwsServices

Instance Method Summary collapse

Instance Method Details

#acmObject



18
19
20
# File 'lib/ufo/aws_services.rb', line 18

def acm
  Aws::ACM::Client.new(aws_options)
end

#applicationautoscalingObject



23
24
25
# File 'lib/ufo/aws_services.rb', line 23

def applicationautoscaling
  Aws::ApplicationAutoScaling::Client.new(aws_options)
end

#aws_optionsObject

Override the AWS retry settings with AWS clients.

The aws-sdk-core has exponential backup with this formula:

2 ** c.retries * c.config.retry_base_delay

Source:

https://github.com/aws/aws-sdk-ruby/blob/version-3/gems/aws-sdk-core/lib/aws-sdk-core/plugins/retry_errors.rb

So the max delay will be 2 ** 7 * 0.6 = 76.8s

Only scoping this to deploy because dont want to affect people’s application that use the aws sdk.

There is also additional rate backoff logic elsewhere, since this is only scoped to deploys.

Useful links:

https://github.com/aws/aws-sdk-ruby/blob/master/gems/aws-sdk-core/lib/aws-sdk-core/plugins/retry_errors.rb
https://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html


82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ufo/aws_services.rb', line 82

def aws_options
  options = {
    retry_limit: 7, # default: 3
    retry_base_delay: 0.6, # default: 0.3
  }
  options.merge!(
    log_level: :debug,
    logger: Logger.new($stdout),
  ) if ENV['UFO_DEBUG_AWS_SDK']
  options
end

#cloudformationObject



28
29
30
# File 'lib/ufo/aws_services.rb', line 28

def cloudformation
  Aws::CloudFormation::Client.new(aws_options)
end

#cloudwatchlogsObject



33
34
35
# File 'lib/ufo/aws_services.rb', line 33

def cloudwatchlogs
  Aws::CloudWatchLogs::Client.new(aws_options)
end

#ec2Object



38
39
40
# File 'lib/ufo/aws_services.rb', line 38

def ec2
  Aws::EC2::Client.new(aws_options)
end

#ecrObject



43
44
45
# File 'lib/ufo/aws_services.rb', line 43

def ecr
  Aws::ECR::Client.new(aws_options)
end

#ecsObject



48
49
50
# File 'lib/ufo/aws_services.rb', line 48

def ecs
  Aws::ECS::Client.new(aws_options)
end

#elbObject



53
54
55
# File 'lib/ufo/aws_services.rb', line 53

def elb
  Aws::ElasticLoadBalancingV2::Client.new(aws_options)
end

#find_stack(stack_name) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ufo/aws_services.rb', line 94

def find_stack(stack_name)
  resp = cloudformation.describe_stacks(stack_name: stack_name)
  resp.stacks.first
rescue Aws::CloudFormation::Errors::ValidationError => e
  # example: Stack with id demo-web does not exist
  if e.message =~ /Stack with/ && e.message =~ /does not exist/
    nil
  else
    raise
  end
end

#ssm_clientObject



58
59
60
# File 'lib/ufo/aws_services.rb', line 58

def ssm_client
  Aws::SSM::Client.new
end

#stack_resources(stack_name) ⇒ Object



106
107
108
109
110
111
# File 'lib/ufo/aws_services.rb', line 106

def stack_resources(stack_name)
  resp = cloudformation.describe_stack_resources(stack_name: stack_name)
  resp.stack_resources
rescue Aws::CloudFormation::Errors::ValidationError => e
  e.message.include?("does not exist") ? return : raise
end

#statusObject



126
127
128
# File 'lib/ufo/aws_services.rb', line 126

def status
  CfnStatus.new(@stack_name) # NOTE: @stack_name must be set in the including Class
end

#task_definition_arns(family, max_items = 10) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ufo/aws_services.rb', line 113

def task_definition_arns(family, max_items=10)
  resp = ecs.list_task_definitions(
    family_prefix: family,
    sort: "DESC",
  )
  arns = resp.task_definition_arns
  arns = arns.select do |arn|
    task_definition = arn.split('/').last.split(':').first
    task_definition == family
  end
  arns[0..max_items]
end