Module: Jets::AwsServices

Overview

We cache the clients globally to avoid re-instantiating them again after the initial Lambda cold start.

Based on: hashrocket.com/blog/posts/implementing-a-macro-in-ruby-for-memoization Except we use a global variable for the cache. So we’ll use the same client across all instances as well as across Lambda executions after the cold-start. Example:

class Foo
  def s3
    Aws::S3::Client.new
  end
  global_memoize :s3
end

foo1 = Foo.new
foo2 = Foo.new
foo1.s3
foo2.s3 # same client as foo1

A prewarmed request after a cold-start will still use the same s3 client instance since it uses the global variable $__memo_methods as the cache.

Defined Under Namespace

Modules: GlobalMemoist, StackStatus Classes: S3Bucket

Instance Method Summary collapse

Methods included from StackStatus

#lookup, #stack_exists?, #stack_in_progress?

Methods included from GlobalMemoist

included

Instance Method Details

#apigatewayObject



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

def apigateway
  Aws::APIGateway::Client.new(aws_options)
end

#aws_lambdaObject



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

def aws_lambda
  Aws::Lambda::Client.new(aws_options)
end

#aws_optionsObject

Override the AWS retry settings with Jets AWS clients.

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

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

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


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/jets/aws_services.rb', line 84

def aws_options
  options = {
    retry_limit: 7, # default: 3
    retry_base_delay: 0.6, # default: 0.3
  }
  # See debug logger. Noisy.
  # Example:
  #     D, [2022-12-02T13:18:55.298788 #26182] DEBUG -- : [Aws::APIGateway::Client 200 0.030837 0 retries] get_method(rest_api_id:"mke40eh6l0",resource_id:"zf8w2m",http_method:"GET")
  options.merge!(
    log_level: :debug,
    logger: Logger.new($stdout),
  ) if ENV['JETS_DEBUG_AWS_SDK']
  # https://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/debugging.html to enable http_wire_trace
  # See the HTTP headers and JSON responses. Super noisy.
  options.merge!(
    http_wire_trace: true,
  ) if ENV['JETS_DEBUG_AWS_SDK_HTTP_WIRE_TRACE']
  options
end

#cfnObject



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

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

#dynamodbObject



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

def dynamodb
  Aws::DynamoDB::Client.new(aws_options)
end

#logsObject



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

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

#s3Object



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

def s3
  Aws::S3::Client.new(aws_options)
end

#s3_resourceObject



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

def s3_resource
  Aws::S3::Resource.new(aws_options)
end

#snsObject



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

def sns
  Aws::SNS::Client.new(aws_options)
end

#sqsObject



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

def sqs
  Aws::SQS::Client.new(aws_options)
end

#stsObject



63
64
65
# File 'lib/jets/aws_services.rb', line 63

def sts
  Aws::STS::Client.new(aws_options)
end