|
| 1 | +# Use Email Notifications |
| 2 | + |
| 3 | +The LambdaCron root module creates the scheduled Lambda and shared SNS topic, but it does not create notification channels. Use `email-notification` to send selected `result_type` values through Amazon SES. |
| 4 | + |
| 5 | +## When to Use |
| 6 | +* You want SES email notifications for one or more LambdaCron `result_type` values. |
| 7 | +* You already have, or can provide, a notification-handler container image. |
| 8 | + |
| 9 | +## Before You Begin |
| 10 | + |
| 11 | +* Complete SES setup in [Set Up SES Prerequisites](set-up-ses.md). |
| 12 | +* Verify your SES sender identity in the same AWS region as your notifier Lambda. |
| 13 | +* If your account is in SES sandbox mode, verify recipient addresses too. |
| 14 | +* Create your subject/text/html template files. |
| 15 | + |
| 16 | +## Inputs to Provide |
| 17 | + |
| 18 | +* `sns_topic_arn` from your LambdaCron stack output. |
| 19 | +* `lambda_image_uri` for the notification handler container. |
| 20 | +* `fifo_queue_name` ending with `.fifo`. |
| 21 | +* `sender`, `recipients`, and optional `reply_to`. |
| 22 | +* `subject_template_file`, `text_template_file`, and `html_template_file`. |
| 23 | +* Optional runtime/routing settings such as `result_types`, `lambda_name`, `batch_size`, and `tags`. |
| 24 | + |
| 25 | +## Steps |
| 26 | + |
| 27 | +### 1. Create email templates |
| 28 | + |
| 29 | +The email notification module requires 3 Jinja templates: one for the email subject, one for the text-only body, and one for the HTML body. |
| 30 | + |
| 31 | +```jinja |
| 32 | +{# templates/email-subject.txt #} |
| 33 | +[{{ result_type }}] LambdaCron notification |
| 34 | +``` |
| 35 | + |
| 36 | +```jinja |
| 37 | +{# templates/email-body.txt #} |
| 38 | +Result type: {{ result_type }} |
| 39 | +Message: {{ message }} |
| 40 | +``` |
| 41 | + |
| 42 | +```html |
| 43 | +<!-- templates/email-body.html --> |
| 44 | +<h2>LambdaCron notification</h2> |
| 45 | +<p><strong>Result type:</strong> {{ result_type }}</p> |
| 46 | +<p><strong>Message:</strong> {{ message }}</p> |
| 47 | +``` |
| 48 | + |
| 49 | +### 2. Republish the notification image or get the image URI |
| 50 | + |
| 51 | +If you have already republished the notification handler image to your own ECR repository, you can skip this step. Otherwise, use the `lambda-image-republish` module to copy the public image to your account/region. |
| 52 | + |
| 53 | +```hcl |
| 54 | +module "notification_image_republish" { |
| 55 | + source = "../../modules/lambda-image-republish" |
| 56 | +
|
| 57 | + source_lambda_repo = "public.ecr.aws/i9p4w7k9/lambdacron-notifications" |
| 58 | + source_lambda_tag = "latest" |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +If you do this, you can wire in the lambda image URI from the module output as shown in the next step. |
| 63 | + |
| 64 | +On the other hand, if you have your own notification handler image, you can provide the URI directly. To get that, you can find it in the ECR console or use the AWS CLI: |
| 65 | + |
| 66 | +```bash |
| 67 | +aws ecr describe-repositories --repository-names "your-repo-name" --query "repositories[0].repositoryUri" --output text |
| 68 | +``` |
| 69 | + |
| 70 | + |
| 71 | +### 3. Add the `email-notification` module |
| 72 | + |
| 73 | +You'll connect it to the LambdaCron SNS topic and provide the email settings and templates. You'll also select which result types to send email for. If you leave `result_types` empty or omit it, the notification handler will send emails for all result types. **You will get one email for each notification type**, this will not combine multiple notification types into a single email. |
| 74 | + |
| 75 | +```hcl |
| 76 | +module "email_notification" { |
| 77 | + source = "../../modules/email-notification" |
| 78 | +
|
| 79 | + sns_topic_arn = module.lambdacron.sns_topic_arn |
| 80 | + fifo_queue_name = "lambdacron-email.fifo" |
| 81 | + lambda_image_uri = module.notification_image_republish.lambda_image_uri_with_digest |
| 82 | +
|
| 83 | + result_types = ["example", "ERROR"] |
| 84 | +
|
| 85 | + sender = var.email_sender |
| 86 | + recipients = var.email_recipients |
| 87 | + reply_to = var.email_reply_to |
| 88 | +
|
| 89 | + subject_template_file = "${path.module}/templates/email-subject.txt" |
| 90 | + text_template_file = "${path.module}/templates/email-body.txt" |
| 91 | + html_template_file = "${path.module}/templates/email-body.html" |
| 92 | +
|
| 93 | + tags = local.common_tags |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +### 4. Plan and apply |
| 98 | + |
| 99 | +Here we use `tofu`, but you could also use `terraform`. This will deploy your infrastructure. |
| 100 | + |
| 101 | +```bash |
| 102 | +tofu plan |
| 103 | +tofu apply |
| 104 | +``` |
| 105 | + |
| 106 | +## Validation |
| 107 | + |
| 108 | +There are two relatively easy ways you can trigger the email notification lambda: |
| 109 | + |
| 110 | +1. [Deploy your LambdaCron with `create_test_url`](use-test-url.md) enabled and invoke the test URL. |
| 111 | +2. Publish a test message with a `result_type` value included in `result_types`: |
| 112 | + |
| 113 | +```bash |
| 114 | +aws sns publish \ |
| 115 | + --topic-arn "$(tofu output -raw sns_topic_arn)" \ |
| 116 | + --message '{"message":"Email notification smoke test"}' \ |
| 117 | + --message-attributes '{"result_type":{"DataType":"String","StringValue":"example"}}' \ |
| 118 | + --message-group-id "email-smoke-test" |
| 119 | +``` |
| 120 | + |
| 121 | +Note that the test URL won't create an email unless the result creates a message with a `result_type` value included in your notifier's `result_types` list. |
| 122 | + |
| 123 | +If you receive the email, then it was successful! |
| 124 | + |
| 125 | +If not, here are some troubleshooting tips: |
| 126 | + |
| 127 | +* Check the CloudWatch logs for the notifier Lambda for any errors. In particular, it might complain if you aren't allowed to send email (see setup instructions in [Set Up SES Prerequisites](set-up-ses.md)). |
| 128 | +* If using the test URL, check the CloudWatch logs for the scheduled Lambda to confirm that it is working. |
| 129 | +* If you don't see logs in the CloudWatch logs for your lambdas, check the Lambda console to confirm that the Lambda was created successfully and that it has the correct triggers. |
0 commit comments