Skip to content

Commit

Permalink
Merge pull request #1865 from Damininixcy/main
Browse files Browse the repository at this point in the history
  • Loading branch information
boyney123 authored Nov 14, 2023
2 parents 94714d2 + 3b165fd commit 998e1aa
Show file tree
Hide file tree
Showing 7 changed files with 634 additions and 0 deletions.
108 changes: 108 additions & 0 deletions apigw-dynamodb-lambda-scheduler-ses-auto-deletion-sam/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Serverless Reminders Application using direct integration of Amazon EventBridge Scheduler with Amazon SES

This pattern deploys a serverless reminder application comprising Amazon API Gateway, DynamoDB Streams, Lambda Function, Amazon Event Bridge Scheduler and Amazon SES services.

Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/apigw-dynamodb-lambda-scheduler-ses-auto-deletion-sam

Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.

## Requirements

* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
* [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed

## Deployment Instructions

1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
```
git clone https://github.com/aws-samples/serverless-patterns
```
2. Change directory to the pattern directory:
```
cd apigw-dynamodb-lambda-scheduler-ses-auto-deletion-sam
```
3. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file:
```
sam deploy --guided
```
4. During the prompts:
* Enter a stack name
* Enter the desired AWS Region
* Specify value for SenderEmailAddress. The email address should be verified identity from Amazon SES.
* Allow SAM CLI to create IAM roles with the required permissions.
Once you have run `sam deploy --guided` mode once and saved arguments to a configuration file (samconfig.toml), you can use `sam deploy` in future to use these defaults.
5. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing.
## Architecture
![Architecture](./images/reminders-application-flow.png)
## How it works
- When a user creates/updates/deletes a reminder using REST API Gateway endpoint, the data is updated in DynamoDB Table.
- Scheduler Lambda function is triggered in response to event in DynamoDB table and then creates/updates/deletes one-time schedules using [Amazon EventBridge Scheduler](https://aws.amazon.com/blogs/compute/introducing-amazon-eventbridge-scheduler/).
- Email reminders are sent to the specified email address before 10 minutes and at the scheduled time.
- Amazon EventBridge Scheduler is directly integrated with Amazon SES using [Universal Targets](https://docs.aws.amazon.com/scheduler/latest/UserGuide/managing-targets-universal.html).
- All schedules are removed once they run (auto deletion).
## Testing
Once the application is deployed, use Postman to test the API using the following instructions.
1) Copy the API Gateway Stage Invoke URL from the SAM deployment output
2) In order to create a new reminder, specify the below values.
- HTTP Method: POST
- Endpoint: https://{ApiId}.execute-api.{Region}.amazonaws.com/v1/reminders
- Request Body (JSON)
```
{
"description": "Order Food",
"datetime": "2023-11-10T05:40:00Z",
"email": "[email protected]"
}
```
- Kindly note that all the three parameters are required and datetime parameter is of the format **YYYY-MM-DDTHH:MM:SSZ** in UTC. Email must be valid.
- The API response includes the unique id of the reminder and other attributes.
- One-time Event Bridge Schedules are created and auto-deleted upon event completion.
3) In order to update a reminder, specify the below values.
- HTTP Method: PUT
- Endpoint: https://{ApiId}.execute-api.{Region}.amazonaws.com/v1/reminders/{reminderid}
- Request Body (JSON)
```
{
"description": "Order Food",
"datetime": "2023-11-10T05:40:00Z",
"email": "[email protected]"
}
```
- Kindly note that all the three parameters are required and datetime parameter is of the format YYYY-MM-DDTHH:MM:SSZ in UTC.
- Existing One-time Event Bridge Schedules are updated and auto-deleted upon event completion.
4) In order to delete a specific reminder, specify the below values.
- HTTP Method: DELETE
- Endpoint: https://{ApiId}.execute-api.{Region}.amazonaws.com/v1/reminders/{reminderid}
- The records are deleted from DynamoDB and the corresponding One-time Event Bridge Schedules are deleted.
5) In order to list all reminders, specify the below values.
- HTTP Method: GET
- Endpoint: https://{ApiId}.execute-api.{Region}.amazonaws.com/v1/reminders
- The API response lists the active and expired reminders.
## Cleanup
1. Delete the stack
```bash
sam delete
```
----
Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"title": "Amazon EventBridge Scheduler with Amazon SES",
"description": "A serverless event-driven pattern that creates Amazon EventBridge schedules dynamically to send email reminders using Amazon SES.",
"language": "Python",
"level": "200",
"framework": "SAM",
"introBox": {
"headline": "How it works",
"text": [
"This pattern deploys a serverless reminder application. When a user creates/updates/deletes a reminder using API Gateway, the data is updated in a DynamoDB Table, which triggers a Lambda Function. This creates a one-time EventBridge schedule with Amazon SES as the target. "
]
},
"gitHub": {
"template": {
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/apigw-dynamodb-lambda-scheduler-ses-auto-deletion-sam",
"templateURL": "serverless-patterns/apigw-dynamodb-lambda-scheduler-ses-auto-deletion-sam",
"projectFolder": "apigw-dynamodb-lambda-scheduler-ses-auto-deletion-sam",
"templateFile": "template.yaml"
}
},
"resources": {
"bullets": [
{
"text": "EventBridge Schedulers",
"link": "https://aws.amazon.com/blogs/compute/introducing-amazon-eventbridge-scheduler/"
},
{
"text": "Using Amazon API Gateway as a proxy for DynamoDB",
"link": "https://aws.amazon.com/blogs/compute/using-amazon-api-gateway-as-a-proxy-for-dynamodb/"
},
{
"text": "EventBridge Schedulers Universal Targets",
"link": "https://docs.aws.amazon.com/scheduler/latest/UserGuide/managing-targets-universal.html"
}
]
},
"deploy": {
"text": [
"sam deploy --guided"
]
},
"testing": {
"text": [
"See the GitHub repo for detailed testing instructions."
]
},
"cleanup": {
"text": [
"Delete the stack: <code>sam delete</code>."
]
},
"authors": [
{
"name": "Damini Nixcy Mol Antony",
"image": "https://avatars.githubusercontent.com/u/39347789?v=4",
"bio": "Senior Cloud Support Engineer (Serverless) @AWS",
"linkedin": "damini-nixcy-mol"
}
]
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
220 changes: 220 additions & 0 deletions apigw-dynamodb-lambda-scheduler-ses-auto-deletion-sam/restapi/api.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
openapi: "3.0.1"
info:
title: "reminders-api"
version: "2023-11-09T13:43:51Z"
paths:
/reminders/{id}:
get:
parameters:
- name: "id"
in: "path"
required: true
schema:
type: "string"
responses:
"200":
description: "200 response"
content:
application/json:
schema:
$ref: "#/components/schemas/Empty"
x-amazon-apigateway-integration:
credentials:
Fn::GetAtt: [APIGatewayDynamoDBRole, Arn]
httpMethod: "POST"
uri: {"Fn::Sub": "arn:aws:apigateway:${AWS::Region}:dynamodb:action/GetItem"}
responses:
default:
statusCode: "200"
responseTemplates:
application/json: "#set($inputRoot = $input.path('$')) \n{\n \"id\"\
: \"$inputRoot.Item.id.S\",\n \"description\": \"$inputRoot.Item.description.S\"\
,\n \"datetime\": \"$inputRoot.Item.datetime.S\",\n \"email\"\
: \"$inputRoot.Item.email.S\"\n}"
requestTemplates:
application/json: {"Fn::Sub": "{\n \"TableName\": \"${RemindersTable}\",\n \"Key\": {\n\
\ \"id\": {\n \"S\": \"$input.params().path.id\"\n }\n\
\ }\n}"}
passthroughBehavior: "when_no_match"
type: "aws"
put:
parameters:
- name: "id"
in: "path"
required: true
schema:
type: "string"
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/reminder"
required: true
responses:
"200":
description: "200 response"
content:
application/json:
schema:
$ref: "#/components/schemas/Empty"
x-amazon-apigateway-request-validator: "Validate body"
x-amazon-apigateway-integration:
credentials:
Fn::GetAtt: [APIGatewayDynamoDBRole, Arn]
httpMethod: "POST"
uri: {"Fn::Sub": "arn:aws:apigateway:${AWS::Region}:dynamodb:action/UpdateItem"}
responses:
default:
statusCode: "200"
responseTemplates:
application/json: "#set($inputRoot = $input.path('$')) \n{\n\"message\"\
: \"Reminder updated successfully\",\n\"data\": {\n \"id\": \"\
$inputRoot.Attributes.id.S\",\n \"description\": \"$inputRoot.Attributes.description.S\"\
,\n \"datetime\": \"$inputRoot.Attributes.datetime.S\"\n }\n\
}"
requestTemplates:
application/json: {"Fn::Sub": "{ \n \"TableName\": \"${RemindersTable}\",\n \"Key\": {\n\
\ \"id\": {\n \"S\": \"$input.params().path.id\"\n \
\ }\n },\n \"UpdateExpression\": \"set description = :description,\
\ #date = :date, #email= :email\",\n \"ExpressionAttributeValues\"\
: {\n \":description\": {\"S\": \"$input.path(\"$.description\"\
)\"},\n \":date\": {\"S\": \"$input.path(\"$.datetime\")\"},\n\
\ \":email\": {\"S\": \"$input.path(\"$.email\")\"}\n },\n\
\ \"ExpressionAttributeNames\": {\n \"#date\": \"datetime\",\n\
\ \"#email\": \"email\"\n },\n \"ReturnValues\": \"ALL_NEW\"\
\n}"}
passthroughBehavior: "when_no_match"
type: "aws"
delete:
parameters:
- name: "id"
in: "path"
required: true
schema:
type: "string"
responses:
"200":
description: "200 response"
content:
application/json:
schema:
$ref: "#/components/schemas/Empty"
x-amazon-apigateway-integration:
credentials:
Fn::GetAtt: [APIGatewayDynamoDBRole, Arn]
httpMethod: "POST"
uri: {"Fn::Sub": "arn:aws:apigateway:${AWS::Region}:dynamodb:action/DeleteItem"}
responses:
default:
statusCode: "200"
responseTemplates:
application/json: "#set($inputRoot = $input.path('$')) \n{\n\"message\"\
: \"Reminder deleted successfully\",\n\"data\": {\n \"id\": \"\
$inputRoot.Attributes.id.S\",\n \"description\": \"$inputRoot.Attributes.description.S\"\
,\n \"datetime\": \"$inputRoot.Attributes.datetime.S\",\n \"\
email\": \"$inputRoot.Attributes.email.S\"\n }\n}"
requestTemplates:
application/json: {"Fn::Sub": "{ \n \"TableName\": \"${RemindersTable}\",\n \"Key\":{\n\
\ \"id\":{\n \"S\": \"$input.params().path.id\"\n \
\ }\n },\n \"ReturnValues\": \"ALL_OLD\"\n}"}
passthroughBehavior: "when_no_match"
type: "aws"
/reminders:
get:
responses:
"200":
description: "200 response"
content:
application/json:
schema:
$ref: "#/components/schemas/Empty"
x-amazon-apigateway-integration:
credentials:
Fn::GetAtt: [APIGatewayDynamoDBRole, Arn]
httpMethod: "POST"
uri: {"Fn::Sub": "arn:aws:apigateway:${AWS::Region}:dynamodb:action/Scan"}
responses:
default:
statusCode: "200"
responseTemplates:
application/json: "#set($inputRoot = $input.path('$'))\n[\n#foreach($elem\
\ in $inputRoot.Items) \n {\n \"id\": \"$elem.id.S\",\n \"\
description\": \"$elem.description.S\",\n \"datetime\": \"$elem.datetime.S\"\
,\n \"email\": \"$elem.email.S\"\n }#if($foreach.hasNext),#end\n\
\ \n#end\n]"
requestTemplates:
application/json: {"Fn::Sub": "{ \n \"TableName\": \"${RemindersTable}\"\n}"}
passthroughBehavior: "when_no_match"
type: "aws"
post:
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/reminder"
required: true
responses:
"200":
description: "200 response"
content:
application/json:
schema:
$ref: "#/components/schemas/Empty"
x-amazon-apigateway-request-validator: "Validate body"
x-amazon-apigateway-integration:
credentials:
Fn::GetAtt: [APIGatewayDynamoDBRole, Arn]
httpMethod: "POST"
uri: {"Fn::Sub": "arn:aws:apigateway:${AWS::Region}:dynamodb:action/UpdateItem"}
responses:
default:
statusCode: "200"
responseTemplates:
application/json: "#set($inputRoot = $input.path('$')) \n{\n\"message\"\
: \"Reminder created successfully\",\n\"data\": {\n \"id\": \"\
$inputRoot.Attributes.id.S\",\n \"description\": \"$inputRoot.Attributes.description.S\"\
,\n \"datetime\": \"$inputRoot.Attributes.datetime.S\",\n \"\
email\": \"$inputRoot.Attributes.email.S\"\n }\n}"
requestTemplates:
application/json: {"Fn::Sub": "{ \n \"TableName\": \"${RemindersTable}\",\n \"Key\": {\n\
\ \"id\": {\n \"S\": \"$context.requestId\"\n \
\ }\n },\n \"UpdateExpression\": \"set description = :description,\
\ #date = :date, #email = :email\",\n \"ExpressionAttributeValues\"\
: {\n \":description\": {\"S\": \"$input.path(\"$.description\"\
)\"},\n \":date\": {\"S\": \"$input.path(\"$.datetime\")\"},\n\
\ \":email\": {\"S\": \"$input.path(\"$.email\")\"}\n },\n\
\ \"ExpressionAttributeNames\": {\n \"#date\": \"datetime\",\n\
\ \"#email\": \"email\"\n },\n \"ReturnValues\": \"ALL_NEW\"\
\n}"}
passthroughBehavior: "when_no_match"
type: "aws"
components:
schemas:
Empty:
title: "Empty Schema"
type: "object"
reminder:
required:
- "datetime"
- "description"
- "email"
type: "object"
properties:
description:
type: "string"
datetime:
type: "string"
format: "date-time"
email:
type: "string"
format: "email"
x-amazon-apigateway-gateway-responses:
BAD_REQUEST_BODY:
statusCode: 400
responseTemplates:
application/json: "{\n\"Invalid Request Body\": \"$context.error.validationErrorString\"\
\n}"
x-amazon-apigateway-request-validators:
Validate body:
validateRequestParameters: false
validateRequestBody: true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
boto3==1.28.83
Loading

0 comments on commit 998e1aa

Please sign in to comment.