Module: Jets::AwsServices

Includes:
GlobalMemoist, StackStatus
Included in:
AwsHelpers, AwsInfo, S3Bucket, CLI::Base, CLI::Call, CLI::Ci::Info, CLI::Dotenv::Ssm, CLI::Lambda::Function, CLI::Lambda::Lookup, CLI::Waf::Info, Cfn::Builder::Parent::Genesis, Cfn::Resource::S3::JetsBucket, Cfn::Stack, Cfn::Stack::Template, Cfn::Teardown::Bucket, Code, Dotenv, Dotenv::Ssm, Dotenv::Var, Remote::Base, Stack::Outputs
Defined in:
lib/jets/aws_services/global_memoist.rb,
lib/jets/aws_services.rb,
lib/jets/aws_services/aws_info.rb,
lib/jets/aws_services/s3_bucket.rb,
lib/jets/aws_services/aws_helpers.rb,
lib/jets/aws_services/stack_status.rb

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: AwsHelpers, GlobalMemoist, StackStatus Classes: AwsInfo, S3Bucket

Instance Method Summary collapse

Methods included from StackStatus

#output_value, #stack_exists?

Methods included from GlobalMemoist

included, #reset_cache!

Instance Method Details

#apigatewayObject



20
21
22
# File 'lib/jets/aws_services.rb', line 20

def apigateway
  Aws::APIGateway::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


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/jets/aws_services.rb', line 102

def aws_options
  options = {
    retry_limit: 7, # default: 3
    retry_base_delay: 0.6, # default: 0.3
    log_level: :info
  }
  # 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")
  if ENV["JETS_DEBUG_AWS_SDK"]
    options[:log_level] = :debug
    options[:logger] = Logger.new($stdout)
  end
  # 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.
  if ENV["JETS_DEBUG_AWS_SDK_HTTP_WIRE_TRACE"]
    options[:http_wire_trace] = true
  end
  options
end

#cfnObject



31
32
33
# File 'lib/jets/aws_services.rb', line 31

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

#codebuildObject



36
37
38
# File 'lib/jets/aws_services.rb', line 36

def codebuild
  Aws::CodeBuild::Client.new(aws_options)
end

#dynamodbObject



41
42
43
# File 'lib/jets/aws_services.rb', line 41

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

#lambda_clientObject Also known as: aws_lambda



25
26
27
# File 'lib/jets/aws_services.rb', line 25

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

#logsObject



46
47
48
# File 'lib/jets/aws_services.rb', line 46

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

#s3Object



51
52
53
# File 'lib/jets/aws_services.rb', line 51

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

#s3_resourceObject



56
57
58
# File 'lib/jets/aws_services.rb', line 56

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

#snsObject



61
62
63
# File 'lib/jets/aws_services.rb', line 61

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

#sqsObject



76
77
78
# File 'lib/jets/aws_services.rb', line 76

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

#ssmObject



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

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

#stsObject



71
72
73
# File 'lib/jets/aws_services.rb', line 71

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

#wafv2Object



81
82
83
# File 'lib/jets/aws_services.rb', line 81

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