This is an example, how to implement an HTTP Feed Server Endpoint Serverless with AWS Lambda and AWS DynamoDB.
This project contains source code and supporting files for a serverless application that you can deploy with the AWS Serverless Application Model (AWS SAM) command line interface (CLI). It includes the following files and folders:
src
- Code for the application's Lambda function.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, an API Gateway API, and Amazon DynamoDB tables. 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.
To build and deploy your application for the first time, run the following in your shell:
sam build
sam deploy --guided
The API Gateway endpoint API will be displayed in the outputs when the deployment is complete.
The application template uses AWS SAM to define application resources. AWS SAM is an extension of AWS CloudFormation with a simpler syntax for configuring common serverless application resources, such as functions, triggers, and APIs. For resources that aren't included in the AWS SAM specification, you can use the standard AWS CloudFormation resource types.
Update template.yaml
to add a dead-letter queue to your application. In the Resources section, add a resource named MyQueue with the type AWS::SQS::Queue. Then add a property to the AWS::Serverless::Function resource named DeadLetterQueue that targets the queue's Amazon Resource Name (ARN), and a policy that grants the function permission to access the queue.
Resources:
InventoryQueue:
Type: AWS::SQS::Queue
appendItemFunction:
Type: AWS::Serverless::Function
Properties:
Handler: src/handlers/append-item.appendItemHandler
Runtime: nodejs14.x
DeadLetterQueue:
Type: SQS
TargetArn: !GetAtt InventoryQueue.Arn
Policies:
- SQSSendMessagePolicy:
QueueName: !GetAtt InventoryQueue.QueueName
The dead-letter queue is a location for Lambda to send events that could not be processed. It's only used if you invoke your function asynchronously, but it's useful here to show how you can modify your application's resources and function configuration.
Deploy the updated application.
my-application$ sam deploy
Open the Applications page of the Lambda console, and choose your application. When the deployment completes, view the application resources on the Overview tab to see the new resource. Then, choose the function to see the updated configuration that specifies the dead-letter queue.
To simplify troubleshooting, the AWS SAM CLI has a command called sam logs
. sam logs
lets you fetch logs that are 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 Lambda functions, not just the ones you deploy using AWS SAM.
my-application$ sam logs -n getFeedItemsFunction --stack-name http-feeds-server --tail
NOTE: This uses the logical name of the function within the stack. This is the correct name to use when searching logs inside an AWS Lambda function within a CloudFormation stack, even if the deployed function name varies due to CloudFormation's unique resource name generation.
You can find more information and examples about filtering Lambda function logs in the AWS SAM CLI documentation.
Tests are defined in the __tests__
folder in this project. Use npm
to install the Jest test framework and run unit tests.
my-application$ npm install
my-application$ npm run test
To 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:
aws cloudformation delete-stack --stack-name http-feeds-server