diff --git a/examples/CloudWatchEventsSample.py b/examples/CloudWatchEventsSample.py new file mode 100644 index 000000000..6333ac6ec --- /dev/null +++ b/examples/CloudWatchEventsSample.py @@ -0,0 +1,58 @@ +from troposphere import Template +from troposphere.events import Rule, Target +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 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()) diff --git a/tests/examples_output/CloudWatchEventsSample.template b/tests/examples_output/CloudWatchEventsSample.template new file mode 100644 index 000000000..ef4ef21c8 --- /dev/null +++ b/tests/examples_output/CloudWatchEventsSample.template @@ -0,0 +1,31 @@ +{ + "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" + } + } +} diff --git a/troposphere/events.py b/troposphere/events.py new file mode 100644 index 000000000..a058e3c1e --- /dev/null +++ b/troposphere/events.py @@ -0,0 +1,32 @@ +# Copyright (c) 2013, Mark Peek +# All rights reserved. +# +# See LICENSE file for full license. + +from . import AWSObject, AWSProperty + + +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) + + }