1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
| import os
from aws_cdk import Duration, Size, Stack, aws_apigateway, aws_ec2, aws_iam, aws_lambda, aws_logs from constructs import Construct
class LambdaPythonExampleStack(Stack): def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs)
self.vpc = self.create_vpc()
self.lambda_function = self.create_lambda_function()
self.api_gateway = self.create_api_gateway()
def create_vpc(self): vpc = aws_ec2.Vpc.from_lookup( self, "SelectedVpc", vpc_id=os.environ.get("AWS_VPC_ID"), )
return vpc
def create_lambda_function(self): lambda_role = aws_iam.Role( self, "LambdaPythonExampleLambdaRole", description="Lambda Python Example Lambda Role", assumed_by=aws_iam.CompositePrincipal( aws_iam.ServicePrincipal("lambda.amazonaws.com"), ), ) lambda_role.add_managed_policy( aws_iam.ManagedPolicy.from_managed_policy_arn( self, "LambdaPythonExampleAWSLambdaBasicExecutionRolePolicy", "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole", ) ) lambda_role.add_managed_policy( aws_iam.ManagedPolicy.from_managed_policy_arn( self, "LambdaPythonExampleAWSLambdaVPCAccessExecutionRolePolicy", "arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole", ) )
lambda_function = aws_lambda.Function( self, "LambdaPythonExampleLambdaFunction", description="Lambda Python Example Lambda Function", runtime=aws_lambda.Runtime.PYTHON_3_11, code=aws_lambda.Code.from_asset("lambda"), handler="hello.handler", architecture=aws_lambda.Architecture.ARM_64, memory_size=512, timeout=Duration.seconds(30), vpc=self.vpc, vpc_subnets=aws_ec2.SubnetSelection(subnet_type=aws_ec2.SubnetType.PRIVATE_WITH_EGRESS, one_per_az=True), role=lambda_role, environment={}, )
return lambda_function
def create_api_gateway(self): log_group = aws_logs.LogGroup( self, "LambdaPythonExampleLogGroup", retention=aws_logs.RetentionDays.ONE_MONTH, )
api_gateway = aws_apigateway.RestApi( self, "LambdaPythonExampleApiGateway", description="Lambda Python Example Api Gateway", min_compression_size=Size.kibibytes(1), endpoint_types=[aws_apigateway.EndpointType.REGIONAL], cloud_watch_role=True, deploy_options=aws_apigateway.StageOptions( stage_name="production", metrics_enabled=True, access_log_destination=aws_apigateway.LogGroupLogDestination(log_group), access_log_format=aws_apigateway.AccessLogFormat.json_with_standard_fields( caller=True, http_method=True, ip=True, protocol=True, request_time=True, resource_path=True, response_length=True, status=True, user=True, ), ), )
usage_plan = api_gateway.add_usage_plan( "LambdaPythonExampleUsagePlan", description="Lambda Python Example Usage Plan", throttle=aws_apigateway.ThrottleSettings( burst_limit=50, rate_limit=100, ), )
api_key = aws_apigateway.ApiKey( self, "LambdaPythonExampleApiKey", description="Lambda Python Example Api Key", )
usage_plan.add_api_key(api_key)
v1_resource = api_gateway.root.add_resource("v1") v1_resource.add_cors_preflight( allow_origins=aws_apigateway.Cors.ALL_ORIGINS, allow_methods=aws_apigateway.Cors.ALL_METHODS, max_age=Duration.hours(1), ) v1_method = v1_resource.add_method( "GET", aws_apigateway.LambdaIntegration(self.lambda_function), api_key_required=True, ) api_resource = v1_resource.add_resource("{path+}") api_resource.add_cors_preflight( allow_origins=aws_apigateway.Cors.ALL_ORIGINS, allow_methods=aws_apigateway.Cors.ALL_METHODS, max_age=Duration.hours(1), ) api_method = api_resource.add_method( "ANY", aws_apigateway.LambdaIntegration(self.lambda_function), api_key_required=True, )
usage_plan.add_api_stage( stage=api_gateway.deployment_stage, throttle=[ aws_apigateway.ThrottlingPerMethod( method=v1_method, throttle=aws_apigateway.ThrottleSettings( burst_limit=50, rate_limit=100, ), ), aws_apigateway.ThrottlingPerMethod( method=api_method, throttle=aws_apigateway.ThrottleSettings( burst_limit=50, rate_limit=100, ), ), ], )
return api_gateway
|