Comment déployer et attacher une couche à la fonction aws lambda à l'aide de aws CDK?
J'ai besoin d'un code cdk simple qui déploie et attache une couche à la fonction aws lambda.
3 Réponses :
Le code Python aws CDK suivant déploie une couche et l'attache à une fonction aws lambda.
par yl.
import datetime def cust_fun(): print("Hello from the deep layers!!") date_time = datetime.datetime.now().isoformat() print("dateTime:[%s]\n" % (date_time)) return 1
# ------------------------------------------------- # testLambda # ------------------------------------------------- import custom_func as cf # this line is errored in pyCharm -> will be fixed on aws when import the layer def lambda_handler(event, context): print(f"EVENT:{event}") ret = cf.cust_fun() return { 'statusCode': 200, 'body': ret }
from aws_cdk import ( aws_lambda as _lambda, core, aws_iam) from aws_cdk.aws_iam import PolicyStatement from aws_cdk.aws_lambda import LayerVersion, AssetCode class CdkLayersStack(core.Stack): def __init__(self, scope: core.Construct, id: str, **kwargs) -> None: super().__init__(scope, id, **kwargs) # 1) deploy lambda functions testLambda : _lambda.Function = CdkLayersStack.cdkResourcesCreate(self) # 2) attach policy to function projectPolicy = CdkLayersStack.createPolicy(self, testLambda) # ----------------------------------------------------------------------------------- @staticmethod def createPolicy(this, testLambda:_lambda.Function) -> None: projectPolicy:PolicyStatement = PolicyStatement( effect=aws_iam.Effect.ALLOW, # resources=["*"], resources=[testLambda.function_arn], actions=[ "dynamodb:Query", "dynamodb:Scan", "dynamodb:GetItem", "dynamodb:PutItem", "dynamodb:UpdateItem", "states:StartExecution", "states:SendTaskSuccess", "states:SendTaskFailure", "cognito-idp:ListUsers", "ses:SendEmail" ] ) return projectPolicy; # ----------------------------------------------------------------------------------- @staticmethod def cdkResourcesCreate(self) -> None: lambdaFunction:_lambda.Function = _lambda.Function(self, 'testLambda', function_name='testLambda', handler='testLambda.lambda_handler', runtime=_lambda.Runtime.PYTHON_3_7, code=_lambda.Code.asset('functions'), ) ac = AssetCode("layers") layer = LayerVersion(self, "l1", code=ac, description="test-layer", layer_version_name='Test-layer-version-name') lambdaFunction.add_layers(layer) return lambdaFunction # -----------------------------------------------------------------------------------
#!/usr/bin/env python3 import sys from aws_cdk import (core) from cdk_layers_deploy import CdkLayersStack app = core.App() CdkLayersStack(app, "cdk-layers") app.synth()
--+ +-app.py +-cdk_layers_deploy.py +--/functions+ +-testLambda.py +--/layers+ +-custom_func.py
Ça ne marchera pas. Vous devez placer custom_func.py
dans les layers/python/lib/python3.7/site-packages/custom_func.py
place pour le faire fonctionner ( https://docs.aws.amazon.com/lambda/latest/dg/configuration -layers.html # configuration-layer-path ).
Vous pouvez l'utiliser comme suit,
mysql_lib = lb.LayerVersion(self, 'mysql', code = lb.AssetCode('lambda/layers/'), compatible_runtimes = [lb.Runtime.PYTHON_3_6], ) my_lambda = lb.Function( self, 'core-lambda-function', runtime=lb.Runtime.PYTHON_3_6, code=lb.InlineCode(handler_code), function_name="lambda_function_name", handler="index.handler", layers = [mysql_lib], timeout=core.Duration.seconds(300), )