ruby-cdk 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +253 -0
  3. data/README.md +28 -5
  4. data/lib/ruby_cdk/assets/hasher.rb +4 -0
  5. data/lib/ruby_cdk/assets.rb +4 -8
  6. data/lib/ruby_cdk/cli/aws.rb +23 -5
  7. data/lib/ruby_cdk/cli/drift.rb +94 -0
  8. data/lib/ruby_cdk/cli/import.rb +306 -0
  9. data/lib/ruby_cdk/cli/lookup.rb +214 -0
  10. data/lib/ruby_cdk/cli/session.rb +114 -3
  11. data/lib/ruby_cdk/cli/watch.rb +297 -0
  12. data/lib/ruby_cdk/cli.rb +76 -1
  13. data/lib/ruby_cdk/constructs/function.rb +24 -3
  14. data/lib/ruby_cdk/constructs/hosted_zone.rb +342 -0
  15. data/lib/ruby_cdk/constructs/record_set.rb +330 -0
  16. data/lib/ruby_cdk/constructs/ssm_parameter.rb +174 -0
  17. data/lib/ruby_cdk/constructs/vpc.rb +822 -0
  18. data/lib/ruby_cdk/constructs/vpc_network.rb +159 -0
  19. data/lib/ruby_cdk/constructs.rb +5 -1
  20. data/lib/ruby_cdk/core/app.rb +28 -2
  21. data/lib/ruby_cdk/core/construct.rb +2 -0
  22. data/lib/ruby_cdk/core/context.rb +138 -0
  23. data/lib/ruby_cdk/core/stack.rb +4 -0
  24. data/lib/ruby_cdk/core.rb +1 -0
  25. data/lib/ruby_cdk/deploy/deployer.rb +283 -8
  26. data/lib/ruby_cdk/deploy/drift.rb +389 -0
  27. data/lib/ruby_cdk/deploy/ec2_adapter.rb +214 -0
  28. data/lib/ruby_cdk/deploy/errors.rb +8 -0
  29. data/lib/ruby_cdk/deploy/fakes.rb +230 -22
  30. data/lib/ruby_cdk/deploy/output/base.rb +8 -0
  31. data/lib/ruby_cdk/deploy/output/json.rb +7 -0
  32. data/lib/ruby_cdk/deploy/poller.rb +10 -0
  33. data/lib/ruby_cdk/deploy/ports.rb +124 -8
  34. data/lib/ruby_cdk/deploy/sdk_adapters.rb +141 -6
  35. data/lib/ruby_cdk/deploy/ssm_adapter.rb +111 -0
  36. data/lib/ruby_cdk/deploy.rb +1 -0
  37. data/lib/ruby_cdk/diff/compare.rb +1 -1
  38. data/lib/ruby_cdk/diff/identity_center.rb +64 -0
  39. data/lib/ruby_cdk/diff/render.rb +147 -8
  40. data/lib/ruby_cdk/diff/security.rb +195 -10
  41. data/lib/ruby_cdk/diff/security_group.rb +94 -0
  42. data/lib/ruby_cdk/nag/rules/security_group_ingress.rb +2 -0
  43. data/lib/ruby_cdk/schema/sources/provider_type.rb +3 -0
  44. data/lib/ruby_cdk/test/private/cyclic.rb +4 -0
  45. data/lib/ruby_cdk/test/private/js.rb +1 -3
  46. data/lib/ruby_cdk/version.rb +1 -1
  47. metadata +43 -1
