AWS Lambda scheduled event source via cloudformation

后端 未结 7 1373
猫巷女王i
猫巷女王i 2021-02-01 02:37

I already have my lambda / roles defined in cloudformation and would love to also use it to add a scheduled eventsources ... are there any docs or examples around ?

7条回答
  •  你的背包
    2021-02-01 03:10

    I solved same problem.

    "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"] }
      }
    }
    

提交回复
热议问题