|
| 1 | +# Built-in imports |
| 2 | +import os |
| 3 | + |
| 4 | +# External imports |
| 5 | +from aws_cdk import ( |
| 6 | + Stack, |
| 7 | + Duration, |
| 8 | + CfnOutput, |
| 9 | + aws_lambda, |
| 10 | + RemovalPolicy, |
| 11 | +) |
| 12 | +from constructs import Construct |
| 13 | + |
| 14 | + |
| 15 | +class LambdaFunctionFastAPIStack(Stack): |
| 16 | + """ |
| 17 | + Class to create the infrastructure on AWS. |
| 18 | + """ |
| 19 | + |
| 20 | + def __init__( |
| 21 | + self, |
| 22 | + scope: Construct, |
| 23 | + construct_id: str, |
| 24 | + name_prefix: str, |
| 25 | + main_resources_name: str, |
| 26 | + deployment_environment: str, |
| 27 | + **kwargs, |
| 28 | + ) -> None: |
| 29 | + super().__init__(scope, construct_id, **kwargs) |
| 30 | + |
| 31 | + # Input parameters |
| 32 | + self.construct_id = construct_id |
| 33 | + self.name_prefix = name_prefix |
| 34 | + self.main_resources_name = main_resources_name |
| 35 | + self.deployment_environment = deployment_environment |
| 36 | + |
| 37 | + # Main methods for the deployment |
| 38 | + self.create_lambda_layers() |
| 39 | + self.create_lambda_functions() |
| 40 | + |
| 41 | + # Create CloudFormation outputs |
| 42 | + self.generate_cloudformation_outputs() |
| 43 | + |
| 44 | + def create_lambda_layers(self): |
| 45 | + """ |
| 46 | + Create the Lambda layers that are necessary for the additional runtime |
| 47 | + dependencies of the Lambda Functions. |
| 48 | + """ |
| 49 | + |
| 50 | + # Layer for "LambdaPowerTools" (for logging, traces, observability, etc) |
| 51 | + self.lambda_layer_powertools = aws_lambda.LayerVersion.from_layer_version_arn( |
| 52 | + self, |
| 53 | + id="LambdaLayer-Powertools", |
| 54 | + layer_version_arn=f"arn:aws:lambda:{self.region}:017000801446:layer:AWSLambdaPowertoolsPythonV2:35", |
| 55 | + ) |
| 56 | + |
| 57 | + # Layer for "FastAPI" and "Mangum" Adapter libraries |
| 58 | + self.lambda_layer_fastapi = aws_lambda.LayerVersion( |
| 59 | + self, |
| 60 | + id="LambdaLayer-FastAPI", |
| 61 | + code=aws_lambda.Code.from_asset("lambda-layers/fastapi/modules"), |
| 62 | + compatible_runtimes=[ |
| 63 | + aws_lambda.Runtime.PYTHON_3_9, |
| 64 | + aws_lambda.Runtime.PYTHON_3_10, |
| 65 | + ], |
| 66 | + description="Lambda Layer for Python with <fastapi> library", |
| 67 | + removal_policy=RemovalPolicy.DESTROY, |
| 68 | + ) |
| 69 | + |
| 70 | + def create_lambda_functions(self): |
| 71 | + """ |
| 72 | + Create the Lambda Functions for the FastAPI server. |
| 73 | + """ |
| 74 | + # Get relative path for folder that contains Lambda function source |
| 75 | + # ! Note--> we must obtain parent dirs to create path (that"s why there is "os.path.dirname()") |
| 76 | + PATH_TO_LAMBDA_FUNCTION_FOLDER = os.path.join( |
| 77 | + os.path.dirname(os.path.dirname(os.path.dirname(__file__))), |
| 78 | + "src", |
| 79 | + "lambdas", |
| 80 | + ) |
| 81 | + self.lambda_fastapi: aws_lambda.Function = aws_lambda.Function( |
| 82 | + self, |
| 83 | + id="Lambda-FastAPI", |
| 84 | + runtime=aws_lambda.Runtime.PYTHON_3_9, |
| 85 | + handler="api/main.handler", |
| 86 | + code=aws_lambda.Code.from_asset(PATH_TO_LAMBDA_FUNCTION_FOLDER), |
| 87 | + timeout=Duration.seconds(30), |
| 88 | + memory_size=128, |
| 89 | + environment={ |
| 90 | + "ENVIRONMENT": self.deployment_environment, |
| 91 | + "LOG_LEVEL": "DEBUG", |
| 92 | + "STATE_MACHINE_ENABLED": "true", |
| 93 | + }, |
| 94 | + layers=[ |
| 95 | + self.lambda_layer_powertools, |
| 96 | + self.lambda_layer_fastapi, |
| 97 | + ], |
| 98 | + ) |
| 99 | + |
| 100 | + self.lambda_function_url = self.lambda_fastapi.add_function_url( |
| 101 | + auth_type=aws_lambda.FunctionUrlAuthType.NONE, |
| 102 | + ) |
| 103 | + |
| 104 | + def generate_cloudformation_outputs(self): |
| 105 | + """ |
| 106 | + Method to add the relevant CloudFormation outputs. |
| 107 | + """ |
| 108 | + |
| 109 | + CfnOutput( |
| 110 | + self, |
| 111 | + "DeploymentEnvironment", |
| 112 | + value=self.deployment_environment, |
| 113 | + description="Deployment environment", |
| 114 | + ) |
| 115 | + |
| 116 | + CfnOutput( |
| 117 | + self, |
| 118 | + "LambdaFunctionUrl", |
| 119 | + value=self.lambda_function_url.url, |
| 120 | + description="URL to invoke Lambda Function", |
| 121 | + ) |
0 commit comments