.
├── README.md <-- This instructions file
├── event.json <-- API Gateway Proxy Integration event payload
├── api-gateway-sample <-- Source code for a lambda function
│ ├── __init__.py
│ ├── app.py <-- Lambda function code
│ ├── requirements.txt <-- Lambda function code
├── template.yaml <-- SAM Template
└── tests <-- Unit tests
└── unit
├── __init__.py
└── test_handler.py
- AWS CLI already configured with Administrator permission
- Python 3 installed
- Docker installed
Invoking function locally using a local sample payload
sam local invoke AuthFunction --event event.json
Invoking function locally through local API Gateway
sam local start-api
If the previous command ran successfully you should now be able to hit the following local endpoint to invoke your function http://localhost:3000/auth
Firstly, we need a S3 bucket
where we can upload our Lambda functions packaged as ZIP before we deploy anything - If you don't have a S3 bucket to store code artifacts then this is a good time to create one:
aws s3 mb s3://BUCKET_NAME
Next, run the following command to package our Lambda function to S3:
sam package \
--output-template-file packaged.yaml \
--s3-bucket REPLACE_THIS_WITH_YOUR_S3_BUCKET_NAME
Next, the following command will create a Cloudformation Stack and deploy your SAM resources.
sam deploy \
--template-file packaged.yaml \
--stack-name api-gateway-sample \
--capabilities CAPABILITY_IAM
After deployment is complete you can run the following command to retrieve the API Gateway Endpoint URL:
aws cloudformation describe-stacks \
--stack-name api-gateway-sample \
--query 'Stacks[].Outputs[?OutputKey==`AuthApi`]' \
--output table
To simplify troubleshooting, SAM CLI has a command called sam logs. sam logs lets you fetch logs generated by your Lambda function from the command line. In addition to printing the logs on the terminal, this command has several nifty features to help you quickly find the bug.
NOTE
: This command works for all AWS Lambda functions; not just the ones you deploy using SAM.
sam logs -n AuthFunction --stack-name api-gateway-sample --tail
You can find more information and examples about filtering Lambda function logs in the SAM CLI Documentation.
Next, we install test dependencies and we run pytest
against our tests
folder to run our initial unit tests:
pip install pytest pytest-mock --user
python -m pytest tests/ -v
In order to delete our Serverless Application recently deployed you can use the following AWS CLI Command:
aws cloudformation delete-stack --stack-name api-gateway-sample