Skip to content

A nodejs lambda that exposes a function url that can be used as a proxy to send emails using AWS SES.

License

Notifications You must be signed in to change notification settings

martinhjartmyr/aws-ses-node-lambda-proxy

Repository files navigation

aws-ses-node-lambda-proxy

A nodejs lambda that exposes a function url that can be used as a proxy to send emails using AWS SES. Before sending any emails using SES you need to follow the Getting started guide.

Installation

Clone the repo

git clone https://github.com/martinhjartmyr/aws-ses-node-lambda-proxy.git && cd aws-ses-node-lambda-proxy

Install deps

yarn install

Build the lambda

yarn build

Create an AWS IAM Role and attach required policys

aws iam create-role --role-name ses-proxy --assume-role-policy-document file://trust-policy.json && aws iam attach-role-policy --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole --role-name ses-proxy && aws iam attach-role-policy --policy-arn arn:aws:iam::aws:policy/AmazonSESFullAccess --role-name ses-proxy

Copy the Arn from the output and use it in the next step.

arn:aws:iam::123456789:role/ses-proxy

Create the lambda function with a 10 second timeout, 256mb of memory and use the role arn from the output above

aws lambda create-function --function-name ses-proxy --runtime "nodejs16.x" --timeout 10 --memory-size 256 --zip-file "fileb://dist/index.zip" --handler index.handler --role [!!! CHANGE ME ROLE FROM ABOVE OUTPUT !!!]

Set the required environment API_KEY secret

aws lambda update-function-configuration --function-name ses-proxy --environment "Variables={API_KEY=[!!! CHANGE ME!!!],DEBUG=false}"

Create a function URL. The output will display the generated URL that will be publicly accessible.

aws lambda create-function-url-config --function-name ses-proxy --auth-type NONE

Grant access to call the lambda function from the url

aws lambda add-permission --function-name ses-proxy --statement-id AuthNone --action lambda:InvokeFunctionUrl --principal "*" --function-url-auth-type NONE

Congrats, you should now be able to use the proxy to send emails. Give it a try using the curl example below.

Usage

curl --location --request POST 'https://[!!! CHANGE_ME !!!].lambda-url.eu-north-1.on.aws/' \
--header 'x-api-key: [!!! CHANGE ME_!!!]' \
--header 'Content-Type: application/json' \
--data-raw '{
    "to": ["receiver@test.com", "receiver-2@test.com"],
    "from": "from@test.com",
    "subject": "Testing subject",
    "bodyHtml": "<html>Testing! <a href=\"https://www.google.com\">Google.com</a></html>",
    "body": "Testing"
}'

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

About

A nodejs lambda that exposes a function url that can be used as a proxy to send emails using AWS SES.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published