@@ -0,0 +1,297 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "failure"
4
+ require_relative "selection"
5
+
6
+ module RubyCdk
7
+ module Cli
8
+ # `watch`: the CDK's development loop. One synthesis and deploy up front,
9
+ # then the watched files are polled, and every change to one of them
10
+ # synthesizes and deploys again, hotswapping by default, until the process
11
+ # is interrupted.
12
+ #
13
+ # The loop is the CDK's watch (packages/aws-cdk/lib/cli/cdk-toolkit.ts:828-903
14
+ # and packages/@aws-cdk/toolkit-lib/lib/toolkit/toolkit.ts:1283-1467 in
15
+ # aws-cdk-cli at f7c9dca95f440a076c2abd61b66b984624b8548e), read at that
16
+ # commit. Two properties of it decide everything here:
17
+ #
18
+ # - The CDK's watcher is chokidar, a native file watcher. This one polls:
19
+ # no process is started and no external watcher is started (architecture
20
+ # section 8). The files are polled through their mtimes -- a change, an
21
+ # appearance and a disappearance are all a difference between two
22
+ # snapshots -- every --interval seconds. The CDK takes no interval: its
23
+ # watcher is event driven, and this flag is this tool's own, for the
24
+ # polling that the law above asks for.
25
+ # - A failure of one deployment, and a synthesis that fails because the app
26
+ # file is broken mid-edit, are reported and watched through, the way the
27
+ # CDK's invokeSafe reports and watches through (toolkit.ts:1384-1396).
28
+ # Only the startup synthesis and the checks before it are fatal.
29
+ #
30
+ # What is watched is this tool's own derivation, because there is no
31
+ # cdk.json here to carry the CDK's include and exclude globs: the app file,
32
+ # and every asset source directory the manifest records, the synthesis out
33
+ # directory and hidden files left out -- the two exclusions the CDK always
34
+ # adds itself (toolkit.ts:1335, 1338).
35
+ class Watch
36
+ # The CDK's wording, read at the commit above: the first deployment
37
+ # (cdk-toolkit.ts:883) and the queued deployment for changes that arrived
38
+ # during one (856). The inner command is this tool's.
39
+ TRIGGER = "Triggering initial 'ruby-cdk deploy'"
40
+ QUEUE = "Detected file changes during deployment. Invoking 'ruby-cdk deploy' again"
41
+
42
+ # One poll per interval. sleep is called with the seconds; a test hands
43
+ # a callable that counts, and makes its scripted changes there, so that
44
+ # no test waits. stop_after is the number of polls after the first
45
+ # deployment before the loop ends -- the CDK's loop runs until the
46
+ # process is interrupted, and only a test ends this one.
47
+ def self.run(session, names, sleep: nil, stop_after: nil)
48
+ new(session, names, sleep:, stop_after:).run
49
+ end
50
+
51
+ def initialize(session, names, sleep:, stop_after:)
52
+ @session = session
53
+ @names = names
54
+ @sleep = sleep || ->(seconds) { Kernel.sleep(seconds) }
55
+ @stop_after = stop_after
56
+ @polled = 0
57
+ @roots = []
58
+ @snapshot = {}
59
+ end
60
+
61
+ # The CDK's startup order (toolkit.ts:1313, 1344-1351, cdk-toolkit.ts:883):
62
+ # the synthesis and the checks first -- a usage error never starts a
63
+ # watch -- then the lines about what is observed, then the first deploy.
64
+ def run
65
+ @deploy_options = session.watch_deploy_options
66
+ interval
67
+ manifest = session.manifest
68
+ session.say_synthesis_time
69
+ entries = order(manifest)
70
+ observe(manifest)
71
+ warn_hotswap
72
+ session.io.say(TRIGGER)
73
+ deploy_phase(entries, manifest)
74
+ watch
75
+ Cli::SUCCESS
76
+ end
77
+
78
+ private
79
+
80
+ attr_reader :session
81
+
82
+ # The watched set: the app file, which every iteration re-reads, and the
83
+ # asset source directories, which a synthesis that added one adds here
84
+ # too. Each line is the CDK's own (cdk-toolkit.ts:891), for the one file
85
+ # and for each directory.
86
+ def observe(manifest)
87
+ @app_path = File.expand_path(session.options.app.to_s)
88
+ @out_dir = File.expand_path(manifest.out_dir)
89
+ refresh_roots
90
+ io = session.io
91
+ io.say("'watch' is observing the file '#{@app_path}' for changes")
92
+ @roots.each { |root| io.say("'watch' is observing directory '#{root}' for changes") }
93
+ io.debug("'watch' leaves the synthesis directory #{@out_dir} and hidden files out")
94
+ @snapshot = snapshot_of
95
+ end
96
+
97
+ # The roots a fresh synthesis asks for. Says what arrived, for --verbose,
98
+ # when a re-synthesis recorded an asset the last one did not have.
99
+ def refresh_roots
100
+ roots = session.stacks.flat_map { |stack| stack.asset.records.map(&:source_path) }.uniq.sort
101
+ return if roots == @roots
102
+
103
+ unless @roots.empty?
104
+ added = roots - @roots
105
+ session.io.debug("'watch' now observes #{added.length} new asset director#{added.length == 1 ? "y" : "ies"}")
106
+ end
107
+ @roots = roots
108
+ end
109
+
110
+ def snapshot_of
111
+ files = {}
112
+ record(files, @app_path)
113
+ @roots.each { |root| walk(root, files) }
114
+ files
115
+ end
116
+
117
+ # A file that is gone is not in the snapshot at all: its absence is the
118
+ # disappearance the next poll reports, never a nil mtime to compare.
119
+ def record(files, path)
120
+ mtime = mtime_of(path)
121
+ files[path] = mtime if mtime
122
+ end
123
+
124
+ # Every file under a watched directory, hidden files and the synthesis
125
+ # directory left out, as the CDK's watch leaves its own out
126
+ # (toolkit.ts:1335, 1338). The walk is tolerant: a file gone before its
127
+ # stat, a directory that cannot be entered -- none of it is in this
128
+ # snapshot, and the next poll answers what it is by then.
129
+ def walk(directory, files)
130
+ Dir.children(directory).sort.each do |name|
131
+ next if name.start_with?(".")
132
+
133
+ path = File.join(directory, name)
134
+ next if path == @out_dir || path.start_with?("#{@out_dir}#{File::SEPARATOR}")
135
+
136
+ if File.directory?(path)
137
+ walk(path, files)
138
+ else
139
+ record(files, path)
140
+ end
141
+ end
142
+ rescue Errno::ENOENT, Errno::EACCES
143
+ nil
144
+ end
145
+
146
+ def mtime_of(path)
147
+ File.mtime(path)
148
+ rescue Errno::ENOENT
149
+ nil
150
+ end
151
+
152
+ # The CDK's change events (chokidar's add, change and unlink, the types
153
+ # its line at cdk-toolkit.ts:893 names): an appearance, an mtime that
154
+ # moved, a disappearance. Sorted by path, so one poll's lines are always
155
+ # in the same order.
156
+ def changes_between(before, after)
157
+ changes = []
158
+ (after.keys - before.keys).each { |path| changes << [ path, "add" ] }
159
+ (before.keys - after.keys).each { |path| changes << [ path, "unlink" ] }
160
+ (before.keys & after.keys).each do |path|
161
+ changes << [ path, "change" ] if before[path] != after[path]
162
+ end
163
+ changes.sort_by { |(path, _type)| path }
164
+ end
165
+
166
+ # The polling half of the loop: sleep, compare, and on any change one
167
+ # whole batch -- fresh synthesis, one line per file that moved, deploy.
168
+ # The CDK runs until its process is interrupted; stop_after ends this
169
+ # loop where a test needs it to.
170
+ def watch
171
+ loop do
172
+ break if @stop_after && @polled >= @stop_after
173
+
174
+ @polled += 1
175
+ @sleep.call(interval)
176
+ changed = changes_between(@snapshot, snapshot_of)
177
+ next if changed.empty?
178
+
179
+ changed.each do |(path, type)|
180
+ session.io.say("Detected change to '#{path}' (type: #{type}). Triggering 'ruby-cdk deploy'")
181
+ end
182
+ safe do
183
+ batch = next_batch
184
+ deploy_phase(batch.last, batch.first) if batch
185
+ end
186
+ @snapshot = snapshot_of
187
+ end
188
+ end
189
+
190
+ # One deployment and the batch behind it: files that move while the
191
+ # deploy runs are discovered against the snapshot taken between the
192
+ # synthesis and the deploy, and queued the way the CDK queues them
193
+ # (cdk-toolkit.ts:856) -- one more synthesis and deploy after this one
194
+ # ends. The snapshot is taken where it is because the synthesis of this
195
+ # batch already carried everything before it.
196
+ def deploy_phase(entries, manifest)
197
+ loop do
198
+ before = snapshot_of
199
+ safe { deploy(entries, manifest) }
200
+ now = snapshot_of
201
+ if changes_between(before, now).empty?
202
+ @snapshot = now
203
+ return
204
+ end
205
+
206
+ session.io.say(QUEUE)
207
+ batch = next_batch
208
+ return unless batch
209
+
210
+ manifest = batch.first
211
+ entries = batch.last
212
+ end
213
+ end
214
+
215
+ # The next batch's synthesis and its stacks: nil when the app file is
216
+ # broken mid-edit or the selection no longer holds. Reported, and the
217
+ # batch ends, as the CDK's invokeSafe ends it (toolkit.ts:1384-1396).
218
+ def next_batch
219
+ manifest = session.reiterate
220
+ session.say_synthesis_time
221
+ refresh_roots
222
+ [ manifest, order(manifest) ]
223
+ rescue StandardError, ScriptError => e
224
+ session.io.failure(e.is_a?(RubyCdk::Error) ? e.message : "#{e.class}: #{e.message}")
225
+ nil
226
+ end
227
+
228
+ # One deploy of the entries, in dependency order, through the same
229
+ # clients and engine the deploy command uses. Nothing here answers an
230
+ # exit code: a deploy that fails is printed by the engine, and the loop
231
+ # watches on, as the CDK's invokeDeployFromWatch does
232
+ # (cdk-toolkit.ts:1546-1549).
233
+ def deploy(entries, manifest)
234
+ entries.each do |entry|
235
+ stack = session.stack(entry.name)
236
+ region = session.region_of(stack)
237
+ clients = session.clients(region:)
238
+ session.io.debug("#{entry.name}: region #{region}#{session.region_source(stack)}")
239
+
240
+ identity = session.identity(clients)
241
+ deployer = session.deployer(clients)
242
+ published = Assets.publish(stack, store: clients.store, owner: identity.account)
243
+ session.io.debug("#{entry.name}: assets #{published[:uploaded]} uploaded, #{published[:skipped]} already in the bucket")
244
+
245
+ deployer.deploy(stack:, template: File.binread(File.join(manifest.out_dir, entry.template)),
246
+ identity:, options: @deploy_options)
247
+ end
248
+ end
249
+
250
+ # One invocation's failure is reported, never propagated, and the watch
251
+ # goes on; the next change tries the whole batch again. This is the CDK's
252
+ # invokeSafe (toolkit.ts:1384-1396), which reports with
253
+ # formatErrorMessage -- the error's own words, as the engine's errors
254
+ # carry them.
255
+ def safe
256
+ yield
257
+ rescue StandardError, ScriptError => e
258
+ session.io.failure(e.is_a?(RubyCdk::Error) ? e.message : "#{e.class}: #{e.message}")
259
+ end
260
+
261
+ # The two drift warnings, once per command, above the deploys, as the
262
+ # deploy command prints them (section 9). The CDK prints them at every
263
+ # hotswap deploy (toolkit.ts:825-829), which under its watch is every
264
+ # iteration; here the command is the unit, as it is for deploy.
265
+ def warn_hotswap
266
+ return unless @deploy_options.hotswap || @deploy_options.hotswap_fallback
267
+
268
+ Deploy::Hotswap::WARNINGS.each { |line| session.io.say(line) }
269
+ end
270
+
271
+ # The stacks of one batch, from the synthesis it carries: the selection
272
+ # is asked again of the fresh app, so a stack the edit added and --all
273
+ # covers joins the next deploy, and a selection that no longer holds is
274
+ # reported with the batch, not with the watch.
275
+ def order(manifest)
276
+ selected = Selection.choose(session.stacks, names: @names, all: session.options.all, command: "watch")
277
+ Selection.ordered(manifest.entries, selected: selected.map(&:stack_name), direction: :up,
278
+ exclusively: session.options.exclusively)
279
+ end
280
+
281
+ # --interval, in seconds, checked like every other option before anything
282
+ # runs. Not a flag of the CDK's: its watcher is event driven, and this is
283
+ # the polling period the law of section 8 asks for.
284
+ def interval
285
+ @interval ||= begin
286
+ value = session.options.interval
287
+ unless value.is_a?(Numeric) && value.positive?
288
+ raise Usage.new("--interval takes a positive number of seconds, not #{value.inspect}",
289
+ next_step: "Run `ruby-cdk watch` again with --interval SECONDS, or without it for one second.")
290
+ end
291
+
292
+ value.to_f
293
+ end
294
+ end
295
+ end
296
+ end
297
+ end
data/lib/ruby_cdk/cli.rb CHANGED
@@ -20,9 +20,13 @@ require_relative "cli/synth"
20
20
  require_relative "cli/list"
21
21
  require_relative "cli/validate"
22
22
  require_relative "cli/diff"
23
+ require_relative "cli/drift"
24
+ require_relative "cli/lookup"
23
25
  require_relative "cli/new"
24
26
  require_relative "cli/operation"
25
27
  require_relative "cli/bootstrap"
28
+ require_relative "cli/watch"
29
+ require_relative "cli/import"
26
30
  require_relative "cli/report"
27
31
 
28
32
  module RubyCdk
@@ -71,7 +75,8 @@ module RubyCdk
71
75
 
72
76
  class_option :force, type: :boolean, aliases: "-f",
73
77
  desc: "Deploy even when the template and the parameters are unchanged; with destroy, do not ask for confirmation; " \
74
- "with new, write into the directory named only when it is empty"
78
+ "with new, write into the directory named only when it is empty; with import, import when the template also " \
79
+ "holds updates or deletes"
75
80
 
76
81
  class_option :quiet, type: :boolean, aliases: "-q", desc: "Print no progress, and for synth no template"
77
82
 
@@ -126,6 +131,19 @@ module RubyCdk
126
131
  method_option :parameters, type: :string,
127
132
  desc: "Template parameters for the deploy, as KEY=VALUE pairs separated by commas"
128
133
  method_option :tags, type: :string, desc: "Tags for the stacks, as KEY=VALUE pairs separated by commas"
134
+ method_option :notification_arns, type: :array,
135
+ desc: "ARNs of SNS topics that CloudFormation will notify with stack related events. " \
136
+ "These will be added to ARNs specified with the 'notificationArns' stack property"
137
+ method_option :rollback_trigger_alarm_arns, type: :array,
138
+ desc: "ARNs of CloudWatch alarms that CloudFormation monitors during the deployment. If any alarm goes to ALARM state " \
139
+ "the deployment is rolled back. A maximum of 5 can be specified. Alarms are treated as metric alarms " \
140
+ "(AWS::CloudWatch::Alarm). Pass the single value 'none' to clear any rollback triggers previously configured on the stack"
141
+ method_option :monitoring_time_minutes, type: :numeric,
142
+ desc: "The number of minutes CloudFormation continues to monitor the rollback trigger alarms after the deployment " \
143
+ "completes, before cleaning up old resources. Must be a whole number between 0 and 180. " \
144
+ "Must be used together with --rollback-trigger-alarm-arns"
145
+ method_option :import_existing_resources, type: :boolean,
146
+ desc: "Indicates if the stack set imports resources that already exist"
129
147
  method_option :outputs_file, type: :string, aliases: "-O",
130
148
  desc: "File the outputs of each stack are written to, as JSON"
131
149
  method_option :previous_parameters, type: :boolean, default: true,
