代码之家  ›  专栏  ›  技术社区  ›  Jasonca1

如何将AssetCode添加到CfFunction类

  •  0
  • Jasonca1  · 技术社区  · 3 年前

    我希望能够创建一个CfFunction,但能够为 code 参数通常,使用更高级别 Function 构造由提供 aws_cdk.aws_lambda ,这就足够了:

    class MyFunction(Stack):
        def __init__(self, scope, id, **kwargs):
            super().__init__(scope, id, **kwargs)
    
            self._python_function = Function(
                self,
                id="PythonFunction",
                runtime=Runtime.PYTHON_3_9,
                handler="app.main.lambda_handler",
                timeout=Duration.minutes(3),
                code=Code.from_asset(
                    path="/path/to/my/function",
                    bundling=BundlingOptions(
                        image=Runtime.PYTHON_3_9.bundling_image,
                        command=[
                            "bash",
                            "-c",
                            "pip install -r requirements.txt -t /asset-output && cp -au . /asset-output",
                        ],
                    ),
                ),
                memory_size=128,
                log_retention=RetentionDays.TWO_WEEKS,
            )
    

    然而,当试图使用较低级别的CfFunction构造来实现这一点时,文档没有提供关于如何传入AssetCode类型的明确示例:

    class MyFunctionWrapper(Stack):
    
        def __init__(
                self,
                scope,
                id,
                **kwargs,
    
        ):
            super().__init__(scope, id, **kwargs)
    
            code_asset: AssetCode = Code.from_asset(
                path="path/to/my/code",
                bundling=BundlingOptions(
                    image=Runtime.PYTHON_3_9.bundling_image,
                    command=[
                        "bash",
                        "-c",
                        "pip install -r requirements.txt -t /asset-output && cp -au . /asset-output",
                    ],
                ),
            )
    
            self._role = Role(
                self,
                id=f"{id}FunctionRole",
                managed_policies=[
                    ManagedPolicy.from_managed_policy_arn(
                        self,
                        id="PolicyArn",
                        managed_policy_arn="arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
                    )
                ],
                assumed_by=ServicePrincipal("lambda.amazonaws.com"),
            )
    
            self._function = CfnFunction(
                self,
                id="Function",
                runtime="PYTHON_3_9",
                handler="app.handler",
                timeout=60,
                memory_size=128,
                role=self._role.role_name,
                code=code_asset,  # <- This is incorrect type for CfnFunction
            )
    
            self._log_group = LogGroup(
                self,
                id=f"{id}LogGroup",
                log_group_name=f"/aws/lambda/{self._function.function_name}",
                retention=RetentionDays.FIVE_DAYS,
            )
    
    
    
    0 回复  |  直到 3 年前
        1
  •  1
  •   kichik    3 年前

    我不知道你为什么会选择使用 CfnFunction 结束 Function ,但您可以查看 source code 看看它有什么作用。它通过水桶和钥匙 code 绑定后。翻译成Python应该是这样的:

    bound_code = code_asset.bind(self)
    
    self._function = CfnFunction(
        self,
        id="Function",
        runtime="PYTHON_3_9",
        handler="app.handler",
        timeout=60,
        memory_size=128,
        role=self._role.role_name,
        code=CfnFunction.CodeProperty(
            s3_bucket=bound_code.s3_location.bucket_name,
            s3_key=bound_code.s3_location.object_key,
            s3_object_version=bound_code.s3_location.object_version,
        ),
    )