Module: HasHelpers::Aws

Defined in:
lib/has_helpers/aws.rb

Class Method Summary collapse

Class Method Details

._get_existing_env!(env_var_name, default_value = nil) ⇒ Object

Gets the environment variable whose name is passed in, but aborts the app with an error message if the value is nil (i.e. the variable wasn't set). Intended for "internal" use to set up the various OpenStructs defined here.



8
9
10
11
12
13
14
15
16
17
# File 'lib/has_helpers/aws.rb', line 8

def self._get_existing_env!(env_var_name, default_value = nil)
  value = ENV[env_var_name] || default_value
  unless value.present? || ::HasUtils::Env.test?
    # rubocop:disable Rails/Exit
    abort "Environment variable #{env_var_name} must be defined. " \
            "See README for details on required environment variables."
    # rubocop:enable Rails/Exit
  end
  value
end

.credentials_confObject

The fallback credentials are for the dev_api_access AWS user, who should only have access to dev and test AWS resources.



23
24
25
26
27
28
# File 'lib/has_helpers/aws.rb', line 23

def self.credentials_conf
  @credentials_conf ||= OpenStruct.new(
    access_key_id: _get_existing_env!("AWS_ACCESS_KEY_ID"),
    secret_access_key: _get_existing_env!("AWS_SECRET_ACCESS_KEY")
  )
end

.new_aws_client(client_class, region:, credentials_conf:) ⇒ Object

Returns a new AWS client of the given client_class, getting credentials from the credentials_conf passed in.



59
60
61
62
63
64
65
# File 'lib/has_helpers/aws.rb', line 59

def self.new_aws_client(client_class, region:, credentials_conf:)
  client_class.new(
    region: region,
    access_key_id: credentials_conf.access_key_id,
    secret_access_key: credentials_conf.secret_access_key
  )
end

.new_s3_client(region: s3_conf.region, credentials_conf: self.credentials_conf) ⇒ Object

Returns a new AWS S3 client for v1 or v2, and handles credentials in an environment-appropriate manner. (See #new_aws_client for details.)



47
48
49
# File 'lib/has_helpers/aws.rb', line 47

def self.new_s3_client(region: s3_conf.region, credentials_conf: self.credentials_conf)
  new_aws_client(::Aws::S3::Client, region: region, credentials_conf: credentials_conf)
end

.new_sqs_client(region: sqs_conf.region, credentials_conf: self.credentials_conf) ⇒ Object

Returns a new AWS SQS client for v1 or v2, and handles credentials in an environment-appropriate manner. (See #new_aws_client for details.)



53
54
55
# File 'lib/has_helpers/aws.rb', line 53

def self.new_sqs_client(region: sqs_conf.region, credentials_conf: self.credentials_conf)
  new_aws_client(::Aws::SQS::Client, region: region, credentials_conf: credentials_conf)
end

.s3_confObject



30
31
32
33
34
35
# File 'lib/has_helpers/aws.rb', line 30

def self.s3_conf
  @s3_conf ||= OpenStruct.new(
    bucket: _get_existing_env!("AWS_S3_BUCKET", "#{::HasHelpers::Application.project}-#{::HasUtils::Env.current_environment}"),
    region: _get_existing_env!("AWS_S3_REGION", "us-east-1")
  )
end

.sqs_confObject



37
38
39
40
41
# File 'lib/has_helpers/aws.rb', line 37

def self.sqs_conf
  @sqs_conf ||= OpenStruct.new(
    region: _get_existing_env!("AWS_SQS_REGION", "us-east-1")
  )
end