Class: Belt::CLI::LambdaConfigCommand

Inherits:
Object
  • Object
show all
Includes:
AppDetection
Defined in:
lib/belt/cli/lambda_config_command.rb

Overview

Reads config/lambda/*.yml files and produces a merged lambda_config hash suitable for Conveyor Belt's lambda_config variable.

Each YAML file follows a database.yml-like pattern:

# config/lambda/customer.yml
default: &default
timeout: 60
memory_size: 512
env_keys:
  - IMAGES_BUCKET_NAME
  - IMAGES_CLOUDFRONT_DOMAIN

dev:
<<: *default
memory_size: 256

prod:
<<: *default
timeout: 30
memory_size: 1024

Constant Summary collapse

SUPPORTED_KEYS =
%w[
  timeout memory_size env_vars env_keys
  s3_buckets dynamodb_tables sns_triggers sqs_triggers
  reserved_concurrency ephemeral_storage
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AppDetection

#detect_app_name, #detect_environments, #detect_namespace, #find_routes_file_path, #find_schema_file_path, #s3_safe_name

Constructor Details

#initialize(options = {}) ⇒ LambdaConfigCommand

Returns a new instance of LambdaConfigCommand.



118
119
120
121
122
123
# File 'lib/belt/cli/lambda_config_command.rb', line 118

def initialize(options = {})
  @environment = options[:environment] || ENV.fetch('BELT_ENV', 'dev')
  @format = options[:format] || 'json'
  @lambda_filter = options[:lambda]
  @config_dir = 'config/lambda'
end

Class Method Details

.help_textObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/belt/cli/lambda_config_command.rb', line 65

def self.help_text
  <<~HELP
    Read lambda config from config/lambda/*.yml files.

    Usage: belt lambda-config [options]

    Reads YAML config files and produces a merged configuration hash for the
    specified environment (like database.yml in Rails).

    Options:
      -e, --environment ENV    Target environment (default: dev)
      -f, --format FORMAT      Output format: json (default), terraform
      --lambda NAME            Only output config for a specific lambda
      -h, --help               Show this help

    Examples:
      belt lambda-config -e prod
      belt lambda-config -e dev --format terraform
      belt lambda-config --lambda customer -e prod

    Config files live at config/lambda/<name>.yml. Each file can define
    environment-specific overrides using YAML anchors:

      # config/lambda/customer.yml
      default: &default
        timeout: 60
        memory_size: 512
        env_keys:
          - IMAGES_BUCKET_NAME
          - STRIPE_KEY

      dev:
        <<: *default
        memory_size: 256

      prod:
        <<: *default
        memory_size: 1024

    Keys:
      timeout              Lambda timeout in seconds (default: 30)
      memory_size          Lambda memory in MB (default: 256)
      env_keys             List of env var names this lambda needs
      env_vars             Static env vars (key: value pairs)
      s3_buckets           S3 bucket access declarations
      dynamodb_tables      DynamoDB table access declarations
      sns_triggers         SNS topic triggers
      sqs_triggers         SQS queue triggers
      reserved_concurrency Reserved concurrency limit
      ephemeral_storage    Ephemeral storage in MB (512-10240)
  HELP
end

.load_configs(environment: 'dev', config_dir: 'config/lambda') ⇒ Object

Public API: load and merge all configs for a given environment. Used by other belt commands and potentially by the Conveyor Belt provider.



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/belt/cli/lambda_config_command.rb', line 144

def self.load_configs(environment: 'dev', config_dir: 'config/lambda')
  return {} unless Dir.exist?(config_dir)

  configs = {}
  Dir.glob(File.join(config_dir, '*.yml')).each do |file|
    name = File.basename(file, '.yml')
    raw = YAML.safe_load_file(file, aliases: true) || {}
    configs[name] = resolve_environment(raw, environment)
  end
  configs
end

.run(args) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/belt/cli/lambda_config_command.rb', line 40

def self.run(args)
  options = {}

  i = 0
  while i < args.length
    case args[i]
    when '-e', '--environment'
      i += 1
      options[:environment] = args[i]
    when '-f', '--format'
      i += 1
      options[:format] = args[i]
    when '--lambda'
      i += 1
      options[:lambda] = args[i]
    when '-h', '--help'
      puts help_text
      exit 0
    end
    i += 1
  end

  new(options).run
end

Instance Method Details

#runObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/belt/cli/lambda_config_command.rb', line 125

def run
  abort "Error: #{@config_dir}/ not found. Create lambda config files there." unless Dir.exist?(@config_dir)

  configs = load_all_configs
  configs = configs.select { |name, _| name == @lambda_filter } if @lambda_filter

  if configs.empty?
    if @lambda_filter
      abort "Error: No config found for lambda '#{@lambda_filter}' in #{@config_dir}/"
    else
      abort "Error: No .yml files found in #{@config_dir}/"
    end
  end

  output(configs)
end