Shows how to deploy an AWS Lambda function, create a REST API, call the REST API in various ways, and remove all of the resources after the demo completes.
()
| 202 | |
| 203 | |
| 204 | def usage_demo(): |
| 205 | """ |
| 206 | Shows how to deploy an AWS Lambda function, create a REST API, call the REST API |
| 207 | in various ways, and remove all of the resources after the demo completes. |
| 208 | """ |
| 209 | logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") |
| 210 | print("-" * 88) |
| 211 | print("Welcome to the AWS Lambda and Amazon API Gateway REST API creation demo.") |
| 212 | print("-" * 88) |
| 213 | |
| 214 | lambda_filename = "lambda_handler_rest.py" |
| 215 | lambda_handler_name = "lambda_handler_rest.lambda_handler" |
| 216 | lambda_role_name = "demo-lambda-role" |
| 217 | lambda_function_name = "demo-lambda-rest" |
| 218 | api_name = "demo-lambda-rest-api" |
| 219 | |
| 220 | iam_resource = boto3.resource("iam") |
| 221 | lambda_client = boto3.client("lambda") |
| 222 | wrapper = LambdaWrapper(lambda_client, iam_resource) |
| 223 | apig_client = boto3.client("apigateway") |
| 224 | |
| 225 | print("Checking for IAM role for Lambda...") |
| 226 | iam_role, should_wait = wrapper.create_iam_role_for_lambda(lambda_role_name) |
| 227 | if should_wait: |
| 228 | logger.info("Giving AWS time to create resources...") |
| 229 | wait(10) |
| 230 | |
| 231 | print( |
| 232 | f"Creating AWS Lambda function {lambda_function_name} from " |
| 233 | f"{lambda_handler_name}..." |
| 234 | ) |
| 235 | deployment_package = wrapper.create_deployment_package( |
| 236 | lambda_filename, lambda_filename |
| 237 | ) |
| 238 | lambda_function_arn = wrapper.create_function( |
| 239 | lambda_function_name, lambda_handler_name, iam_role, deployment_package |
| 240 | ) |
| 241 | |
| 242 | print(f"Creating Amazon API Gateway REST API {api_name}...") |
| 243 | account_id = boto3.client("sts").get_caller_identity()["Account"] |
| 244 | api_base_path = "demoapi" |
| 245 | api_stage = "test" |
| 246 | api_id = create_rest_api( |
| 247 | apig_client, |
| 248 | api_name, |
| 249 | api_base_path, |
| 250 | api_stage, |
| 251 | account_id, |
| 252 | lambda_client, |
| 253 | lambda_function_arn, |
| 254 | ) |
| 255 | api_url = construct_api_url( |
| 256 | api_id, apig_client.meta.region_name, api_stage, api_base_path |
| 257 | ) |
| 258 | print(f"REST API created, URL is :\n\t{api_url}") |
| 259 | print(f"Sleeping for a couple seconds to give AWS time to prepare...") |
| 260 | time.sleep(2) |
| 261 |
no test coverage detected