This is a sample template for sam-app - Below is a brief explanation of what we have generated for you:
.
├── README.md <-- This instructions file
├── src/functions <-- Source code for a lambda function
│ └── app.py <-- Lambda function code
├── requirements.txt <-- Python dependencies
├── template.yaml <-- SAM template
└── tests <-- Unit tests
└── unit
├── __init__.py
└── test_handler.py
- AWS CLI already configured with at least PowerUser permission
- Python 3 installed
- Docker installed
Invoking function locally through local API Gateway
sam local start-api --env-vars sam-local.json
If the previous command ran successfully you should now be able to hit the following local endpoint to invoke your function http://localhost:3000/hello
SAM CLI is used to emulate both Lambda and API Gateway locally and uses our template.yaml
to understand how to bootstrap this environment (runtime, where the source code is, etc.) - The following excerpt is what the CLI will read in order to initialize an API and its routes:
...
Events:
HelloWorld:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /hello
Method: get
Run the following command to package our Lambda function to S3:
sam package \
--template-file template.yaml \
--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 sam-app \
--capabilities CAPABILITY_IAM
See Serverless Application Model (SAM) HOWTO Guide for more details in how to get started.
After deployment is complete you can run the following command to retrieve the API Gateway Endpoint URL:
aws cloudformation describe-stacks \
--stack-name sam-app \
--query 'Stacks[].Outputs'
We use Pytest for testing our code and you can install it using pip: pip install pytest
Next, we run pytest
against our tests
folder to run our initial unit tests:
python -m pytest tests/ -v
NOTE: It is recommended to use a Python Virtual environment to separate your application development from your system Python installation.