Bootstrap FreeKB - Amazon Web Services (AWS) - Create your first Serverless Application Model (SAM) Hello World app in VSCode
Amazon Web Services (AWS) - Create your first Serverless Application Model (SAM) Hello World app in VSCode

Updated:   |  Amazon Web Services (AWS) articles

Assuming you are running VSCode on a Windows PC, you will first need to download and install the SAM CLI on Windows and then in VSCode Terminal, use the sam --version command to verify the SAM CLI is working.

PS C:\Users\jerem\VSCode> sam --version
SAM CLI, version 1.103.0

 

Running the SAM app in VSCode will probably require Docker Desktop for Windows. Check out my article Install Docker on Windows to get Docker Desktop for Windows installed.


In VSCode, select View > Command Palette (or Ctrl + Shift + P) and selectt AWS: Create Lambda SAM Application.

Select a runtime, such as python 3.11 for a Python app. If you select the option that doesn't contain "image" this will create a zip file that contains the various files that make up the app. If you select image, a container image will be created.

It may take a minute or two for the app to be created. Once created, it should be listed in the left panel of VSCode.

 

Select the template.yaml file and click on AWS: Add Debug Configuration.

 

This should create a file named launch.json that contains the markup like this.

{
    "configurations": [
        {
            "type": "aws-sam",
            "request": "direct-invoke",
            "name": "lambda-python3.11:HelloWorldFunction",
            "invokeTarget": {
                "target": "template",
                "templatePath": "${workspaceFolder}/lambda-python3.11/template.yaml",
                "logicalId": "HelloWorldFunction"
            },
            "lambda": {
                "payload": {},
                "environmentVariables": {}
            }
        }
    ]
}

 

Select launch.json and select Run Start Debugging (or F5). The output console should contain something like this.

2023-11-20 20:15:30 [INFO]: Command: (not started) [C:\Program Files\Amazon\AWSSAMCLI\bin\sam.cmd init --name lambda-python3.11 --no-interactive --dependency-manager pip --runtime python3.11 --app-template hello-world]
2023-11-20 20:15:35 [INFO]: 
	SAM CLI now collects telemetry to better understand customer needs.

	You can OPT OUT and disable telemetry collection by setting the
	environment variable SAM_CLI_TELEMETRY=0 in your shell.
	Thanks for your help!

	Learn More: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-telemetry.html


2023-11-20 20:15:45 [INFO]: 
Cloning from https://github.com/aws/aws-sam-cli-app-templates (process may take a moment)

2023-11-20 20:37:32 [INFO]: Preparing to debug locally: Lambda "app.lambda_handler"
2023-11-20 20:37:32 [INFO]: Building SAM application...
2023-11-20 20:37:34 [INFO]: Command: (not started) [C:\Program Files\Amazon\AWSSAMCLI\bin\sam.cmd build --build-dir C:\Users\johndoe\AppData\Local\Temp\aws-toolkit-vscode\vsctkzywvMv\output --template C:/Users/jerem/VSCode/lambda-python3.11/template.yaml --manifest C:\Users\johndoe\AppData\Local\Temp\aws-toolkit-vscode\vsctkzywvMv\debug-requirements.txt]
2023-11-20 20:37:42 [INFO]: 
	SAM CLI now collects telemetry to better understand customer needs.

	You can OPT OUT and disable telemetry collection by setting the
	environment variable SAM_CLI_TELEMETRY=0 in your shell.
	Thanks for your help!

	Learn More: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-telemetry.html


2023-11-20 20:37:45 [INFO]: Starting Build use cache

2023-11-20 20:37:45 [INFO]: Manifest file is changed (new hash: 57812bd8bbb49d0ee945ad443fa3e92b) or dependency folder (.aws-sam\deps\1594dbd8-5e4f-4b3c-a783-166294ea2835) is missing for (HelloWorldFunction), downloading dependencies and copying/building source

2023-11-20 20:37:45 [INFO]: Building codeuri: C:\Users\johndoe\VSCode\lambda-python3.11\hello_world runtime: python3.11 metadata: {} architecture: x86_64 functions: HelloWorldFunction

2023-11-20 20:37:47 [INFO]:  Running PythonPipBuilder:CleanUp

2023-11-20 20:37:47 [INFO]:  Running PythonPipBuilder:ResolveDependencies

2023-11-20 20:38:00 [INFO]:  Running PythonPipBuilder:CopySource

2023-11-20 20:38:14 [INFO]:  Running PythonPipBuilder:CopySource

2023-11-20 20:38:14 [INFO]: 
Build Succeeded

Built Artifacts  : ..\..\AppData\Local\Temp\aws-toolkit-vscode\vsctkzywvMv\output
Built Template   : ..\..\AppData\Local\Temp\aws-toolkit-vscode\vsctkzywvMv\output\template.yaml

Commands you can use next
=========================
[*] Validate SAM template: sam validate

[*] Invoke Function: sam local invoke -t ..\..\AppData\Local\Temp\aws-toolkit-vscode\vsctkzywvMv\output\template.yaml

[*] Test Function in the Cloud: sam sync --stack-name {{stack-name}} --watch

[*] Deploy: sam deploy --guided --template-file ..\..\AppData\Local\Temp\aws-toolkit-vscode\vsctkzywvMv\output\template.yaml

2023-11-20 20:38:16 [INFO]: Build complete.
2023-11-20 20:38:16 [INFO]: Starting SAM application locally
2023-11-20 20:38:17 [INFO]: AWS.running.command
Invoking app.lambda_handler (python3.11)
Local image was not found.
Removing rapid images for repo public.ecr.aws/sam/emulation-python3.11
Building image......................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................

Using local image: public.ecr.aws/lambda/python:3.11-rapid-x86_64.

Mounting C:\Users\johndoe\AppData\Local\Temp\aws-toolkit-vscode\vsctkOYabHU\output\HelloWorldFunction as /var/task:ro,delegated, inside runtime container
START RequestId: 7fe07a9d-f404-4ed4-a5c0-bda0058ccb1f Version: $LATEST
Prepending Lambda task root to path: /var/task
Starting debugger...
0.22s - Debugger warning: It seems that frozen modules are being used, which may
0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
0.00s - to python to disable frozen modules.
0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.
Debugger waiting for client...
2023-11-20 21:00:03 [INFO]: Attaching debugger to SAM application...
2023-11-20 21:00:05 [INFO]: Debugger attached
END RequestId: 7fe07a9d-f404-4ed4-a5c0-bda0058ccb1f
REPORT RequestId: 7fe07a9d-f404-4ed4-a5c0-bda0058ccb1f	Init Duration: 0.59 ms	Duration: 18763.00 ms	Billed Duration: 18764 ms	Memory Size: 128 MB	Max Memory Used: 128 MB	
{"statusCode": 200, "body": "{\"message\": \"hello world\"}"}

Command stopped: "sam local invoke"

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 5baf0e in the box below so that we can be sure you are a human.