Amazon AWS provides a service called AWS Lambda that allows serverless functions to be created and hosted on their cloud platform. Even though serverless functions are hosted online Amazon provide a way to work with serverless functions locally through a CLI tool called AWS SAM (Serverless Application Model).
This guide shows how to set up and use the AWS SAM CLI tool for web development.
How to install on MacOS
The wget library is required, use brew to install it if you don’t have it.
brew install wgetDepending on your Apple device you will require the ARM64 for M processors or the x86 version for Intel Apple devices.
Option 1 - x84_64 for Intel MacOS devices
Use the wget to download the x84 installer file.
wget https://github.com/aws/aws-sam-cli/releases/latest/download/aws-sam-cli-macos-x86_64.pkgRun the installer after the download has been completed.
sudo installer -pkg ./aws-sam-cli-macos-x86_64.pkg -target /Option 2 - ARM64 for M Processor MacOS devices
Use wget to download the ARM64 version of the installer file.
wget https://github.com/aws/aws-sam-cli/releases/latest/download/aws-sam-cli-macos-arm64.pkgRun the installer file after download.
sudo installer -pkg ./aws-sam-cli-macos-arm64.pkg -target /Verify Install
After using either of the above methods to install verify that the sam command is working.
The version number should output.
sam --versionThis was the output for me.
SAM CLI, version 1.118.0
How to install on Linux
The unzip command is required, install it using your package manager.
# Ubuntu APT package manager install
sudo apt install -y unzipDownload the AWS SAM CLI zip file using the command below.
wget https://github.com/aws/aws-sam-cli/releases/latest/download/aws-sam-cli-linux-x86_64.zipUnzip the zip file to a folder named sam-installation.
unzip aws-sam-cli-linux-x86_64.zip -d sam-installationNow run the install script.
sudo ./sam-installation/installVerify that the installation was successful.
sam --versionHow to use
Here is a collection of commands to use the AWS SAM tool.
Output the full list of SAM commands.
sam -hCreate a new lambda project
sam initTo run sam on an existing lambda project locally change the current directory to the lambda project and run either of the following…
Run the serverless function using the invoke command.
sam local invoke --event events/event.jsonTo host the serverless function on a local web server use this command.
sam local start-api --port 8080After running the web server go to the outputted URL in the web browser or use curl to run the serverless function.
The following example is my URL from my local machine.
curl http://127.0.0.1:8080/hello