Class: ElasticBeans::EnvVars

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_beans/env_vars.rb

Overview

Interfaces with environment variable storage for the Elastic Beanstalk application. Environment variables are stored in JSON in S3 to avoid the 4096-byte limit on environment variables in Elastic Beanstalk. An ebextension is used to inject these into the environment variables on the running instance.

Instance Method Summary collapse

Constructor Details

#initialize(application:, s3:) ⇒ EnvVars

Returns a new instance of EnvVars.



11
12
13
14
# File 'lib/elastic_beans/env_vars.rb', line 11

def initialize(application:, s3:)
  @application = application
  @s3 = s3
end

Instance Method Details

#del(env_keys) ⇒ Object

Deletes the specified env_keys environment variables stored in S3.



38
39
40
41
42
43
44
45
46
# File 'lib/elastic_beans/env_vars.rb', line 38

def del(env_keys)
  json_hash = to_h
  env_keys.each { |key|
    json_hash.delete(key)
  }

  body = env_script(json_hash)
  s3.put_object(bucket: application.bucket_name, key: s3_key, body: body)
end

#s3_keyObject



16
17
18
# File 'lib/elastic_beans/env_vars.rb', line 16

def s3_key
  @s3_key ||= "#{application.name}/env_vars.json"
end

#to_hObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/elastic_beans/env_vars.rb', line 20

def to_h
  s3.head_object(bucket: application.bucket_name, key: s3_key)
  response = s3.get_object(bucket: application.bucket_name, key: s3_key)
  existing_script = response.body.read
  JSON.parse(existing_script)
rescue ::Aws::S3::Errors::NotFound
  {}
rescue ::Aws::S3::Errors::Forbidden
  raise AccessDeniedS3Error.new(bucket: application.bucket_name, key: s3_key)
end

#upsert(env_hash) ⇒ Object

Updates the environment variables stored in S3 by merging it with the given env_hash.



32
33
34
35
# File 'lib/elastic_beans/env_vars.rb', line 32

def upsert(env_hash)
  body = env_script(to_h.merge(env_hash))
  s3.put_object(bucket: application.bucket_name, key: s3_key, body: body)
end