Skip to content

Commit

Permalink
docs: added AWS WorkMail hello world app in examples (#670)
Browse files Browse the repository at this point in the history
  • Loading branch information
sagariiit authored and keetonian committed Nov 19, 2018
1 parent 55c599d commit 75cb2e8
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 0 deletions.
41 changes: 41 additions & 0 deletions examples/2016-10-31/workmail-hello-world-python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# AWS WorkMail hello world

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)

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)

### Local development

First, [set up the SAM CLI](https://github.com/awslabs/aws-sam-cli/blob/develop/docs/installation.rst).

Now, test the application locally using:

`sam local invoke WorkMailHelloWorldFunction -e event.json`

### Deploying

```bash
sam package \
--template-file template.yaml \
--output-template-file packaged.yaml \
--s3-bucket $YOUR_BUCKET_NAME
```

```bash
sam deploy \
--template-file packaged.yaml \
--stack-name workmail-hello-world \
--capabilities CAPABILITY_IAM
```

### Configure WorkMail
Find the ARN of your new lambda function using:

```bash
aws cloudformation describe-stacks \
--stack-name workmail-hello-world \
--query 'Stacks[].Outputs[0].OutputValue'
```

Now you can go to WorkMail console and configure an outbound rule to use your new lambda.

17 changes: 17 additions & 0 deletions examples/2016-10-31/workmail-hello-world-python/event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"summaryVersion": "2018-10-10",
"envelope": {
"mailFrom" : {
"address" : "from@domain.test"
},
"recipients" : [
{ "address" : "recipient1@domain.test" },
{ "address" : "recipient2@domain.test" }
]
},
"sender" : {
"address" : "sender@domain.test"
},
"subject" : "Hello From Amazon WorkMail!",
"truncated": false
}
25 changes: 25 additions & 0 deletions examples/2016-10-31/workmail-hello-world-python/template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
WorkMail hello world lambda SAM
Resources:
WorkMailHelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: workmail-hello-world/
Handler: app.lambda_handler
Runtime: python3.6
Timeout: 10

PermissionToCallLambdaAbove:
Type: AWS::Lambda::Permission
DependsOn: WorkMailHelloWorldFunction
Properties:
Action: lambda:InvokeFunction
FunctionName: !Ref WorkMailHelloWorldFunction
Principal: !Sub 'workmail.${AWS::Region}.amazonaws.com'

Outputs:
HelloWorldArn:
Value: !GetAtt WorkMailHelloWorldFunction.Arn
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"""
Hello world example for AWS WorkMail
Parameters
----------
event: dict, required
AWS WorkMail Message Summary Input Format
For more information, see https://docs.aws.amazon.com/workmail/latest/adminguide/lambda.html
{
"summaryVersion": "2018-10-10", # AWS WorkMail Message Summary Version
"envelope": {
"mailFrom" : {
"address" : "from@domain.test" # String containing from email address
},
"recipients" : [ # List of all recipient email addresses
{ "address" : "recipient1@domain.test" },
{ "address" : "recipient2@domain.test" }
]
},
"sender" : {
"address" : "sender@domain.test" # String containing sender email address
},
"subject" : "Hello From Amazon WorkMail!", # String containing email subject (Truncated to first 256 chars)"
"truncated": false # boolean indicating if any field in message was truncated due to size limitations
}
context: object, required
Lambda Context runtime methods and attributes
Attributes
----------
context.aws_request_id: str
Lambda request ID
context.client_context: object
Additional context when invoked through AWS Mobile SDK
context.function_name: str
Lambda function name
context.function_version: str
Function version identifier
context.get_remaining_time_in_millis: function
Time in milliseconds before function times out
context.identity:
Cognito identity provider context when invoked through AWS Mobile SDK
context.invoked_function_arn: str
Function ARN
context.log_group_name: str
Cloudwatch Log group name
context.log_stream_name: str
Cloudwatch Log stream name
context.memory_limit_in_mb: int
Function memory
https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html
Returns
------
Nothing
"""
def lambda_handler(event, context):
try:
fromAddress = event['envelope']['mailFrom']['address']
subject = event['subject']
print(f"Received Email from {fromAddress} with Subject {subject}")

except Exception as e:
# Send some context about this error to Lambda Logs
print(e)
raise e

0 comments on commit 75cb2e8

Please sign in to comment.