|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import aws_cdk as cdk |
| 3 | +from aws_cdk import ( |
| 4 | + Duration, |
| 5 | + Stack, |
| 6 | +) |
| 7 | +from aws_cdk import ( |
| 8 | + aws_apigateway as apigateway, |
| 9 | +) |
| 10 | +from aws_cdk import ( |
| 11 | + aws_lambda as _lambda, |
| 12 | +) |
| 13 | +from aws_cdk import ( |
| 14 | + aws_logs as logs, |
| 15 | +) |
| 16 | +from constructs import Construct |
| 17 | + |
| 18 | + |
| 19 | +class PowertoolsLambdaStack(Stack): |
| 20 | + def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: |
| 21 | + super().__init__(scope, construct_id, **kwargs) |
| 22 | + |
| 23 | + # Use public Powertools layer |
| 24 | + powertools_layer = _lambda.LayerVersion.from_layer_version_arn( |
| 25 | + self, |
| 26 | + "PowertoolsLayer", |
| 27 | + layer_version_arn="arn:aws:lambda:us-east-1:017000801446:layer:AWSLambdaPowertoolsPythonV3-python313-x86_64:1", |
| 28 | + ) |
| 29 | + |
| 30 | + # Lambda Function |
| 31 | + api_function = _lambda.Function( |
| 32 | + self, |
| 33 | + "ApiFunction", |
| 34 | + runtime=_lambda.Runtime.PYTHON_3_13, |
| 35 | + handler="lambda_function.lambda_handler", |
| 36 | + code=_lambda.Code.from_asset("src"), |
| 37 | + layers=[powertools_layer], |
| 38 | + timeout=Duration.seconds(30), |
| 39 | + memory_size=512, |
| 40 | + environment={ |
| 41 | + "POWERTOOLS_SERVICE_NAME": "api-service", |
| 42 | + "POWERTOOLS_METRICS_NAMESPACE": "MyApp", |
| 43 | + "POWERTOOLS_LOG_LEVEL": "INFO", |
| 44 | + }, |
| 45 | + log_retention=logs.RetentionDays.ONE_WEEK, |
| 46 | + ) |
| 47 | + |
| 48 | + # API Gateway |
| 49 | + api = apigateway.RestApi( |
| 50 | + self, |
| 51 | + "ApiGateway", |
| 52 | + rest_api_name="Powertools API", |
| 53 | + description="API powered by Lambda with Powertools", |
| 54 | + ) |
| 55 | + |
| 56 | + # API Integration |
| 57 | + integration = apigateway.LambdaIntegration(api_function) |
| 58 | + api.root.add_proxy( |
| 59 | + default_integration=integration, |
| 60 | + any_method=True, |
| 61 | + ) |
| 62 | + |
| 63 | + # Outputs |
| 64 | + cdk.CfnOutput( |
| 65 | + self, |
| 66 | + "ApiUrl", |
| 67 | + value=api.url, |
| 68 | + description="API Gateway URL", |
| 69 | + ) |
| 70 | + |
| 71 | + |
| 72 | +app = cdk.App() |
| 73 | +PowertoolsLambdaStack(app, "PowertoolsLambdaStack") |
| 74 | +app.synth() |
0 commit comments