Skip to content

Commit 75cb2e8

Browse files
sagariiitkeetonian
authored andcommitted
docs: added AWS WorkMail hello world app in examples (#670)
1 parent 55c599d commit 75cb2e8

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# AWS WorkMail hello world
2+
3+
This is a hello world example of the WorkMail lambda feature. For more information see [AWS WorMail lambda documentation](https://docs.aws.amazon.com/workmail/latest/adminguide/lambda.html)
4+
5+
To use this application you can deploy it via Lambda console. Visit [AWS Lambda Console](https://console.aws.amazon.com/lambda/home?region=us-east-1#/create?firstrun=true&tab=serverlessApps)
6+
7+
### Local development
8+
9+
First, [set up the SAM CLI](https://github.com/awslabs/aws-sam-cli/blob/develop/docs/installation.rst).
10+
11+
Now, test the application locally using:
12+
13+
`sam local invoke WorkMailHelloWorldFunction -e event.json`
14+
15+
### Deploying
16+
17+
```bash
18+
sam package \
19+
--template-file template.yaml \
20+
--output-template-file packaged.yaml \
21+
--s3-bucket $YOUR_BUCKET_NAME
22+
```
23+
24+
```bash
25+
sam deploy \
26+
--template-file packaged.yaml \
27+
--stack-name workmail-hello-world \
28+
--capabilities CAPABILITY_IAM
29+
```
30+
31+
### Configure WorkMail
32+
Find the ARN of your new lambda function using:
33+
34+
```bash
35+
aws cloudformation describe-stacks \
36+
--stack-name workmail-hello-world \
37+
--query 'Stacks[].Outputs[0].OutputValue'
38+
```
39+
40+
Now you can go to WorkMail console and configure an outbound rule to use your new lambda.
41+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"summaryVersion": "2018-10-10",
3+
"envelope": {
4+
"mailFrom" : {
5+
"address" : "[email protected]"
6+
},
7+
"recipients" : [
8+
{ "address" : "[email protected]" },
9+
{ "address" : "[email protected]" }
10+
]
11+
},
12+
"sender" : {
13+
"address" : "[email protected]"
14+
},
15+
"subject" : "Hello From Amazon WorkMail!",
16+
"truncated": false
17+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: >
4+
WorkMail hello world lambda SAM
5+
6+
Resources:
7+
WorkMailHelloWorldFunction:
8+
Type: AWS::Serverless::Function
9+
Properties:
10+
CodeUri: workmail-hello-world/
11+
Handler: app.lambda_handler
12+
Runtime: python3.6
13+
Timeout: 10
14+
15+
PermissionToCallLambdaAbove:
16+
Type: AWS::Lambda::Permission
17+
DependsOn: WorkMailHelloWorldFunction
18+
Properties:
19+
Action: lambda:InvokeFunction
20+
FunctionName: !Ref WorkMailHelloWorldFunction
21+
Principal: !Sub 'workmail.${AWS::Region}.amazonaws.com'
22+
23+
Outputs:
24+
HelloWorldArn:
25+
Value: !GetAtt WorkMailHelloWorldFunction.Arn
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""
2+
3+
Hello world example for AWS WorkMail
4+
5+
Parameters
6+
----------
7+
event: dict, required
8+
AWS WorkMail Message Summary Input Format
9+
For more information, see https://docs.aws.amazon.com/workmail/latest/adminguide/lambda.html
10+
11+
{
12+
"summaryVersion": "2018-10-10", # AWS WorkMail Message Summary Version
13+
"envelope": {
14+
"mailFrom" : {
15+
"address" : "[email protected]" # String containing from email address
16+
},
17+
"recipients" : [ # List of all recipient email addresses
18+
{ "address" : "[email protected]" },
19+
{ "address" : "[email protected]" }
20+
]
21+
},
22+
"sender" : {
23+
"address" : "[email protected]" # String containing sender email address
24+
},
25+
"subject" : "Hello From Amazon WorkMail!", # String containing email subject (Truncated to first 256 chars)"
26+
"truncated": false # boolean indicating if any field in message was truncated due to size limitations
27+
}
28+
29+
context: object, required
30+
Lambda Context runtime methods and attributes
31+
32+
Attributes
33+
----------
34+
35+
context.aws_request_id: str
36+
Lambda request ID
37+
context.client_context: object
38+
Additional context when invoked through AWS Mobile SDK
39+
context.function_name: str
40+
Lambda function name
41+
context.function_version: str
42+
Function version identifier
43+
context.get_remaining_time_in_millis: function
44+
Time in milliseconds before function times out
45+
context.identity:
46+
Cognito identity provider context when invoked through AWS Mobile SDK
47+
context.invoked_function_arn: str
48+
Function ARN
49+
context.log_group_name: str
50+
Cloudwatch Log group name
51+
context.log_stream_name: str
52+
Cloudwatch Log stream name
53+
context.memory_limit_in_mb: int
54+
Function memory
55+
56+
https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html
57+
58+
Returns
59+
------
60+
Nothing
61+
"""
62+
def lambda_handler(event, context):
63+
try:
64+
fromAddress = event['envelope']['mailFrom']['address']
65+
subject = event['subject']
66+
print(f"Received Email from {fromAddress} with Subject {subject}")
67+
68+
except Exception as e:
69+
# Send some context about this error to Lambda Logs
70+
print(e)
71+
raise e
72+

0 commit comments

Comments
 (0)