如何将 AssetCode 添加到 CfnFunction class
How to add AssetCode to to a CfnFunction class
我希望能够创建一个 CfnFunction,但能够为 code
参数传入一个 AssetCode class。通常,使用 aws_cdk.aws_lambda
提供的更高级别 Function
构造,这就足够了:
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,
)
但是,当尝试使用较低级别的 CfnFunction 构造执行此操作时,文档未提供有关如何传入 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,
)
我不确定您为什么会选择使用 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,
),
)
我希望能够创建一个 CfnFunction,但能够为 code
参数传入一个 AssetCode class。通常,使用 aws_cdk.aws_lambda
提供的更高级别 Function
构造,这就足够了:
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,
)
但是,当尝试使用较低级别的 CfnFunction 构造执行此操作时,文档未提供有关如何传入 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,
)
我不确定您为什么会选择使用 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,
),
)