Created
October 18, 2021 00:41
-
-
Save gmariette/dc96156deab4a94c53ea9e9d9adee3a3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from typing_extensions import runtime | |
| import os | |
| from aws_cdk import ( | |
| core, | |
| aws_lambda as _lambda, | |
| aws_iam as iam | |
| ) | |
| class MediumLambdaStack(core.Stack): | |
| ''' | |
| DOCSTRING: Create a Cloudformation stack | |
| ''' | |
| def __init__(self, scope: core.Construct, construct_id: str, **kwargs) -> None: | |
| super().__init__(scope, construct_id, **kwargs) | |
| ''' | |
| DOCSTRING: Main program on which the objects are declared | |
| ''' | |
| # Get all the variables from the cdk.json file | |
| myenv = os.environ['MYENV'] | |
| environments = self.node.try_get_context("ENVIRONMENTS") | |
| environment = environments.get(myenv) | |
| # We will use the iam role for all the func | |
| lambda_iam_role = iam.Role.from_role_arn(self, 'Role', f'arn:aws:iam::{environment["context"].get("account")}:role/{environment["context"].get("lambda_iam_role")}', mutable=False) | |
| # Define the lambdas functions and the parameters | |
| # Structure | |
| # Lambda_name | |
| # asset: folder on which the lambda is located | |
| # handler: python file + handler | |
| my_lambdas = { | |
| "medium_lambda": { | |
| "asset": "lambda", | |
| "handler": "hello.handler" | |
| }, | |
| "medium_lambda2": { | |
| "asset": "lambda", | |
| "handler": "hello2.handler" | |
| }, | |
| } | |
| # Loop to dynamically create the lambdas | |
| for my_lambda in my_lambdas: | |
| my_lambdas[my_lambda]['func'] = create_lambda(self, my_lambda, my_lambdas[my_lambda].get('asset'), my_lambdas[my_lambda].get('handler'), environment.get('variables'), lambda_iam_role) | |
| if "cfn_output" not in my_lambdas[my_lambda].keys(): | |
| my_lambdas[my_lambda]["cfn_output"] = [] | |
| my_lambdas[my_lambda]["cfn_output"].append(create_cfn_output(self, f"{my_lambda}Name", my_lambdas[my_lambda]['func'].function_name)) | |
| def create_lambda(self, name:str, asset:str, handler:str, env_var:dict, role:iam.Role.from_role_arn) -> _lambda.Function: | |
| ''' | |
| DOCSTRING: Create a Lambda function | |
| INPUTS: | |
| - name -> Function id + function name | |
| - asset -> Folder where we can find the code | |
| - handler -> File + handler | |
| - variable -> dict which contains all the variables | |
| - role -> iam role to map to the function | |
| ''' | |
| return _lambda.Function( | |
| self, name, | |
| function_name=name, | |
| runtime=_lambda.Runtime.PYTHON_3_9, | |
| code=_lambda.Code.from_asset(asset), | |
| handler=handler, | |
| environment=env_var, | |
| role=role | |
| ) | |
| def create_cfn_output(self, output_name:str, value) -> core.CfnOutput: | |
| ''' | |
| DOCSTRING: Add a cloudformation output based on an item | |
| INPUT: | |
| - ouput_name -> The name of the output you want to use | |
| - value -> output value | |
| ''' | |
| return core.CfnOutput(self, output_name, value=value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment