Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New serverless pattern - lambda-eventbridge_scheduler-cdk #2000

8 changes: 8 additions & 0 deletions cdk-lambda-scheduler-ses/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.js
!jest.config.js
*.d.ts
node_modules

# CDK asset staging directory
.cdk.staging
cdk.out
6 changes: 6 additions & 0 deletions cdk-lambda-scheduler-ses/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.ts
!*.d.ts

# CDK asset staging directory
.cdk.staging
cdk.out
94 changes: 94 additions & 0 deletions cdk-lambda-scheduler-ses/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Using EventBridge Scheduler to send scheduled reminder emails.

This pattern demonstrates how to create an EventBridge scheduler that would send scheduled reminder email and would then be deleted. The pattern uses a Lambda function to create EventBridge scheduler.

Learn more about this pattern at Serverless Land Patterns: << Add the live URL here >>

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
* [AWS Cloud Development Kit](https://docs.aws.amazon.com/cdk/v2/guide/cli.html)


## 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 cdk-lambda-scheduler-ses
```
3. Install dependencies
```
npm install
```
4. From the command line, configure AWS CDK:
```
cdk bootstrap aws://ACCOUNT-NUMBER/REGION

eg: cdk bootstrap aws://123456789012/us-east-1
```

5. Synthesize CloudFormation template from the AWS CDK app:
```
cdk synth
```
6. To deploy your stack, run the following the command line :
```
cdk deploy --all --parameters SenderEmail={source-email-address}
```
Here,

* Replace {source-email-address} with the email address that should be sending reminder email address. The email address should be verified identity from Amazon SES.

## Architecture

![Architecture](images/scheduler-reminder-architecture.png)

## How it works

Let us now dive deeper into the architecture :

1. Scheduler Lambda Invocation:

When the Scheduler Lambda is invoked. It extracts the following details from event:
* Message to be sent.
* Date and time when the message should be sent.
* Email ID to whom the message should be sent.

The lambda function then creates the EventBridge schedule and passes the details extracted as a payload to the scheduler.

2. EventBridge Scheduler Activation:

The EventBridge Scheduler sends email using SES. After the scheduler has sent email, it is deleted automatically.

## Testing

Once the whole setup is deployed using CDK, you can carry out testing by creating a [Test event](https://docs.aws.amazon.com/lambda/latest/dg/testing-functions.html#creating-private-events) for the Lambda function created and invoking the lambda function with the Test event. Expected input for the event:

```
{
"message": "Hello, this is a simple test",
"datetime": "2023-12-22T10:02:00Z",
"email": "{email-address-to-whom-email-should-be-sent}"
}
```
These three fields are required and are case-sensitive. Also note that "{email-address-to-whom-email-should-be-sent}" should be a verified identity from Amazon SES

## Cleanup

1. Delete the stack

```
cdk destroy --all
```

----
Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: MIT-0
21 changes: 21 additions & 0 deletions cdk-lambda-scheduler-ses/bin/cdk-lambda-scheduler-ses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env node
import 'source-map-support/register';
import * as cdk from 'aws-cdk-lib';
import { CdkLambdaSchedulerSesStack } from '../lib/lambda-scheduler-ses-stack';

const app = new cdk.App();
new CdkLambdaSchedulerSesStack(app, 'CdkLambdaSchedulerSesStack', {
/* If you don't specify 'env', this stack will be environment-agnostic.
* Account/Region-dependent features and context lookups will not work,
* but a single synthesized template can be deployed anywhere. */

/* Uncomment the next line to specialize this stack for the AWS Account
* and Region that are implied by the current CLI configuration. */
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },

/* Uncomment the next line if you know exactly what Account and Region you
* want to deploy the stack to. */
// env: { account: '123456789012', region: 'us-east-1' },

/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
});
88 changes: 88 additions & 0 deletions cdk-lambda-scheduler-ses/cdk-lambda-schedular-ses.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{
"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": "CDK",
"introBox": {
"headline": "How it works",
"text": [
"This pattern demonstrates how to create an EventBridge scheduler that would send scheduled reminder email and would then be deleted. The pattern uses a Lambda function to create EventBridge scheduler."
]
},
"gitHub": {
"template": {
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/cdk-lambda-scheduler-ses",
"templateURL": "serverless-patterns/cdk-lambda-scheduler-ses",
"projectFolder": "cdk-lambda-scheduler-ses",
"templateFile": "lib/lambda-scheduler-ses-stack.ts"
}
},
"resources": {
"bullets": [
{
"text": "Getting started with the AWS CDK",
"link": "https://docs.aws.amazon.com/cdk/v2/guide/getting_started.html"
},
{
"text": "Amazon EventBridge Scheduler",
"link": "https://docs.aws.amazon.com/scheduler/latest/UserGuide/what-is-scheduler.html"
},
{
"text": "Verify an email address using Amazon SES",
"link": "https://docs.aws.amazon.com/ses/latest/dg/creating-identities.html"
}
]
},
"deploy": {
"text": [
"cdk deploy"
]
},
"testing": {
"text": [
"See the GitHub repo for detailed testing instructions."
]
},
"cleanup": {
"text": [
"Delete the stack: <code>cdk destroy</code>."
]
},
"authors": [
{
"name": "Anirudh Sharma",
"image": "https://avatars.githubusercontent.com/u/144559992",
"bio": "Cloud Support Engineer 2 @AWS",
"linkedin": "anirudh-sharma-279248142"
}
],
"patternArch": {
"icon1": {
"x": 20,
"y": 50,
"service": "lambda",
"label": "AWS Lambda"
},
"icon2": {
"x": 50,
"y": 50,
"service": "eventbridge-scheduler",
"label": "Amazon EventBridge Scheduler"
},
"icon3": {
"x": 80,
"y": 50,
"service": "ses",
"label": "Amazon SES"
},
"line1": {
"from": "icon1",
"to": "icon2"
},
"line2": {
"from": "icon2",
"to": "icon3"
}
}
}
64 changes: 64 additions & 0 deletions cdk-lambda-scheduler-ses/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"app": "npx ts-node --prefer-ts-exts bin/cdk-lambda-scheduler-ses.ts",
"watch": {
"include": [
"**"
],
"exclude": [
"README.md",
"cdk*.json",
"**/*.d.ts",
"**/*.js",
"tsconfig.json",
"package*.json",
"yarn.lock",
"node_modules",
"test"
]
},
"context": {
"@aws-cdk/aws-lambda:recognizeLayerVersion": true,
"@aws-cdk/core:checkSecretUsage": true,
"@aws-cdk/core:target-partitions": [
"aws",
"aws-cn"
],
"@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true,
"@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true,
"@aws-cdk/aws-ecs:arnFormatIncludesClusterName": true,
"@aws-cdk/aws-iam:minimizePolicies": true,
"@aws-cdk/core:validateSnapshotRemovalPolicy": true,
"@aws-cdk/aws-codepipeline:crossAccountKeyAliasStackSafeResourceName": true,
"@aws-cdk/aws-s3:createDefaultLoggingPolicy": true,
"@aws-cdk/aws-sns-subscriptions:restrictSqsDescryption": true,
"@aws-cdk/aws-apigateway:disableCloudWatchRole": true,
"@aws-cdk/core:enablePartitionLiterals": true,
"@aws-cdk/aws-events:eventsTargetQueueSameAccount": true,
"@aws-cdk/aws-iam:standardizedServicePrincipals": true,
"@aws-cdk/aws-ecs:disableExplicitDeploymentControllerForCircuitBreaker": true,
"@aws-cdk/aws-iam:importedRoleStackSafeDefaultPolicyName": true,
"@aws-cdk/aws-s3:serverAccessLogsUseBucketPolicy": true,
"@aws-cdk/aws-route53-patters:useCertificate": true,
"@aws-cdk/customresources:installLatestAwsSdkDefault": false,
"@aws-cdk/aws-rds:databaseProxyUniqueResourceName": true,
"@aws-cdk/aws-codedeploy:removeAlarmsFromDeploymentGroup": true,
"@aws-cdk/aws-apigateway:authorizerChangeDeploymentLogicalId": true,
"@aws-cdk/aws-ec2:launchTemplateDefaultUserData": true,
"@aws-cdk/aws-secretsmanager:useAttachedSecretResourcePolicyForSecretTargetAttachments": true,
"@aws-cdk/aws-redshift:columnId": true,
"@aws-cdk/aws-stepfunctions-tasks:enableEmrServicePolicyV2": true,
"@aws-cdk/aws-ec2:restrictDefaultSecurityGroup": true,
"@aws-cdk/aws-apigateway:requestValidatorUniqueId": true,
"@aws-cdk/aws-kms:aliasNameRef": true,
"@aws-cdk/aws-autoscaling:generateLaunchTemplateInsteadOfLaunchConfig": true,
"@aws-cdk/core:includePrefixInUniqueNameGeneration": true,
"@aws-cdk/aws-efs:denyAnonymousAccess": true,
"@aws-cdk/aws-opensearchservice:enableOpensearchMultiAzWithStandby": true,
"@aws-cdk/aws-lambda-nodejs:useLatestRuntimeVersion": true,
"@aws-cdk/aws-efs:mountTargetOrderInsensitiveLogicalId": true,
"@aws-cdk/aws-rds:auroraClusterChangeScopeOfInstanceParameterGroupWithEachParameters": true,
"@aws-cdk/aws-appsync:useArnForSourceApiAssociationIdentifier": true,
"@aws-cdk/aws-rds:preventRenderingDeprecatedCredentials": true,
"@aws-cdk/aws-codepipeline-actions:useNewDefaultBranchForCodeCommitSource": true
}
}
60 changes: 60 additions & 0 deletions cdk-lambda-scheduler-ses/example-pattern.json
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": "CDK",
"introBox": {
"headline": "How it works",
"text": [
"This pattern demonstrates how to create an EventBridge scheduler that would send scheduled reminder email and would then be deleted. The pattern uses a Lambda function to create EventBridge scheduler."
]
},
"gitHub": {
"template": {
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/cdk-lambda-scheduler-ses",
"templateURL": "serverless-patterns/cdk-lambda-scheduler-ses",
"projectFolder": "cdk-lambda-scheduler-ses",
"templateFile": "lib/lambda-scheduler-ses-stack.ts"
}
},
"resources": {
"bullets": [
{
"text": "Getting started with the AWS CDK",
"link": "https://docs.aws.amazon.com/cdk/v2/guide/getting_started.html"
},
{
"text": "Amazon EventBridge Scheduler",
"link": "https://docs.aws.amazon.com/scheduler/latest/UserGuide/what-is-scheduler.html"
},
{
"text": "Verify an email address using Amazon SES",
"link": "https://docs.aws.amazon.com/ses/latest/dg/creating-identities.html"
}
]
},
"deploy": {
"text": [
"cdk deploy"
]
},
"testing": {
"text": [
"See the GitHub repo for detailed testing instructions."
]
},
"cleanup": {
"text": [
"Delete the stack: <code>cdk destroy</code>."
]
},
"authors": [
{
"name": "Anirudh Sharma",
"image": "https://avatars.githubusercontent.com/u/144559992",
"bio": "Cloud Support Engineer 2 @AWS",
"linkedin": "anirudh-sharma-279248142"
}
]
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions cdk-lambda-scheduler-ses/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
testEnvironment: 'node',
roots: ['<rootDir>/test'],
testMatch: ['**/*.test.ts'],
transform: {
'^.+\\.tsx?$': 'ts-jest'
}
};
Loading
Loading