sam local can execute lambda in a local docker environment simulating the lambda environment on the cloud. not exactly same though.
By default uses the local AWS credential
"sam local start-api" - with hot reload, better for debugging but still needs to reattach debugger
`sam build` builds the project, and the artifacts are put under .aws/build directory, which includes:
template.yaml - the template
each lambda in separate directory, each includes:
package.json
node_modules
the handler (such as build/handlers/)
For details (node.js) see https://docs.aws.amazon.com/lambda/latest/dg/nodejs-package.html
sam build do a complete build for each of the functions, which includes 'npm install', so it's very very slow. It's desirable to do a quick build...
with `--use-container` sam builds inside a container which gives a standard environment. however it makes more difficult to pass things inside
SAM Build uses various builders to build various types of projects (Java Maven, Java Gradle, Node.js, etc.)
The builders and their workflows can be found in this project: https://github.com/aws/aws-lambda-builders
This allows SAM build with a custom build flow, instead of the standard one. See https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/building-custom-runtimes.html
This works both with or without "--use-container"
To use:
add a Makefile under the code (!!) root, which is available inside container. Not the whole project root (not alongside template)
note the Makefile format - use TAB not spaces
note the commands are run under sh not bash
put the custom build flow inside Makefile - note that now it's your responsibility to do everything right - the standard flow would not be executed
if your makefile does nothing, build will succeed with nothing
put "Metadata:".. (see aws document) in template under the function that you wish to build with custom flow
`sam package` zips the artifacts, upload them to a S3, and return a template with reference to local artifacts (lambda code) replaced with S3 url
`sam deploy` deploys the stack
Without providing URL, AWS SDK automatically construct end point from configured AWS region.
So, without providing URL, uses table on cloud, not on local.
To debug with DynamoDB local, provide endpoint URL when creating DynamoDB or DocumentClient instance
SAM Local will not create table - use aws CLI
"sam local .... -d 9229" - listen on port
To attach to a debugger process launched in terminal, use below code, note:
protocol: "inspector" or "legacy" for specific protocol, or just (best) with "auto"
localRoot: match codeUri in template
{
"type": "node",
"request": "attach",
"name": "Attach to SAM Local",
"address": "localhost",
"port": 9229,
"localRoot": "${workspaceRoot}/src_dir_of_code",
"remoteRoot": "/var/task",
"protocol": "auto"
}
To launch and attach, use below:
Issue: after running, the debug console does not output the execution result (the JSON return body). guess is that the result is returned after debug session terminated. if I configure "console" to the integrated terminal or outside terminal, I can view the result but then there's another annoyance - at breakpoint the execution automatically continue without I press F5.
{
"type": "node",
"request": "launch",
"name": "Test FindContacts",
"address": "localhost",
"port": 9229,
"cwd":"${workspaceFolder}",
"localRoot": "${workspaceRoot}/src",
"remoteRoot": "/var/task",
"runtimeExecutable": "sam",
"runtimeArgs": [
"local","invoke","FindContacts","-e","examples/find-contacts.json","-d","9229"
],
},
Issue: when debugging runtime nodejs.8.10 VS code cannot find awslambda/index.js. This issue can be ignored though. (see my comments here https://github.com/awslabs/aws-sam-cli/issues/555)
Or use Google chrome "about:inspect" for inspector protocol
Breakpoint - since source is in "/var/task" of docker container, must correctly specify localRoot and remoteRoot. "localRoot" must be the directory containing the source code, i.e. corresponding the "codeUri" in SAM template.
`sam validate` can fail without message (except the template is invalid) but if ignore the error, package and deploy works just fine. So don't use.