Create and configure a Lambda function for a Amazon Pinpoint campaign - Amazon Pinpoint

End of support notice: On October 30, 2026, AWS will end support for Amazon Pinpoint. After October 30, 2026, you will no longer be able to access the Amazon Pinpoint console or Amazon Pinpoint resources (endpoints, segments, campaigns, journeys, and analytics). For more information, see Amazon Pinpoint end of support. Note: APIs related to SMS, voice, mobile push, OTP, and phone number validate are not impacted by this change and are supported by AWS End User Messaging.

Create and configure a Lambda function for a Amazon Pinpoint campaign

This section provides an overview of the steps to create a Lambda function that sends messages over a custom channel. First, you create the function. Then, you add an execution policy to the function. This policy allows Amazon Pinpoint to execute the policy when a campaign runs.

For an introduction to creating Lambda functions, see Building Lambda functions in the AWS Lambda Developer Guide.

Example Lambda function

The following code example processes the payload and logs the number of endpoints of each endpoint type in CloudWatch.

import boto3 import random import pprint import json import time cloudwatch = boto3.client('cloudwatch') def lambda_handler(event, context): customEndpoints = 0 smsEndpoints = 0 pushEndpoints = 0 emailEndpoints = 0 voiceEndpoints = 0 numEndpoints = len(event['Endpoints']) print("Payload:\n", event) print("Endpoints in payload: " + str(numEndpoints)) for key in event['Endpoints'].keys(): if event['Endpoints'][key]['ChannelType'] == "CUSTOM": customEndpoints += 1 elif event['Endpoints'][key]['ChannelType'] == "SMS": smsEndpoints += 1 elif event['Endpoints'][key]['ChannelType'] == "EMAIL": emailEndpoints += 1 elif event['Endpoints'][key]['ChannelType'] == "VOICE": voiceEndpoints += 1 else: pushEndpoints += 1 response = cloudwatch.put_metric_data( MetricData = [ { 'MetricName': 'EndpointCount', 'Dimensions': [ { 'Name': 'CampaignId', 'Value': event['CampaignId'] }, { 'Name': 'ApplicationId', 'Value': event['ApplicationId'] } ], 'Unit': 'None', 'Value': len(event['Endpoints']) }, { 'MetricName': 'CustomCount', 'Dimensions': [ { 'Name': 'CampaignId', 'Value': event['CampaignId'] }, { 'Name': 'ApplicationId', 'Value': event['ApplicationId'] } ], 'Unit': 'None', 'Value': customEndpoints }, { 'MetricName': 'SMSCount', 'Dimensions': [ { 'Name': 'CampaignId', 'Value': event['CampaignId'] }, { 'Name': 'ApplicationId', 'Value': event['ApplicationId'] } ], 'Unit': 'None', 'Value': smsEndpoints }, { 'MetricName': 'EmailCount', 'Dimensions': [ { 'Name': 'CampaignId', 'Value': event['CampaignId'] }, { 'Name': 'ApplicationId', 'Value': event['ApplicationId'] } ], 'Unit': 'None', 'Value': emailEndpoints }, { 'MetricName': 'VoiceCount', 'Dimensions': [ { 'Name': 'CampaignId', 'Value': event['CampaignId'] }, { 'Name': 'ApplicationId', 'Value': event['ApplicationId'] } ], 'Unit': 'None', 'Value': voiceEndpoints }, { 'MetricName': 'PushCount', 'Dimensions': [ { 'Name': 'CampaignId', 'Value': event['CampaignId'] }, { 'Name': 'ApplicationId', 'Value': event['ApplicationId'] } ], 'Unit': 'None', 'Value': pushEndpoints }, { 'MetricName': 'EndpointCount', 'Dimensions': [ ], 'Unit': 'None', 'Value': len(event['Endpoints']) }, { 'MetricName': 'CustomCount', 'Dimensions': [ ], 'Unit': 'None', 'Value': customEndpoints }, { 'MetricName': 'SMSCount', 'Dimensions': [ ], 'Unit': 'None', 'Value': smsEndpoints }, { 'MetricName': 'EmailCount', 'Dimensions': [ ], 'Unit': 'None', 'Value': emailEndpoints }, { 'MetricName': 'VoiceCount', 'Dimensions': [ ], 'Unit': 'None', 'Value': voiceEndpoints }, { 'MetricName': 'PushCount', 'Dimensions': [ ], 'Unit': 'None', 'Value': pushEndpoints } ], Namespace = 'PinpointCustomChannelExecution' ) print("cloudwatchResponse:\n",response)

When an Amazon Pinpoint campaign executes this Lambda function, Amazon Pinpoint sends the function a list of segment members. The function counts the number of endpoints of each ChannelType. It then sends that data to Amazon CloudWatch. You can view these metrics in the Metrics section of the CloudWatch console. The metrics are available in the PinpointCustomChannelExecution namespace.

You can modify this code example so that it also connects to the API of an external service in order to send messages through that service.