Skip to content

Instantly share code, notes, and snippets.

@jgontrum
Last active August 4, 2022 14:49
Show Gist options
  • Save jgontrum/81eceb94c0bc80fda14f715ee1d972bb to your computer and use it in GitHub Desktop.
Save jgontrum/81eceb94c0bc80fda14f715ee1d972bb to your computer and use it in GitHub Desktop.
import boto3
import json
client = boto3.client('events')
lambda_client = boto3.client('lambda')
def add_schedule():
rule = client.put_rule(
Name="MyRuleId",
ScheduleExpression="rate(2 minutes)",
State="ENABLED"
RoleArn="MyRoleARN"
)
client.put_targets(
Rule=rule_id,
Targets=[
{
"Id": "MyTargetId",
"Arn": "MyLambdaFunctionARN",
"Input": json.dumps({"foo": "bar"})
}
]
)
lambda_client.add_permission(
FunctionName="MyLambdaFunctionARN",
StatementId="IUseTheSameHereAsTheRuleIdButYouDoAsYouPlease",
Action="lambda:InvokeFunction",
Principal="events.amazonaws.com",
SourceArn=rule["RuleArn"]
)
def remove_schedule():
client.remove_targets(Rule="MyRuleId", Ids=["MyTargetId"])
client.delete_rule(Name="MyRuleId")
lambda_client.remove_permission(
FunctionName="MyLambdaFunctionARN",
StatementId="IUseTheSameHereAsTheRuleIdButYouDoAsYouPlease"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment