Skip to content

Commit 7b5aae7

Browse files
authored
YAML Handling Improvements (#1159)
PR turned out larger than I'd hoped but it covers significant ground: * We no longer produce a minimal template for debugs targeting a template file. * We still produce a temporary template, but it: * Uses all the fields from the old one aside from overwriting the handler name of the targeted function with any refs to parameters or parameter overrides * This is to accommodate the Python debug case, which requires a temporary shim file * The temporary template is now created as a sibling to the existing template file so the build can use relative pathing for any overrides * Any parameters in the template file are now override-able through the `sam.template.parameters` field in the launch config. * We now support `AWS::Lambda::Function`-type resources As a side-effect, other template-related things (e.g. Lambda layers) should work after doing this. I haven't manually tested this yet. We may want to consider how we handle the runtime, and handle it like the handler. As of now, if a runtime isn't explicitly provided (read: if it's provided as a ref to a parameter), the template won't validate. This is covered in a comment.
1 parent a0ce4db commit 7b5aae7

18 files changed

+810
-137
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,13 +307,14 @@
307307
"type": "boolean"
308308
},
309309
"template": {
310+
"description": "%AWS.configuration.description.awssam.debug.template%",
310311
"properties": {
311312
"parameters": {
313+
"description": "%AWS.configuration.description.awssam.debug.templateParameters%",
312314
"additionalProperties": {
313315
"type": [
314316
"string",
315-
"number",
316-
"boolean"
317+
"number"
317318
]
318319
},
319320
"type": "object"

package.nls.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@
7979
"AWS.configuration.description.awssam.debug.dockerNetwork": "Specifies the name or id of an existing Docker network that Lambda Docker containers should connect to.",
8080
"AWS.configuration.description.awssam.debug.localArguments": "Additional arguments to pass to the `sam local` command.",
8181
"AWS.configuration.description.awssam.debug.skipNewImageCheck": "Specifies whether the command should skip pulling down the latest Docker image for Lambda runtime (default: false).",
82+
"AWS.configuration.description.awssam.debug.template": "Values to override in the template",
83+
"AWS.configuration.description.awssam.debug.templateParameters": "Key:value mappings for SAM template parameter overrides. More information can be found here: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/stackinstances-override.html#stackinstances-override-cli",
8284
"AWS.configuration.description.awssam.debug.snippets.lambdaCode.label": "AWS SAM: Direct Lambda handler invoke",
8385
"AWS.configuration.description.awssam.debug.snippets.lambdaCode.description": "A new configuration for invoking an AWS Lambda directly via the SAM CLI.",
8486
"AWS.configuration.description.awssam.debug.snippets.lambdaTemplate.label": "AWS SAM: Template-based Lambda invoke",
@@ -270,7 +272,7 @@
270272
"AWS.sam.debugger.missingTemplate": "Cannot find template file (must be workspace-relative, or absolute): {0}",
271273
"AWS.sam.debugger.missingResource": "Cannot find template resource \"{0}\" in template file: {1}",
272274
"AWS.sam.debugger.missingField": "Missing required field \"{0}\" in debug config",
273-
"AWS.sam.debugger.resourceNotAFunction": "Template Resource {0} in Template file {1} needs to be of type {2}",
275+
"AWS.sam.debugger.resourceNotAFunction": "Template Resource {0} in Template file {1} needs to be of type {2} or {3}",
274276
"AWS.sam.debugger.unsupportedRuntime": "Runtime for Template Resource {0} in Template file {1} is either undefined or unsupported.",
275277
"AWS.sam.debugger.useExistingConfig": "AWS Toolkit detected an existing legacy configuration for this function. Create the debug config based on the legacy config?",
276278
"AWS.sam.debugger.useExistingConfig.migrate": "Create based on the legacy config",

src/lambda/local/debugConfiguration.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,18 @@ export function getCodeRoot(
8888
}
8989
case 'template': {
9090
const templateInvoke = config.invokeTarget as TemplateTargetProperties
91+
const template = getTemplate(folder, config)
92+
if (!template) {
93+
return undefined
94+
}
9195
const templateResource = getTemplateResource(folder, config)
9296
if (!templateResource?.Properties) {
9397
return undefined
9498
}
9599
const fullPath = tryGetAbsolutePath(folder, templateInvoke.templatePath)
96100
const templateDir = path.dirname(fullPath)
97-
return pathutil.normalize(path.resolve(templateDir ?? '', templateResource?.Properties?.CodeUri))
101+
const uri = CloudFormation.getStringForProperty(templateResource?.Properties?.CodeUri, template)
102+
return uri ? pathutil.normalize(path.resolve(templateDir ?? '', uri)) : undefined
98103
}
99104
default: {
100105
throw Error('invalid invokeTarget') // Must not happen.
@@ -115,8 +120,17 @@ export function getHandlerName(
115120
return codeInvoke.lambdaHandler
116121
}
117122
case 'template': {
123+
const template = getTemplate(folder, config)
124+
if (!template) {
125+
return ''
126+
}
118127
const templateResource = getTemplateResource(folder, config)
119-
return templateResource?.Properties?.Handler!!
128+
const propertyValue = CloudFormation.resolvePropertyWithOverrides(
129+
templateResource?.Properties?.Handler!!,
130+
template,
131+
config.sam?.template?.parameters
132+
)
133+
return propertyValue ? propertyValue.toString() : ''
120134
}
121135
default: {
122136
// Should never happen.

src/lambda/local/detectLocalLambdas.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ async function detectLambdasFromTemplate(
6262
workspaceFolder,
6363
templatePath,
6464
templateGlobals: template.Globals,
65-
handler: getHandler(resources[key]!),
65+
handler: getHandler(resources[key]!, template),
6666
resource: resources[key]!,
6767
}))
6868
}
6969

70-
function getHandler(resource: CloudFormation.Resource): string | undefined {
70+
function getHandler(resource: CloudFormation.Resource, template: CloudFormation.Template): string | undefined {
7171
if (resource.Properties && resource.Properties.Handler) {
72-
return resource.Properties.Handler
72+
return CloudFormation.getStringForProperty(resource.Properties.Handler, template)
7373
}
7474

7575
return undefined

0 commit comments

Comments
 (0)