2015-12-18 6 views

Antwort

19

Verwenden Aws::Event::Rule mit einem ScheduleExpression und ein AWS::Lambda::Permission

// rule to periodically call the lambda 
"TagWatcherRule": { 
    "Type": "AWS::Events::Rule", 
    "Properties": { 
    "ScheduleExpression": "rate(10 minutes)", 
    "Targets": [ 
     { 
     "Id": "TagWatcherScheduler", 
     "Arn": { 
      "Fn::GetAtt": [ 
      "TagWatcherFunction", 
      "Arn" 
      ] 
     } 
     } 
    ] 
    } 
}, 
// role may call the lambda 
"InvokeLambdaPermission": { 
    "Type": "AWS::Lambda::Permission", 
    "Properties": { 
    "FunctionName": { 
     "Fn::GetAtt": [ 
     "TagWatcherFunction", 
     "Arn" 
     ] 
    }, 
    "Action": "lambda:InvokeFunction", 
    "Principal": "events.amazonaws.com", 
    "SourceArn": { 
     "Fn::GetAtt": [ 
     "TagWatcherRule", 
     "Arn" 
     ] 
    } 
    } 
} 
+0

Laut den Dokumenten muss ScheduleExpression mehrere Minuten nicht Minute sein. http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/ScheduledEvents.html –

+0

@ timmah.faase Nicht wahr? "Gültige Werte: Minute | Minuten | Stunde | Stunden | Tag | Tage" – dsvensson

+2

Sie dürfen Singular auf "1", z. "1 Minute", muss jedoch im Plural verwendet werden, z. "10 Minuten". Grammatik wird durchgesetzt! – helloPiers

8

Leider wird das Konfigurieren geplanter Ereignisquellen für Lambda-Funktionen derzeit von CloudFormation nicht unterstützt. Sie müssen Ihr Lambda mithilfe von CloudFormation bereitstellen und dann Ihre geplanten Ereignisse manuell konfigurieren.

CloudFormation unterstützt einen Ressourcentyp AWS::Lambda::EventSourceMapping. Diese Ressource ist jedoch nur eingeschränkt in der Lage, Kinesis- oder DynamoDB-Streams zu konfigurieren, daher ist dies für Sie wahrscheinlich nicht hilfreich.

http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-eventsourcemapping.html


** Update - ab April 2016 wird diese nun unterstützt mit Cloudwatch Events - https://aws.amazon.com/about-aws/whats-new/2016/04/amazon-cloudwatch-events-now-supported-in-aws-cloudformation-templates/

3

Ab dieser Woche (18. April 2016) ist es nun möglich, ein hinzufügen Geplante CloudWatch-Ereignisregel, die Ihre Lambda-Funktion auslöst.

Die AWS::Event::Rule hat eine ScheduleExpression Feld für den cron-style Zeitplan und eine Targets Array, die eine Lambda-Funktion ARN annehmen kann.

5

ich gleiches Problem gelöst.

"RoleForLambdaStopEC2Instances" : { 
    "Type": "AWS::IAM::Role", 
    "Properties": { 
    "AssumeRolePolicyDocument": { 
     "Version": "2012-10-17", 
     "Statement": [ 
     { 
      "Sid": "", 
      "Effect": "Allow", 
      "Principal": { 
      "Service": "lambda.amazonaws.com" 
      }, 
      "Action": "sts:AssumeRole" 
     } 
     ] 
    }, 
    "Policies": [ 
     { 
     "PolicyName": "LambdaStopEC2InstancesPolicy", 
     "PolicyDocument": { 
      "Version": "2012-10-17", 
      "Statement": [ 
      { 
       "Effect": "Allow", 
       "Action": [ 
       "logs:CreateLogGroup", 
       "logs:CreateLogStream", 
       "logs:PutLogEvents", 
       "ec2:StopInstances" 
       ], 
       "Resource": [ 
       "arn:aws:logs:*:*:*", 
       "arn:aws:ec2:*" 
       ] 
      } 
      ] 
     } 
     } 
    ], 
    "Path": "/" 
    } 
}, 
"LambdaStopEC2Instances": { 
    "Type": "AWS::Lambda::Function", 
    "Properties": { 
    "Code": { 
     "S3Bucket": "XXXXXXXXXXXXXXXXX", 
     "S3Key": "XXXXXXXXXXXXXXXXXX" 
    }, 
    "Handler": "stopEC2Instances.handler", 
    "Role": { "Fn::GetAtt" : ["RoleForLambdaStopEC2Instances", "Arn"] }, 
    "Runtime": "nodejs4.3", 
    "Timeout": "5" 
    } 
}, 
"StopEC2InstancesRule": { 
    "Type" : "AWS::Events::Rule", 
    "Properties" : { 
    "Name" : "StopEC2Instances", 
    "ScheduleExpression" : "cron(0 13 ? * MON-FRI *)", 
    "State": "ENABLED", 
    "Targets": [{ 
     "Arn": { "Fn::GetAtt": ["LambdaStopEC2Instances", "Arn"] }, 
     "Id": "stopEC2Instances" 
    }] 
    } 
}, 
"LambdaInvokePermission": { 
    "Type": "AWS::Lambda::Permission", 
    "Properties": { 
    "FunctionName" : { "Fn::GetAtt" : ["LambdaStopEC2Instances", "Arn"] }, 
    "Action": "lambda:InvokeFunction", 
    "Principal": "events.amazonaws.com", 
    "SourceAccount": { "Ref" : "AWS::AccountId" }, 
    "SourceArn": { "Fn::GetAtt": ["StopEC2InstancesRule","Arn"] } 
    } 
} 
+0

Vielen Dank für das Hinzufügen eines vollständigen Arbeitsbeispiels. – Seamus

+0

In LambdaInvokePermission hat die SourceAccount-Eigenschaft die Ausführung für mich gestoppt – jhanson