Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CloudWatch Events Resource/Property #467

Merged
merged 7 commits into from
Apr 19, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions examples/CloudWatchEvents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from troposphere import Template
from troposphere.events import Rule, Target
from troposphere.iam import Role, Policy
from troposphere.awslambda import Function, Code
from troposphere import GetAtt, Join


t = Template()


# Create a Lambda function that will be mapped
code = [
"var response = require('cfn-response');",
"exports.handler = function(event, context) {",
" context.succeed('foobar!');",
" return 'foobar!';",
"};",
]

# Create a role for the lambda function
t.add_resource(Role(
"LambdaExecutionRole",
Path="/",
Policies=[Policy(
PolicyName="root",
PolicyDocument={
"Version": "2012-10-17",
"Statement": [{
"Action": ["logs:*"],
"Resource": "arn:aws:logs:*:*:*",
"Effect": "Allow"
}, {
"Action": ["lambda:*"],
"Resource": "*",
"Effect": "Allow"
}]
})],
AssumeRolePolicyDocument={"Version": "2012-10-17", "Statement": [
{
"Action": ["sts:AssumeRole"],
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com",
"apigateway.amazonaws.com"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need this one ;)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha thanks - i haven't contributed to this before so used your PR as a guide :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me neither ;)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well i guess good luck to both of us 👍

]
}
}
]},
))

# Create the Lambda function
foobar_function = t.add_resource(Function(
"FoobarFunction",
Code=Code(
ZipFile=Join("", code)
),
Handler="index.handler",
Role=GetAtt("LambdaExecutionRole", "Arn"),
Runtime="nodejs",
))

# Create the Event Target
foobar_target = Target(
"FoobarTarget",
Arn=GetAtt('FoobarFunction', 'Arn'),
Id="FooBarFunction1"
)

# Create the Event Rule
rule = Rule(
"FoobarRule",
EventPattern={
"source": [
"aws.ec2"
],
"detail-type": [
"EC2 Instance State-change Notification"
],
"detail": {
"state": [
"stopping"
]
}
},
Description="Foobar CloudWatch Event",
State="ENABLED",
Targets=[foobar_target]
)

print(t.to_json())
79 changes: 79 additions & 0 deletions tests/examples_output/CloudWatchEventsSample.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{'Source': ([<type 'basestring'>], True), 'Detail': (<type 'dict'>, True), 'DetailType': ([<type 'basestring'>], True)}
{
"Resources": {
"FoobarFunction": {
"Properties": {
"Code": {
"ZipFile": {
"Fn::Join": [
"",
[
"var response = require('cfn-response');",
"exports.handler = function(event, context) {",
" context.succeed('foobar!');",
" return 'foobar!';",
"};"
]
]
}
},
"Handler": "index.handler",
"Role": {
"Fn::GetAtt": [
"LambdaExecutionRole",
"Arn"
]
},
"Runtime": "nodejs"
},
"Type": "AWS::Lambda::Function"
},
"LambdaExecutionRole": {
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com",
"apigateway.amazonaws.com"
]
}
}
],
"Version": "2012-10-17"
},
"Path": "/",
"Policies": [
{
"PolicyDocument": {
"Statement": [
{
"Action": [
"logs:*"
],
"Effect": "Allow",
"Resource": "arn:aws:logs:*:*:*"
},
{
"Action": [
"lambda:*"
],
"Effect": "Allow",
"Resource": "*"
}
],
"Version": "2012-10-17"
},
"PolicyName": "root"
}
]
},
"Type": "AWS::IAM::Role"
}
}
}
33 changes: 33 additions & 0 deletions troposphere/events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright (c) 2013, Mark Peek <mark@peek.org>
# All rights reserved.
#
# See LICENSE file for full license.

from . import AWSObject, AWSProperty, Ref
from .validators import positive_integer, boolean

class Target(AWSProperty):

props = {
'Arn': (basestring, True),
'Id': (basestring, True),
'Input': (basestring, False),
'InputPath': (basestring, False)

}


class Rule(AWSObject):
resource_type = "AWS::Events::Rule"

props = {

'Description': (basestring, False),
'EventPattern': (dict, False),
'Name': (basestring, False),
'RoleArn': (basestring, False),
'ScheduleExpression': (basestring, False),
'State': (basestring, False),
'Targets': ([Target], False)

}