@@ -149,6 +167,47 @@ module RubyCdk
149
167
  Cli::Operation.new(session, kind: :destroy).call(names)
150
168
  end
151
169
 
170
+ desc "watch [STACK...]", "Watch the app's files, and synth and deploy again on each change, hotswapping by default"
171
+ method_option :all, type: :boolean, desc: "Watch and deploy every stack of the app, dependencies first"
172
+ method_option :exclusively, type: :boolean, aliases: "-e",
173
+ desc: "Deploy only the stacks named, not the ones they depend on"
174
+ method_option :parameters, type: :string,
175
+ desc: "Template parameters for each deploy, as KEY=VALUE pairs separated by commas"
176
+ method_option :tags, type: :string, desc: "Tags for the stacks, as KEY=VALUE pairs separated by commas"
177
+ method_option :previous_parameters, type: :boolean, default: true,
178
+ desc: "A parameter that is not named again keeps the value the stack was last deployed with. " \
179
+ "--no-previous-parameters: every parameter without a template default must be named on every deploy"
180
+ method_option :hotswap, type: :boolean,
181
+ desc: "Attempts to perform a 'hotswap' deployment on every change, but does not fall back to a full deployment " \
182
+ "if that is not possible. Instead, changes to any non-hotswappable properties are ignored. " \
183
+ "'true' by default, use --no-hotswap to turn off"
184
+ method_option :hotswap_fallback, type: :boolean,
185
+ desc: "Attempts to perform a 'hotswap' deployment on every change, which skips the change set and updates the " \
186
+ "stack directly, and falls back to a full deployment if that is not possible"
187
+ method_option :interval, type: :numeric, default: 1,
188
+ desc: "Seconds between two polls of the watched files"
189
+ def watch(*names)
190
+ Cli::Watch.run(session, names)
191
+ end
192
+
193
+ desc "import [STACK]", "Import existing resource(s) into the given STACK"
194
+ method_option :execute, type: :boolean, default: true,
195
+ desc: "Execute the change set. --no-execute leaves it to execute in the console"
196
+ method_option :change_set_name, type: :string, desc: "Name of the CloudFormation change set to create"
197
+ method_option :notification_arns, type: :array,
198
+ desc: "ARNs of SNS topics that CloudFormation will notify with stack related events"
199
+ method_option :record_resource_mapping, type: :string,
200
+ desc: "Write the mapping of the resources to import to the identifiers they are imported under to this file, " \
201
+ "and perform no import"
202
+ method_option :resource_mapping, type: :string, aliases: "-m",
203
+ desc: "A JSON file of { logical id => { property => value } } naming the physical resources to import, instead " \
204
+ "of asking for each one. Can be run from scripts"
205
+ method_option :resource_mapping_inline, type: :string,
206
+ desc: "Inline JSON resource mapping, e.g. '{\"MyResource\":{\"TableName\":\"my-table\"}}'"
207
+ def import(*names)
208
+ Cli::Import.run(session, names)
209
+ end
210
+
152
211
  desc "diff [STACK...]", "Show what would change in each stack, against the template that is deployed"
