Shows how to deploy an AWS Lambda function, create an Amazon EventBridge schedule rule that invokes the function, and how to clean up the resources after the demo completes.
()
| 165 | |
| 166 | |
| 167 | def usage_demo(): |
| 168 | """ |
| 169 | Shows how to deploy an AWS Lambda function, create an Amazon EventBridge schedule |
| 170 | rule that invokes the function, and how to clean up the resources after the demo |
| 171 | completes. |
| 172 | """ |
| 173 | logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") |
| 174 | print("-" * 88) |
| 175 | print("Welcome to the AWS Lambda scheduled rule demo.") |
| 176 | print("-" * 88) |
| 177 | |
| 178 | lambda_function_filename = "lambda_handler_scheduled.py" |
| 179 | lambda_handler_name = "lambda_handler_scheduled.lambda_handler" |
| 180 | lambda_role_name = "demo-lambda-role" |
| 181 | lambda_function_name = "demo-lambda-scheduled" |
| 182 | event_rule_name = "demo-event-scheduled" |
| 183 | event_schedule = "rate(1 minute)" |
| 184 | |
| 185 | iam_resource = boto3.resource("iam") |
| 186 | lambda_client = boto3.client("lambda") |
| 187 | wrapper = LambdaWrapper(lambda_client, iam_resource) |
| 188 | eventbridge_client = boto3.client("events") |
| 189 | logs_client = boto3.client("logs") |
| 190 | |
| 191 | print("Checking for IAM role for Lambda...") |
| 192 | iam_role, should_wait = wrapper.create_iam_role_for_lambda(lambda_role_name) |
| 193 | if should_wait: |
| 194 | logger.info("Giving AWS time to create resources...") |
| 195 | wait(10) |
| 196 | |
| 197 | print( |
| 198 | f"Creating AWS Lambda function {lambda_function_name} from the " |
| 199 | f"{lambda_handler_name} function in {lambda_function_filename}..." |
| 200 | ) |
| 201 | deployment_package = wrapper.create_deployment_package( |
| 202 | lambda_function_filename, lambda_function_filename |
| 203 | ) |
| 204 | lambda_function_arn = wrapper.create_function( |
| 205 | lambda_function_name, lambda_handler_name, iam_role, deployment_package |
| 206 | ) |
| 207 | |
| 208 | print(f"Scheduling {lambda_function_name} to run once per minute...") |
| 209 | schedule_lambda_function( |
| 210 | eventbridge_client, |
| 211 | event_rule_name, |
| 212 | event_schedule, |
| 213 | lambda_client, |
| 214 | lambda_function_name, |
| 215 | lambda_function_arn, |
| 216 | ) |
| 217 | |
| 218 | print(f"Sleeping for 3 minutes to let our function trigger a few times...") |
| 219 | time.sleep(3 * 60) |
| 220 | |
| 221 | print(f"Getting last 20 Amazon CloudWatch log events for {lambda_function_name}...") |
| 222 | log_group_name = f"/aws/lambda/{lambda_function_name}" |
| 223 | log_streams = logs_client.describe_log_streams( |
| 224 | logGroupName=log_group_name, orderBy="LastEventTime", descending=True, limit=1 |
no test coverage detected