从Lambda支持的自定义资源CDK返回数据

2024-05-13 23:39:05 发布

您现在位置:Python中文网/ 问答频道 /正文

这不可能这么难,我有一个定制的资源类,我一辈子都不知道如何访问数据并继续前进

from typing_extensions import runtime
import os
from aws_cdk import (
    core as cdk, aws_iam as iam, aws_lambda as lamb, aws_route53 as route53, aws_logs as logs,
    custom_resources as cr
)

import builtins

from jsii import python


class hz_with_delegation(cdk.Construct):
    def __init__(self, scope: cdk.Construct, id: builtins.str, delSet: builtins.str, env: builtins.str) -> builtins.any:
        super().__init__(scope, id)

        self.hostedZoneLambdaRole = iam.Role(
            self, "hzlambdarole", assumed_by=iam.ServicePrincipal('lambda.amazonaws.com'))
        self.hostedZoneLambdaRole.add_to_policy(
            iam.PolicyStatement(resources=["*"], actions=["route53:*"])
        )

        self.hzResourceLambda = lamb.Function(self, "hz_custom_resource_handler",
                                              runtime=lamb.Runtime.PYTHON_3_8,
                                              handler="main.on_event",
                                              role=self.hostedZoneLambdaRole,
                                              code=lamb.Code.from_asset(
                                                  os.path.dirname(__file__) + "/lambda"),
                                              log_retention=logs.RetentionDays.ONE_DAY
                                              )

        self.hz_with_delegation_provider = cr.Provider(self, "hz_with_delegation_set",
                                                       on_event_handler=self.hzResourceLambda,

                                                       )
        self.cr = cdk.CustomResource(self, "hz_with_delegation",
                                     service_token=self.hz_with_delegation_provider.service_token,
                                     properties={
                                         "delSet": delSet,
                                         "env": env
                                     })

我不知道如何从这个笨蛋那里得到任何信息

class CdkPoCStack(cdk.Stack):

    def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)
        
# Custom resource to handle the delegation

        test = custom_hosted_zone.hz_with_delegation(
            self, id="some Id", delSet="N06079012Q8954F8DG91Y", env=environment)

最终,我需要物理资源ID,我要做的是将其传递回响应数据字段中的提供者lambda中。为了获取数据,我需要执行getAttr,为了实现这一点,我需要logicalID

我一辈子都搞不懂如何获取自定义资源构造的物理ID或更重要的是逻辑ID

我试过这个

self.get_logical_id(test)

我得到了这个错误

Traceback (most recent call last):
  File "/Users/sahm.samarghandi/bbot/CDK_PoC/app.py", line 16, in <module>
    CdkPoCStack(app, "DjangoAppStack",
  File "/Users/sahm.samarghandi/bbot/CDK_PoC/.venv/lib/python3.9/site-packages/jsii/_runtime.py", line 83, in __call__
    inst = super().__call__(*args, **kwargs)
  File "/Users/sahm.samarghandi/bbot/CDK_PoC/cdk_poc/cdk_poc_stack.py", line 35, in __init__
    print(self.get_logical_id(test.cr))
  File "/Users/sahm.samarghandi/bbot/CDK_PoC/.venv/lib/python3.9/site-packages/aws_cdk/core/__init__.py", line 15945, in get_logical_id
    return typing.cast(builtins.str, jsii.invoke(self, "getLogicalId", [element]))
  File "/Users/sahm.samarghandi/bbot/CDK_PoC/.venv/lib/python3.9/site-packages/jsii/_kernel/__init__.py", line 128, in wrapped
    return _recursize_dereference(kernel, fn(kernel, *args, **kwargs))
  File "/Users/sahm.samarghandi/bbot/CDK_PoC/.venv/lib/python3.9/site-packages/jsii/_kernel/__init__.py", line 340, in invoke
    response = self.provider.invoke(
  File "/Users/sahm.samarghandi/bbot/CDK_PoC/.venv/lib/python3.9/site-packages/jsii/_kernel/providers/process.py", line 359, in invoke
    return self._process.send(request, InvokeResponse)
  File "/Users/sahm.samarghandi/bbot/CDK_PoC/.venv/lib/python3.9/site-packages/jsii/_kernel/providers/process.py", line 326, in send
    raise JSIIError(resp.error) from JavaScriptError(resp.stack)
jsii.errors.JSIIError: Object of type @aws-cdk/core.CustomResource is not convertible to @aws-cdk/core.CfnElement

Tags: pyselfawsidinitlineusersfile