Creating a Basic Hello-World Lambda from SAM Cookiecutter Template
Creating A Basic Hello-World Lambda from SAM Cookiecutter Template#
chap1 project directory, we will clone a sample project that AWS provides for Python and SAM.
From that same directory, we will use
sam init --location cookiecutter-aws-sam-python to create our dicebot lambda directory. We will be prompted for certain parameters after sending this command, so fill them in according to the example shown below.
We should now see a directory called
dice_bot created within our
chap1 directory. This
dice_bot directory will contain all of the important code for this project. So go ahead and navigate into that directory and init a git project there, as shown below.
If we have
VSCode installed, we can use the command
code . to open VSCode with the explorer already pointing to this directory.
Poke around the directory structure a bit to see the basic scaffolding created by
cookiecutter. We won't need to touch most of these files yet, but there are a few essential files we need to review, and modify.
The main files we will be concerned with are:
README.md- Always write useful instructions into the README.md file. In
VSCode, we this file and do
Ctrl+Shift+Vto preview it.
template.yaml- This is a SAM infrastructure template that defines resources being deployed to AWS
first_function/app.py- Python application code that AWS Lambda will execute
events/first_function.json- Sample event that will be used when testing the Lambda locally. This will simulate the slack slash command.
Update the SAM template#
template.yaml file provided by AWS is almost ready to use as-is. It has defined a generic lambda resource and an API Gateway that we can use with slack. We need to just make a few minor tweaks to make it ready to go.