153
212
  method_option :all, type: :boolean, desc: "Diff every stack of the app, dependencies first"
154
213
  method_option :exclusively, type: :boolean, aliases: "-e",
@@ -167,11 +226,27 @@ module RubyCdk
167
226
  Cli::Diff.run(session, names)
168
227
  end
169
228
 
229
+ desc "drift [STACK...]", "Detect drifts in the given CloudFormation stack(s)"
230
+ method_option :all, type: :boolean, desc: "Detect drift in every stack of the app, dependencies first"
231
+ method_option :exclusively, type: :boolean, aliases: "-e",
232
+ desc: "Detect drift only in the stacks named, not the ones they depend on"
233
+ method_option :fail, type: :boolean,
234
+ desc: "Fail with exit code 1 if drift is detected"
235
+ def drift(*names)
236
+ Cli::Drift.run(session, names)
237
+ end
238
+
170
239
  desc "new PROJECT", "Create the directory PROJECT with the app scaffold: cdk.rb, one stack, the Gemfile and the git files"
171
240
  def new(project = nil)
172
241
  Cli::New.run(session, project)
173
242
  end
174
243
 
244
+ desc "lookup", "Answer the context lookups the app reads (D-022) against AWS, and write them into cdk.context.json " \
245
+ "beside the app file"
246
+ def lookup(*names)
247
+ Cli::Lookup.run(session, names)
248
+ end
249
+
175
250
  desc "bootstrap", "Deploy the tool's asset bucket as the ruby-cdk-bootstrap stack"
