Thursday, August 18, 2022
HomeWordPress DevelopmentUse Boto3 create EventBridge-Rule tigger Lambda Operate

Use Boto3 create EventBridge-Rule tigger Lambda Operate


import os
import time
import boto3
from botocore.config import Config

lambda_client = boto3.shopper('lambda',config=Config(region_name=os.environ['AWS_REGION']))
event_client = boto3.shopper('occasions',config=Config(region_name=os.environ['AWS_REGION']))

def lambda_handler(occasion, context):
    rule_name = 'LeifengRule' # Outline a var for rule_name
    cron_sec = 'cron(59 23 * * ? *)' # Outline a var for cron
    lambda_fc_name = 'LeifengFC' # Outline a var for lambda title
    lambda_fc_arn = 'arn:aws:lambda:us-east-1:431660491089:operate:LeifengFC' # Right here you want copy the lambda_fc_name operate arn
    add_permission_role_arn = 'arn:aws:iam::431660491089:position/add_permission' # put create position ARN
    # use boto3 create a rule
    create_rule_resp = event_client.put_rule(
            Identify=rule_name, # There put your rule title
            ScheduleExpression=cron_sec, # there put your cron
            State='ENABLED', # there set the rule state ENABLED or DISABLED
            EventBusName='default', # set eventbus ,I take advantage of default
            RoleArn=add_permission_role_arn
        )

    put_target_resp = event_client.put_targets(
            Rule=rule_name,
            Targets=[{
                'Id': lambda_fc_name,
                'Arn': lambda_fc_arn
            }]
        )

    # use if to find out the lambda_fc_arn climate '$' exists
    # if the '$' in lambda_fc_arn,simply take away from $

    if '$' in lambda_fc_arn:
        lambda_fc_arn = lambda_fc_arn[:-8]
    add_lambda_permission = lambda_client.add_permission(
            FunctionName=lambda_fc_arn,
            StatementId=str(time.time())[-5:]+lambda_fc_name,
            Motion='lambda:InvokeFunction',
            Principal='occasions.amazonaws.com',
            SourceArn=create_rule_resp['RuleArn']
        )
Enter fullscreen mode

Exit fullscreen mode



1.1 Create add_permission position



1.1.1 Open AWS IAM console

Click on right here:https://us-east-1.console.aws.amazon.com/iam/residence



1.1.2 Create a job

Image description

Image description



1.1.2.1 use the json file

{
  "Model": "2012-10-17",
  "Assertion": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "lambda.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    },
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "events.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
} 
Enter fullscreen mode

Exit fullscreen mode

Image description



1.1.2.2 connect 2 coverage to the position(lambda and occasion full entry)

Image description
Image description



1.1.2.3 Keep in mind the add_permission position ARN

Image description



1.2 Create lambda execute position



1.2.1 Open AWS IAM console

Click on right here:https://us-east-1.console.aws.amazon.com/iam/residence



1.2.2 Create a job for lambda

Image description



1.2.3 set a reputation lambda_exec_role

Image description



1.2.4 verify the position(not connect any coverage)

Image description



1.3 Create a coverage for lambda_exec_role

{
    "Model": "2012-10-17",
    "Assertion": [
        {
            "Effect": "Allow",
            "Action": [
                "events:DeleteRule",
                "events:PutTargets",
                "events:DescribeRule",
                "events:ListRuleNamesByTarget",
                "events:EnableRule",
                "events:PutRule",
                "events:ListRules",
                "events:RemoveTargets",
                "events:ListTargetsByRule",
                "events:DisableRule",
                "lambda:ListFunctions",
                "lambda:AddPermission",
                "iam:PassRole"
            ],
            "Useful resource": "*"
        }
    ]
}
Enter fullscreen mode

Exit fullscreen mode



1.4 connect lambda_exec_role_policy to lambda_exec_role

Image description
Image description

Image description



2.1 create a lambda operate set runtime as python3.9

Image description



2.2 Copy the code to lambda

please substitute rule_name cron_sec lambda_fc_name and lambda_fc_arn worth

this code simply present you

import os
import time
import boto3
from botocore.config import Config

lambda_client = boto3.shopper('lambda',config=Config(region_name=os.environ['AWS_REGION']))
event_client = boto3.shopper('occasions',config=Config(region_name=os.environ['AWS_REGION']))

def lambda_handler(occasion, context):
    rule_name = 'LeifengRule' # Outline a var for rule_name
    cron_sec = 'cron(59 23 * * ? *)' # Outline a var for cron
    lambda_fc_name = 'LeifengFC' # Outline a var for lambda title
    lambda_fc_arn = 'arn:aws:lambda:us-east-1:431660491089:operate:LeifengFC' # Right here you want copy the lambda_fc_name operate arn
    add_permission_role_arn = 'arn:aws:iam::431660491089:position/add_permission' # put create position ARN
    # use boto3 create a rule
    create_rule_resp = event_client.put_rule(
            Identify=rule_name, # There put your rule title
            ScheduleExpression=cron_sec, # there put your cron
            State='ENABLED', # there set the rule state ENABLED or DISABLED
            EventBusName='default', # set eventbus ,I take advantage of default
            RoleArn=add_permission_role_arn
        )

    put_target_resp = event_client.put_targets(
            Rule=rule_name,
            Targets=[{
                'Id': lambda_fc_name,
                'Arn': lambda_fc_arn
            }]
        )

    # use if to find out the lambda_fc_arn climate '$' exists
    # if the '$' in lambda_fc_arn,simply take away from $

    if '$' in lambda_fc_arn:
        lambda_fc_arn = lambda_fc_arn[:-8]
    add_lambda_permission = lambda_client.add_permission(
            FunctionName=lambda_fc_arn,
            StatementId=str(time.time())[-5:]+lambda_fc_name,
            Motion='lambda:InvokeFunction',
            Principal='occasions.amazonaws.com',
            SourceArn=create_rule_resp['RuleArn']
        )
Enter fullscreen mode

Exit fullscreen mode

Image description

Image description
Image description

Image description

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments