blob: 25faaa094c6b60792aa848fedba2a21806b33f58 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# notification-sender
A Java based AWS Lambda to send notifications via Amazon SNS (Amazon Simple Notification Service).
## Setup
### AWS Lambda
- Choose the latest version of the Java runtime
- The handler is `eu.ortlepp.notificationsender.NotificationHandler::handleRequest`
- The recommended timeout is 30 seconds
- The recommended RAM is 512 MB
- Two environment variables are required to run the Lambda:
- `REGION` - the region of your SNS topic, e.g. `eu-central-1`
- `ARN` - the ARN of your SNS topic, something like `arn:aws:sns:eu-central-1:123456789:Topic-Name`
- Create a function URL to trigger the Lambda
- It is recommended to secure the function URL with IAM
### Amazon SNS
- Create a new topic; in the topic, create a subscription
- Only e-mail subscriptions have been tested so far; other subscription types are untested
## Data
### Input (request)
HTTP POST to the function URL with JSON body:
```
{
"messages" : ["first notification", "second notification"]
}
```
### Output (response)
The response is HTTP 200 with either
```
{
"status" : "SUCCESS"
}
```
or
```
{
"status" : "FAILED"
}
```
If the Lambda setup is improper, a timeout occurred, etc., the response is HTTP 500 from the AWS environment (not the Lambda itself).
### Result (SNS message)
If the messages array contains multiple messages, all elements of the messages array are combined into one SNS message. Empty messages (empty / whitespace-only string) are omitted.
Only one message:
```
my notification
```
Multiple messages:
```
1. Notification:
first notification
2. Notification:
second notification
```
SNS will automatically append an unsubscribe footer to each e-mail.
## Testing
You can use curl to trigger your Lambda:
```
curl -X POST -H "Content-Type: application/json" -d "{\"messages\":[\"my notification\"]}" --aws-sigv4 "aws:amz:eu-central-1:lambda" --user "ACCESS_KEY:SECRET_KEY" https://MY_FUNCTION_URL.lambda-url.eu-central-1.on.aws/
```
## Documentation
- [Building Lambda functions with Java](https://docs.aws.amazon.com/lambda/latest/dg/lambda-java.html)
- [How To Secure and Authenticate AWS Lambda Function URLs](https://www.rahulpnath.com/blog/how-to-secure-and-authenticate-lambda-function-urls/) & [FUNCTION URLs - Authenticating and Securing | AWS Lambda](https://www.youtube.com/watch?v=75EEaN-oBAg)
- [Amazon SNS examples using SDK for Java 2.x](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/java_sns_code_examples.html)
|