Handle SES create receipt rule

This commit is contained in:
Benoit Beraud 2016-07-14 16:58:51 +02:00
parent 30d131bc3b
commit 77a8ea69c6
5 changed files with 180 additions and 3 deletions

View file

@ -300,6 +300,24 @@ SnsSubscribeFunctionArn
[sns.subscribe.template](test/aws/sns.subscribe.template)
### Create a SES Receipt Rule
Allows to create an SES Receipt Rule inside an existing SES Rule set (active or not).
Mirrors the [SES.CreateReceipRule API method](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SES.html#createReceiptRule-property).
This will delete the rule when the corresponding stack is deleted.
#### Paramters
See the reference above or the example below for full list of parameters. All parameters are directly passed 'as is' except boolean which are converted.
#### Reference Output Name
SesCreateReceiptRuleFunctionArn
#### Example/Test Template
[ses.createReceiptRule.template](test/aws/ses.createReceiptRule.template)
## Deployment (contributors)
After making changes (i.e. adding a new helper function), please do the following:
@ -318,4 +336,4 @@ After making changes (i.e. adding a new helper function), please do the followin
## License
Copyright 2016 Gilt Groupe, Inc.
Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0

39
aws/ses.js Normal file
View file

@ -0,0 +1,39 @@
var Promise = require('bluebird'),
AWS = require('aws-sdk'),
base = require('lib/base'),
helpers = require('lib/helpers'),
ses = Promise.promisifyAll(new AWS.SES());
// Exposes the SES.createReceiptRule API method
function CreateReceiptRule(event, context) {
base.Handler.call(this, event, context);
}
CreateReceiptRule.prototype = Object.create(base.Handler.prototype);
CreateReceiptRule.prototype.handleCreate = function() {
var p = this.event.ResourceProperties;
delete p.ServiceToken;
p.Rule.Enabled = ("true" === p.Rule.Enabled );
p.Rule.ScanEnabled = ("true" === p.Rule.ScanEnabled );
return ses.createReceiptRuleAsync(p)
.then(function() {
return {
RuleSetName : p.RuleSetName,
RuleName : p.Rule.Name
}
});
}
CreateReceiptRule.prototype.handleDelete = function(referenceData) {
return Promise.try(function() {
if (referenceData) {
return ses.deleteReceiptRuleAsync({
RuleSetName : referenceData.RuleSetName,
RuleName : referenceData.RuleName
});
}
});
}
exports.createReceiptRule = function(event, context) {
console.log(JSON.stringify(event));
handler = new CreateReceiptRule(event, context);
handler.handle();
}

View file

@ -383,6 +383,61 @@
"DependsOn": [
"SnsSubscribeFunctionRole"
]
},
"SesCreateReceiptRuleFunctionRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version" : "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [ "lambda.amazonaws.com" ]
},
"Action": [ "sts:AssumeRole" ]
}
]
},
"ManagedPolicyArns": [
{ "Ref": "RoleBasePolicy" }
],
"Policies": [
{
"PolicyName": "SESReceiptRuleModifier",
"PolicyDocument": {
"Version" : "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ses:CreateReceiptRule",
"ses:DeleteReceiptRule"
],
"Resource": "*"
}
]
}
}
]
}
},
"SesCreateReceiptRuleFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"S3Bucket": "com.gilt.public.backoffice",
"S3Key": "lambda_functions/cloudformation-helpers.zip"
},
"Description": "Used to create SES receipt rules.",
"Handler": "aws/ses.createReceiptRule",
"Role": {"Fn::GetAtt" : [ "SesCreateReceiptRuleFunctionRole", "Arn" ] },
"Runtime": "nodejs4.3",
"Timeout": 30
},
"DependsOn": [
"SesCreateReceiptRuleFunctionRole"
]
}
},
"Outputs": {
@ -409,6 +464,10 @@
"S3PutObjectFunctionArn": {
"Description": "The ARN of the S3PutObjectFunction, for use in other CloudFormation templates.",
"Value": { "Fn::GetAtt" : ["S3PutObjectFunction", "Arn"] }
},
"SesCreateReceiptRuleFunctionArn": {
"Description": "The ARN of the SesCreateReceiptRuleFunction, for use in other CloudFormation templates.",
"Value": { "Fn::GetAtt" : ["SesCreateReceiptRuleFunction", "Arn"] }
}
}
}
}

View file

@ -51,4 +51,4 @@
]
}
}
}
}

View file

@ -0,0 +1,61 @@
{
"AWSTemplateFormatVersion": "2010-09-09",
"Parameters": {
"CFHelperStackName": {
"Type": "String",
"Description": "The name of the stack where you installed the CloudFormation helper functions. See https://github.com/gilt/cloudformation-helpers."
},
"RuleSetName": {
"Type": "String",
"Description": "The name of the rule set where to create the rule. Must already exist."
},
"S3Bucket": {
"Type": "String",
"Description": "The name of the S3 bucket where to put the object. Must already exist."
},
"MailRecipient" :{
"Type": "String",
"Description": "Email used to receive mails in the configured rule"
}
},
"Resources": {
"CFHelperStack": {
"Type": "AWS::CloudFormation::Stack",
"Properties": {
"TemplateURL": "https://s3.amazonaws.com/com.gilt.public.backoffice/cloudformation_templates/lookup_stack_outputs.template"
}
},
"CFHelper": {
"Type": "Custom::CFHelper",
"Properties": {
"ServiceToken": { "Fn::GetAtt" : ["CFHelperStack", "Outputs.LookupStackOutputsArn"] },
"StackName": { "Ref": "CFHelperStackName" }
},
"DependsOn": [
"CFHelperStack"
]
},
"SesCreateReceiptRule": {
"Type": "Custom::SesCreateReceiptRule",
"Properties": {
"ServiceToken": { "Fn::GetAtt" : ["CFHelper", "SesCreateReceiptRuleFunctionArn"] },
"Rule" : {
"Name": "Test-SESRule",
"Recipients" : [{ "Ref": "MailRecipient" }],
"Enabled" : true,
"ScanEnabled" : true,
"Actions" : [{
"S3Action": {
"BucketName": { "Ref": "S3Bucket" },
"ObjectKeyPrefix": "incoming_mails/"
}
}]
},
"RuleSetName" :{ "Ref": "RuleSetName" }
},
"DependsOn": [
"CFHelper"
]
}
}
}