Skip to content

A simple lambda function that allows access to the AWS SES V2 API from AWS Lambda

License

Notifications You must be signed in to change notification settings

talentmaker/lambda-ses

Repository files navigation

Lambda SES

A simple lambda function that allows access to the AWS SES V2 API from AWS Lambda, which is definetly not to use the free 62 000 emails a month.

Installing node-client

npm i talentmaker/lambda-ses#semver:<version>
pnpm add talentmaker/lambda-ses#semver:<version>
yarn add talentmaker/lambda-ses#semver:<version>

E.G

pnpm add talentmaker/lambda-ses#semver:^1.0.0

Examples

node-client with SDK V3

import { LambdaClient } from "@aws-sdk/client-lambda";
import { LambdaSes } from "lambda-ses";

const config = {
    credentials: {
        accessKeyId: process.env.AWS_ACCESS_KEY_ID,
        secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
    },
    apiVersion: "2022-01-10",
    region: "us-east-1",
};

const lambda = new LambdaClient(config);
const lambdaSes = new LambdaSes(lambda, "lambda-ses");

lambdaSes.send({
    email: {
        from: "[email protected]",
        dest: {
            to: ["[email protected]"],
        },
        content: {
            simple: {
                body: {
                    html: {
                        charset: "UTF-8",
                        data: "<h1>Hello!</h1><br/><p>This is a message</p>",
                    },
                },
                subject: {
                    charset: "UTF-8",
                    data: "no",
                },
            },
        },
    },
});

CLI V2

Make sure cli_binary_format=raw-in-base64-out

{
    "email": {
        "from": "[email protected]",
        "dest": {
            "to": ["[email protected]"]
        },
        "content": {
            "simple": {
                "body": {
                    "html": {
                        "charset": "UTF-8",
                        "data": "<h1>Hello!</h1><br/><p>This is a message</p>"
                    }
                },
                "subject": {
                    "charset": "UTF-8",
                    "data": "no"
                }
            }
        }
    }
}
aws lambda invoke --function-name "lambda-ses" --payload "$(cat ./email.json)" /dev/stdout

Uploading to AWS

  1. Build with docker
docker build -t ses .
docker container create --name temp ses
docker container cp temp:/go/src/github.com/talentmaker/lambda-ses/main .
docker container rm temp
  1. Create Lambda Function

Lambda Config

  1. Upload to AWS
zip main.zip main
aws lambda update-function-code --function-name lambda-ses --zip-file fileb://main.zip