2015-12-18 15 views

risposta

19

Utilizzare Aws::Event::Rule con ScheduleExpression e 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

Secondo i documenti, ScheduleExpression deve essere plurale minuti non minuti. http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/ScheduledEvents.html –

+0

@ timmah.faase Non vero? "Valori validi: minuto | minuti | ora | ore | giorno | giorni" – dsvensson

+2

Sei autorizzato singolare su "1", ad es. "1 minuto", ma è necessario utilizzare il plurale altrimenti, ad es. "10 minuti". La grammatica è applicata! – helloPiers

8

Sfortunatamente, la configurazione delle origini evento programmate per le funzioni lambda non è attualmente supportata da CloudFormation. Dovrai distribuire il tuo lambda usando CloudFormation e quindi configurare manualmente gli eventi pianificati.

CloudFormation supporta un tipo di risorsa AWS::Lambda::EventSourceMapping. Tuttavia, questa risorsa è limitata alla configurazione di stream Kinesis o DynamoDB, quindi probabilmente non ti sarà utile.

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


** Aggiornamento - a partire da aprile 2016, questo è ora supportato utilizzando CloudWatch Eventi - https://aws.amazon.com/about-aws/whats-new/2016/04/amazon-cloudwatch-events-now-supported-in-aws-cloudformation-templates/

3

A partire da questa settimana (18 aprile 2016) è ora possibile aggiungere un Regola evento programmata di CloudWatch che attiverà la tua funzione Lambda.

Il AWS::Event::Rule ha un campo ScheduleExpression per la pianificazione cron-style e un array Targets che può accettare una funzione Lambda ARN.

5

ho risolto stesso problema.

"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

Grazie mille per aggiungere un esempio operativo completo. – Seamus

+0

In LambdaInvokePermission la proprietà SourceAccount ha smesso di funzionare per me – jhanson

Problemi correlati