There are 2 ways of running a Serverless app locally: 1) By invoking an individual Lambda function or 2) By running a local HTTP server that simulates API Gateway. For this workshop, we will focus on number 2, but you can learn about invoking individual functions in the SAM Local Invoke reference.
In the terminal, run the following command from the root directory of the sam-app folder:
cd ~/environment/sam-app sam build sam local start-api --port 8080
In a Cloud9 workspace, you must use port 8080, 8081 or 8082 to be able to open the URL in the local browser for preview.
If you are using Cloud9 and get an insufficent space or
ENOSPC: no space left on device error, you may resize your volume by using the following commands from your Cloud9 terminal.
wget https://cicd.serverlessworkshops.io/assets/resize.sh chmod +x resize.sh ./resize.sh 20
Once your local server is running, we can send HTTP requests to test it. Chose one of the following options:
Without killing the running process, open a new terminal.
Test your endpoint by running a
curl command that triggers an HTTP GET request.
In Cloud9, go to the top menu and chose Tools > Preview > Preview Running Application. A browser tab will open, append
/hello to the end of the URL. This will invoke your Lambda function locally.
Note how SAM is pulling the Docker container image sam/emulation-dotnetcore3.1 automatically. This is how SAM is able to simulate the Lambda runtime locally and run your function within it. The first invocation might take a few seconds due to the docker pull command, but subsequent invocations should be much faster.