Skip to content

Commit 9ea6c5d

Browse files
authored
Merge pull request #2800 from SarathKumarKS/SarathKumarKS-feature-lambda-endusermessaging
New serverless pattern - lambda-endusermessaging
2 parents 939c58b + 9b6afec commit 9ea6c5d

File tree

12 files changed

+595
-0
lines changed

12 files changed

+595
-0
lines changed

lambda-endusermessaging/README.md

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
# Sending SMS with AWS Lambda and AWS End User Messaging
2+
3+
This sample pattern demonstrates how to send scheduled SMS messages using AWS Lambda and AWS End User Messaging. The solution uses Amazon EventBridge to trigger a Lambda function at a specified time each day, which then sends an SMS message to a configured phone number.
4+
5+
Built with a serverless-first approach, this solution automatically sends messages at your preferred time each day.
6+
7+
**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.
8+
9+
### Getting Started
10+
11+
The entire solution is built using AWS Cloud Development Kit (CDK) and AWS Lambda functions running on Python 3.11 runtime. This serverless architecture ensures minimal operational overhead while providing maximum flexibility for customization.
12+
13+
### Requirements
14+
15+
* [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.
16+
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
17+
* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
18+
* [AWS Cloud Development Kit](https://docs.aws.amazon.com/cdk/v2/guide/getting_started.html) installed
19+
* [Node.js](https://nodejs.org/en) and [npm](https://www.npmjs.com/) installed (required for CDK)
20+
* A destination phone number to receive SMS messages
21+
* An originating number based on your [country's requirements](https://docs.aws.amazon.com/sms-voice/latest/userguide/phone-numbers-sms-by-country.html)
22+
23+
### AWS Services Used
24+
25+
* AWS Lambda
26+
* AWS End User Messaging SMS
27+
* AWS Secrets Manager
28+
* Amazon EventBridge
29+
30+
### Architecture Diagram
31+
32+
![Lambda End User Messaging Architecture](images/lambda_endusermessaging.png)
33+
34+
### How it works
35+
36+
* EventBridge triggers the Lambda function daily at your specified time
37+
* Lambda function performs the following actions:
38+
* Retrieves phone numbers from AWS Secrets Manager
39+
* Uses End User Messaging to send SMS with a predefined message
40+
41+
### Deployment Instructions
42+
43+
1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
44+
```
45+
git clone https://github.com/aws-samples/serverless-patterns/
46+
```
47+
2. Change directory to the pattern directory:
48+
```
49+
cd lambda-endusermessaging
50+
```
51+
52+
3. Create [AWS Secrets Manager Secret](https://docs.aws.amazon.com/secretsmanager/latest/userguide/create_secret.html) Secrets for your destination phone number and origination phone number.
53+
54+
Log in to your AWS account and navigate to the AWS Secrets Manager service in the AWS Console in your preferred region. Then select the Store a new secret button.
55+
56+
For the secret type, select Other type of secret. Under Key/value pair, select the Plaintext tab and enter the phone number. For the encryption key, you can either encrypt using the AWS KMS key that AWS Secrets Manager creates or use a customer-managed AWS KMS key that you create. Select Next, provide the secret name as DestinationPhone, select Next, and click the Store button to create the secret. Note the secret ARN, as this will be needed for the next step.
57+
58+
Repeat this process for your origination phone number, creating a separate secret named OriginationPhone.
59+
60+
If you prefer the CLI option, use the commands below. Replace the placeholder values with your actual destination and origination phone numbers, along with your preferred region.
61+
62+
```
63+
aws secretsmanager create-secret \
64+
--name "OriginationPhone" \
65+
--description "Origination phone number for SMS sending" \
66+
--secret-string "+12065550100" \
67+
--region us-east-1
68+
69+
aws secretsmanager create-secret \
70+
--name "DestinationPhone" \
71+
--description "Destination phone number for SMS messages" \
72+
--secret-string "+12065550123" \
73+
--region us-east-1
74+
```
75+
76+
4. Edit the constants.py file under the folder simple_sms_messaging and customize the following parameters according to your preferences:
77+
1. DESTINATION_PHONE_SECRET_ARN: The ARN of the secret containing the phone number where you want to receive SMS messages
78+
2. ORIGINATION_PHONE_SECRET_ARN: The ARN of the secret containing your verified sender ID for SMS messages
79+
3. SCHEDULE_HOUR and SCHEDULE_MINUTE: Your desired delivery time (in UTC)
80+
4. MESSAGE: The message you want to send
81+
82+
Example below
83+
84+
```
85+
DESTINATION_PHONE_SECRET_ARN = "arn:aws:secretsmanager:us-west-2:123456789012:secret:destination-phone-Abc123"
86+
ORIGINATION_PHONE_SECRET_ARN = "arn:aws:secretsmanager:us-west-2:123456789012:secret:origination-phone-Def456"
87+
SCHEDULE_HOUR = 15
88+
SCHEDULE_MINUTE = 0
89+
MESSAGE = "Hello! This is your daily message from AWS End User Messaging."
90+
```
91+
92+
5. Create a virtualenv on MacOS and Linux
93+
```
94+
python3 -m venv .venv
95+
```
96+
6. After the init process completes and the virtualenv is created, you can use the following step to activate your virtualenv
97+
```
98+
source .venv/bin/activate
99+
```
100+
[Optional - for Windows] If you are a Windows platform, you would activate the virtualenv with below command
101+
102+
```
103+
.venv\Scripts\activate.bat
104+
```
105+
7. Once the virtualenv is activated, you can install the required dependencies
106+
```
107+
pip install -r requirements.txt
108+
```
109+
8. Synthesize the CloudFormation template for this code
110+
```
111+
cdk synth
112+
```
113+
9. Deploy the stack
114+
115+
```
116+
cdk deploy
117+
```
118+
### Testing Your Deployment
119+
120+
* For quick verification of your deployment using only the AWS CLI, invoke the SMS sender function to trigger an immediate message:
121+
122+
```
123+
aws lambda invoke \
124+
--function-name $(aws cloudformation describe-stacks --stack-name SimpleSmSMessagingStack --query "Stacks[0].Outputs[?OutputKey=='SmsSenderFunctionName'].OutputValue" --output text) \
125+
--payload '{}' \
126+
--cli-binary-format raw-in-base64-out \
127+
response.json
128+
129+
```
130+
131+
* Check the execution status for detailed logs and any error messages. You should also have received an SMS on the destination phone number.
132+
133+
```
134+
cat response.json
135+
```
136+
137+
### Cleanup
138+
139+
1. To cleanup/delete resources created while deploying the solution, go to the root folder of the project repository and run
140+
```
141+
cdk destroy
142+
```
143+
144+
### Useful commands
145+
146+
* `cdk ls` list all stacks in the app
147+
* `cdk synth` emits the synthesized CloudFormation template
148+
* `cdk deploy` deploy this stack to your default AWS account/region
149+
* `cdk diff` compare deployed stack with current state
150+
* `cdk docs` open CDK documentation
151+
152+
------
153+
Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.
154+
155+
SPDX-License-Identifier: MIT-0

lambda-endusermessaging/app.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env python3
2+
import os
3+
import aws_cdk as cdk
4+
from simple_sms_messaging.simple_sms_messaging_stack import SimpleSmSMessagingStack
5+
6+
app = cdk.App()
7+
8+
SimpleSmSMessagingStack(
9+
app,
10+
"SimpleSmSMessagingStack",
11+
env=cdk.Environment(
12+
account=os.getenv('CDK_DEFAULT_ACCOUNT'),
13+
region=os.getenv('CDK_DEFAULT_REGION')
14+
)
15+
)
16+
17+
app.synth()

lambda-endusermessaging/cdk.json

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
{
2+
"app": "python3 app.py",
3+
"watch": {
4+
"include": [
5+
"**"
6+
],
7+
"exclude": [
8+
"README.md",
9+
"cdk*.json",
10+
"requirements*.txt",
11+
"source.bat",
12+
"**/__init__.py",
13+
"**/__pycache__",
14+
"tests"
15+
]
16+
},
17+
"context": {
18+
"@aws-cdk/aws-lambda:recognizeLayerVersion": true,
19+
"@aws-cdk/core:checkSecretUsage": true,
20+
"@aws-cdk/core:target-partitions": [
21+
"aws",
22+
"aws-cn"
23+
],
24+
"@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true,
25+
"@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true,
26+
"@aws-cdk/aws-ecs:arnFormatIncludesClusterName": true,
27+
"@aws-cdk/aws-iam:minimizePolicies": true,
28+
"@aws-cdk/core:validateSnapshotRemovalPolicy": true,
29+
"@aws-cdk/aws-codepipeline:crossAccountKeyAliasStackSafeResourceName": true,
30+
"@aws-cdk/aws-s3:createDefaultLoggingPolicy": true,
31+
"@aws-cdk/aws-sns-subscriptions:restrictSqsDescryption": true,
32+
"@aws-cdk/aws-apigateway:disableCloudWatchRole": true,
33+
"@aws-cdk/core:enablePartitionLiterals": true,
34+
"@aws-cdk/aws-events:eventsTargetQueueSameAccount": true,
35+
"@aws-cdk/aws-ecs:disableExplicitDeploymentControllerForCircuitBreaker": true,
36+
"@aws-cdk/aws-iam:importedRoleStackSafeDefaultPolicyName": true,
37+
"@aws-cdk/aws-s3:serverAccessLogsUseBucketPolicy": true,
38+
"@aws-cdk/aws-route53-patters:useCertificate": true,
39+
"@aws-cdk/customresources:installLatestAwsSdkDefault": false,
40+
"@aws-cdk/aws-rds:databaseProxyUniqueResourceName": true,
41+
"@aws-cdk/aws-codedeploy:removeAlarmsFromDeploymentGroup": true,
42+
"@aws-cdk/aws-apigateway:authorizerChangeDeploymentLogicalId": true,
43+
"@aws-cdk/aws-ec2:launchTemplateDefaultUserData": true,
44+
"@aws-cdk/aws-secretsmanager:useAttachedSecretResourcePolicyForSecretTargetAttachments": true,
45+
"@aws-cdk/aws-redshift:columnId": true,
46+
"@aws-cdk/aws-stepfunctions-tasks:enableEmrServicePolicyV2": true,
47+
"@aws-cdk/aws-ec2:restrictDefaultSecurityGroup": true,
48+
"@aws-cdk/aws-apigateway:requestValidatorUniqueId": true,
49+
"@aws-cdk/aws-kms:aliasNameRef": true,
50+
"@aws-cdk/aws-autoscaling:generateLaunchTemplateInsteadOfLaunchConfig": true,
51+
"@aws-cdk/core:includePrefixInUniqueNameGeneration": true,
52+
"@aws-cdk/aws-efs:denyAnonymousAccess": true,
53+
"@aws-cdk/aws-opensearchservice:enableOpensearchMultiAzWithStandby": true,
54+
"@aws-cdk/aws-lambda-nodejs:useLatestRuntimeVersion": true,
55+
"@aws-cdk/aws-efs:mountTargetOrderInsensitiveLogicalId": true,
56+
"@aws-cdk/aws-rds:auroraClusterChangeScopeOfInstanceParameterGroupWithEachParameters": true,
57+
"@aws-cdk/aws-appsync:useArnForSourceApiAssociationIdentifier": true,
58+
"@aws-cdk/aws-rds:preventRenderingDeprecatedCredentials": true,
59+
"@aws-cdk/aws-codepipeline-actions:useNewDefaultBranchForCodeCommitSource": true,
60+
"@aws-cdk/aws-cloudwatch-actions:changeLambdaPermissionLogicalIdForLambdaAction": true,
61+
"@aws-cdk/aws-codepipeline:crossAccountKeysDefaultValueToFalse": true,
62+
"@aws-cdk/aws-codepipeline:defaultPipelineTypeToV2": true,
63+
"@aws-cdk/aws-kms:reduceCrossAccountRegionPolicyScope": true,
64+
"@aws-cdk/aws-eks:nodegroupNameAttribute": true,
65+
"@aws-cdk/aws-ec2:ebsDefaultGp3Volume": true,
66+
"@aws-cdk/aws-ecs:removeDefaultDeploymentAlarm": true,
67+
"@aws-cdk/custom-resources:logApiResponseDataPropertyTrueDefault": false,
68+
"@aws-cdk/aws-s3:keepNotificationInImportedBucket": false,
69+
"@aws-cdk/aws-ecs:enableImdsBlockingDeprecatedFeature": false,
70+
"@aws-cdk/aws-ecs:disableEcsImdsBlocking": true,
71+
"@aws-cdk/aws-ecs:reduceEc2FargateCloudWatchPermissions": true,
72+
"@aws-cdk/aws-dynamodb:resourcePolicyPerReplica": true,
73+
"@aws-cdk/aws-ec2:ec2SumTImeoutEnabled": true,
74+
"@aws-cdk/aws-appsync:appSyncGraphQLAPIScopeLambdaPermission": true,
75+
"@aws-cdk/aws-rds:setCorrectValueForDatabaseInstanceReadReplicaInstanceResourceId": true,
76+
"@aws-cdk/core:cfnIncludeRejectComplexResourceUpdateCreatePolicyIntrinsics": true,
77+
"@aws-cdk/aws-lambda-nodejs:sdkV3ExcludeSmithyPackages": true,
78+
"@aws-cdk/aws-stepfunctions-tasks:fixRunEcsTaskPolicy": true,
79+
"@aws-cdk/aws-ec2:bastionHostUseAmazonLinux2023ByDefault": true,
80+
"@aws-cdk/aws-route53-targets:userPoolDomainNameMethodWithoutCustomResource": true,
81+
"@aws-cdk/aws-elasticloadbalancingV2:albDualstackWithoutPublicIpv4SecurityGroupRulesDefault": true,
82+
"@aws-cdk/aws-iam:oidcRejectUnauthorizedConnections": true,
83+
"@aws-cdk/core:enableAdditionalMetadataCollection": true,
84+
"@aws-cdk/aws-lambda:createNewPoliciesWithAddToRolePolicy": false,
85+
"@aws-cdk/aws-s3:setUniqueReplicationRoleName": true,
86+
"@aws-cdk/aws-events:requireEventBusPolicySid": true,
87+
"@aws-cdk/core:aspectPrioritiesMutating": true,
88+
"@aws-cdk/aws-dynamodb:retainTableReplica": true,
89+
"@aws-cdk/aws-stepfunctions:useDistributedMapResultWriterV2": true,
90+
"@aws-cdk/s3-notifications:addS3TrustKeyPolicyForSnsSubscriptions": true,
91+
"@aws-cdk/aws-ec2:requirePrivateSubnetsForEgressOnlyInternetGateway": true,
92+
"@aws-cdk/aws-s3:publicAccessBlockedByDefault": true
93+
}
94+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"title": "Sending SMS with AWS Lambda and AWS End User Messaging",
3+
"description": "Scheduled SMS messaging using AWS Lambda and AWS End User Messaging",
4+
"language": "Python",
5+
"level": "300",
6+
"framework": "AWS CDK",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"This pattern demonstrates how to send scheduled SMS messages using AWS Lambda and AWS End User Messaging with a serverless architecture."
11+
]
12+
},
13+
"gitHub": {
14+
"template": {
15+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/lambda-endusermessaging",
16+
"templateURL": "serverless-patterns/lambda-endusermessaging",
17+
"projectFolder": "lambda-endusermessaging",
18+
"templateFile": "simple_sms_messaging/simple_sms_messaging_stack.py"
19+
}
20+
},
21+
"resources": {
22+
"bullets": [
23+
{
24+
"text": "AWS Lambda",
25+
"link": "https://aws.amazon.com/lambda/"
26+
},
27+
{
28+
"text": "AWS End User Messaging",
29+
"link": "https://aws.amazon.com/end-user-messaging/"
30+
},
31+
{
32+
"text": "Amazon EventBridge",
33+
"link": "https://aws.amazon.com/eventbridge/"
34+
}
35+
]
36+
},
37+
"deploy": {
38+
"text": [
39+
"Deploy the stack:<code>cdk deploy</code>."
40+
]
41+
},
42+
"testing": {
43+
"text": [
44+
"See the GitHub repo for detailed testing instructions."
45+
]
46+
},
47+
"cleanup": {
48+
"text": [
49+
"Delete the stack: <code>cdk delete</code>."
50+
]
51+
},
52+
"authors": [
53+
{
54+
"name": "Sarath Kumar K.S",
55+
"image": "https://i.postimg.cc/6qpwHbWD/IMG-0258.jpg",
56+
"bio": "Sarath is a Senior Technical Account Manager at AWS.",
57+
"linkedin": "kssarathkumar"
58+
}
59+
]
60+
}
27 KB
Loading

0 commit comments

Comments
 (0)