Search code examples
pythonaws-lambdaaws-pinpoint

How to integrate AWS Lambda and AWS PinPoint?


I want to send an email from AWS lambda using AWS pinpoint to the end-user using Python.


Solution

  • I was able to get the example from https://docs.aws.amazon.com/pinpoint/latest/developerguide/send-messages-sdk.html to work with just a couple updates to the code.

    1. Deploy a new pinpoint project. Go to Settings > General Settings and note the Project ID.
    2. Pinpoint should prompt you to verify your email address since you're in the sandbox, put in your email and click the appropriate link that is emailed to you.
    3. Create a new blank lambda and add the following permissions policy to the role in IAM:
    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "Pinpoint",
                "Effect": "Allow",
                "Action": "mobiletargeting:SendMessages",
                "Resource": "*"
            }
        ]
    }
    
    1. Add the code from the developer guide to your lambda. Move the import statement above the lambda handler, and the rest inside the lambda handler. Like this:
        import boto3
    
        from botocore.exceptions import ClientError
        
        def lambda_handler(event, context):
        
            # The AWS Region that you want to use to send the email. For a list of
            # AWS Regions where the Amazon Pinpoint API is available, see
            # https://docs.aws.amazon.com/pinpoint/latest/apireference/
            AWS_REGION = "us-east-1"
            
            # The "From" address. This address has to be verified in
            # Amazon Pinpoint in the region you're using to send email.
            [...]
    
    1. Then update the following items from the sample:
    AWS_REGION = [the region you setup your pinpoint and lambda]
    SENDER = [your verified email address]
    TOADDRESS = [your verified email address]
    APPID = [your project id from pinpoint]
    
    1. Run the lambda and you should receive the email.