Module: Ufo::AwsServices

Instance Method Summary collapse

Instance Method Details

#acmObject



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

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

#applicationautoscalingObject



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

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


90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ufo/aws_services.rb', line 90

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



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

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

#cloudwatchlogsObject



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

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

#ec2Object



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

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

#ecrObject



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

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

#ecsObject



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

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

#elbObject



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

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

#find_stack(stack_name) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ufo/aws_services.rb', line 102

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

ssm is a helper method



60
61
62
# File 'lib/ufo/aws_services.rb', line 60

def ssm_client
  Aws::SSM::Client.new(aws_options)
end

#stack_resources(stack_name) ⇒ Object



114
115
116
117
118
119
# File 'lib/ufo/aws_services.rb', line 114

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



134
135
136
# File 'lib/ufo/aws_services.rb', line 134

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



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/ufo/aws_services.rb', line 121

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

#waf_clientObject

waf is a helper method



66
67
68
# File 'lib/ufo/aws_services.rb', line 66

def waf_client
  Aws::WAFV2::Client.new(aws_options)
end