class AddyLambda::AWS

Common functionality for AWS related activities

Public Class Methods

copy_file_from_bucket_to_bucket(s3_client, src_bucket, src_key, dest_bucket, dest_key) click to toggle source

s3_client = Aws::S3::Client.new

# File lib/addy_lambda/aws.rb, line 56
def self.copy_file_from_bucket_to_bucket(s3_client, src_bucket, src_key, dest_bucket, dest_key)
  s3_client.copy_object(bucket: dest_bucket, copy_source: "/#{src_bucket}/#{src_key}", key: dest_key)
end
copy_file_from_s3(s3_client, bucket, key, destination) click to toggle source

s3_client = Aws::S3::Client.new

# File lib/addy_lambda/aws.rb, line 72
def self.copy_file_from_s3(s3_client, bucket, key, destination)
  s3_client.get_object({ bucket: bucket, key: key }, target: destination)
end
generate_jwt(lambda_client, issuer, audience, expire, payload) click to toggle source

NOTE: issuer AddyLambda::JwtIssuers::…, Audience AddyLambda::JwtAudiences::…

# File lib/addy_lambda/aws.rb, line 101
def self.generate_jwt(lambda_client, issuer, audience, expire, payload)
  lambda_payload = {
    'issuer' => JWT_ISSUERS[issuer],
    'audience' => JWT_AUDIENCES[audience],
    'expire' => expire,
    'payload' => payload
  }
  jwt_response = AddyLambda::AWS.invoke_lambda(lambda_client, 'jwt-generate', lambda_payload)
  jwt_response.key?('jwt') ? jwt_response['jwt'] : jwt_response
end
get_ssm_value(ssm_client, path) click to toggle source

ssm_client = Aws::SSM::Client.new

# File lib/addy_lambda/aws.rb, line 21
def self.get_ssm_value(ssm_client, path)
  request = {
    name: path,
    with_decryption: true
  }
  response = ssm_client.get_parameter(request)
  response.parameter.value
end
invoke_lambda(lambda_client, function_name, payload, syncronous: true) click to toggle source

lambda_client = Aws::Lambda::Client.new

# File lib/addy_lambda/aws.rb, line 31
def self.invoke_lambda(lambda_client, function_name, payload, syncronous: true)
  resp = lambda_client.invoke({
                                function_name: function_name,
                                invocation_type: syncronous ? 'RequestResponse' : 'Event',
                                payload: payload.to_json
                              })
  AddyLambda::Common.valid_json?(resp.payload.string) ? JSON.parse(resp.payload.string) : resp.payload.string
end
list_s3_files(s3_client, bucket, prefix, suffix) click to toggle source

s3_client = Aws::S3::Client.new

# File lib/addy_lambda/aws.rb, line 61
def self.list_s3_files(s3_client, bucket, prefix, suffix)
  files = []
  objects = s3_client.list_objects_v2({ bucket: bucket, prefix: prefix }).contents
  objects.each do |o|
    key = o[:key]
    files << key if key.start_with?(prefix) && key.end_with?(suffix)
  end
  files
end
post_slack_message(lambda_client, post_as_name, channel, message, emoji = ':tada', unfurl_links: false) click to toggle source

lambda_client = Aws::Lambda::Client.new

# File lib/addy_lambda/aws.rb, line 41
def self.post_slack_message(lambda_client, post_as_name, channel, message, emoji = ':tada', unfurl_links: false)
  input_params = {
    'user' => post_as_name,
    'channel' => channel,
    'message' => message,
    'emoji' => emoji,
    'unfurl_links' => unfurl_links
  }
  response_body = AddyLambda::AWS.invoke_lambda(lambda_client, 'slack-client', input_params)
  return response_body unless response_body.key?('error')

  logger.error(response_body['error'])
end
save_file_to_s3(s3_client, file_location, bucket, key) click to toggle source

s3_client = Aws::S3::Client.new

# File lib/addy_lambda/aws.rb, line 77
def self.save_file_to_s3(s3_client, file_location, bucket, key)
  # Upload
  File.open(file_location, 'rb') do |file|
    s3_client.put_object(bucket: bucket, key: key, body: file)
  end
end
send_email(lambda_client, recipient_emails, from_name, from_email, subject, content_html) click to toggle source

lambda_client = Aws::Lambda::Client.new

# File lib/addy_lambda/aws.rb, line 85
def self.send_email(lambda_client, recipient_emails, from_name, from_email, subject, content_html)
  input_params = {
    'recipient_list' => recipient_emails,
    'from_name' => from_name,
    'from_email' => from_email,
    'subject' => subject,
    'content_html' => content_html
  }

  response_body = AddyLambda::AWS.invoke_lambda(lambda_client, 'mailgun-client', input_params)
  return response_body unless response_body.key?('error')

  logger.error(response_body['error'])
end
validate_jwt(lambda_client, jwt) click to toggle source
# File lib/addy_lambda/aws.rb, line 112
def self.validate_jwt(lambda_client, jwt)
  lambda_payload = {
    'jwt' => jwt
  }
  response = AddyLambda::AWS.invoke_lambda(lambda_client, 'jwt-validate', lambda_payload)
  response['statusCode'] == 200 ? JSON.parse(response['body']) : response
end