176
251
  method_option :deployer, type: :string,
177
252
  desc: "ARN of the principal that reads the assets back out of the bucket. Nothing given: the account's own principals"
@@ -72,6 +72,7 @@ module RubyCdk
72
72
  @handler = valid_handler(handler)
73
73
  @code = valid_code(code)
74
74
  @function_name = valid_function_name(function_name)
75
+ @description = valid_description(description)
75
76
  @role = role || IAM::Role.new(self, "ServiceRole",
76
77
  assumed_by: "lambda.amazonaws.com",
77
78
  managed_policy_arns: [ BASIC_EXECUTION_POLICY_ARN ])
@@ -87,7 +88,7 @@ module RubyCdk
87
88
 
88
89
  @resource = resource("Resource", "AWS::Lambda::Function",
89
90
  FunctionName: @function_name,
90
- Description: description,
91
+ Description: @description,
91
92
  Code: code_location,
92
93
  Handler: @handler,
93
94
  Runtime: @runtime,
@@ -199,11 +200,31 @@ module RubyCdk
199
200
  "CloudFormation's Code shape, got #{code.inspect}"
200
201
  end
201
202
 
203
+ # The CDK's two validations of a literal function name (function.ts:1009-1016
204
+ # at the pinned commit): at most 64 characters, only letters, numbers,
205
+ # hyphens and underscores. An intrinsic name is the CDK's unresolved token
206
+ # and is not checked, here as there.
207
+ MAX_NAME_LENGTH = 64
208
+ NAME_PATTERN = /\A[a-zA-Z0-9_-]+\z/
209
+
210
+ # The CDK's description check (function.ts:1018-1022): at most 256 characters.
211
+ MAX_DESCRIPTION_LENGTH = 256
212
+
202
213
  def valid_function_name(name)
203
214
  return nil if name.nil?
204
- return name if name.is_a?(String) && !name.empty?
215
+ raise UsageError, "#{path}: the function name must be a non-empty String, got #{name.inspect}" unless name.is_a?(String) && !name.empty?
216
+ raise UsageError, "#{path}: Function name can not be longer than #{MAX_NAME_LENGTH} characters but has #{name.length} characters." if name.length > MAX_NAME_LENGTH
217
+ return name if name.match?(NAME_PATTERN)
218
+
219
+ raise UsageError, "#{path}: Function name #{name} can contain only letters, numbers, hyphens, or underscores with no spaces."
220
+ end
221
+
222
+ def valid_description(description)
223
+ return nil if description.nil?
224
+ raise UsageError, "#{path}: the description must be a String, got #{description.inspect}" unless description.is_a?(String)
225
+ return description if description.length <= MAX_DESCRIPTION_LENGTH
205
226
 
206
- raise UsageError, "#{path}: the function name must be a non-empty String, got #{name.inspect}"
227
+ raise UsageError, "#{path}: Function description can not be longer than #{MAX_DESCRIPTION_LENGTH} characters but has #{description.length} characters."
207
228
  end
208
229
 
209
230
  def code_location