This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI. It includes the following files and folders.
- src/hello_world - Code for a simple Hello World Lambda function.
- src/view_count - Code for the view counter Lambda function integrated with portfolio-frontend
- events - Invocation events that you can use to invoke the function.
- tests - Unit tests for the application code.
- template.yaml - A template that defines the application's AWS resources.
The application uses several AWS resources, including Lambda functions and an API Gateway API. These resources are defined in the template.yaml file in this project. You can update the template to add AWS resources through the same deployment process that updates your application code.
The Serverless Application Model Command Line Interface (SAM CLI) is an extension of the AWS CLI that adds functionality for building and testing Lambda applications. It uses Docker to run your functions in an Amazon Linux environment that matches Lambda. It can also emulate your application's build environment and API.
To use the SAM CLI, you need the following tools.
- SAM CLI - Install the SAM CLI
- Python 3 installed
- Docker - Install Docker community edition
To build and deploy your application for the first time, run the following in your shell:
sam build --use-container
sam deploy --guidedYou can find your API Gateway Endpoint URL in the output values displayed after deployment.
Build your application with the sam build --use-container command.
portfolio-backend$ sam build --use-containerThe SAM CLI installs dependencies defined in hello_world/requirements.txt, creates a deployment package, and saves it in the .aws-sam/build folder.
Test a single function by invoking it directly with a test event. An event is a JSON document that represents the input that the function receives from the event source. Test events are included in the events folder in this project.
Run functions locally and invoke them with the sam local invoke command.
portfolio-backend$ sam local invoke HelloWorldFunction --event events/event.jsonThe SAM CLI can also emulate your application's API. Use the sam local start-api to run the API locally on port 3000.
portfolio-backend$ sam local start-api
portfolio-backend$ curl http://localhost:3000/Tests are defined in the tests folder in this project. Use PIP to install the test dependencies and run tests.
portfolio-backend$ pip install -r tests/requirements.txt --user
# unit test
portfolio-backend$ python -m pytest tests/unit -v
# integration test, requiring deploying the stack first.
# Create the env variable AWS_SAM_STACK_NAME with the name of the stack we are testing
portfolio-backend$ AWS_SAM_STACK_NAME="portfolio-backend" python -m pytest tests/integration -vTo delete the sample application that you created, use the AWS CLI. Assuming you used your project name for the stack name, you can run the following:
sam delete --stack-name "portfolio-backend"