[Python] Return Data from Lambda backed custom resource CDK

Discussão em 'Python' iniciado por Stack, Junho 23, 2025.

  1. Stack

    Stack Membro Participativo

    It cannot be this hard, I have a custom resource class and for the life of me cannot figure out how to access the data and move on.

    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
    })



    I cannot figure out how to get any information out of this sucker once it's made

    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)



    Ultimately I want the physical resourceID, the way I'm going about it is passing it back in the provider lambda in the Data field of the response. In order to get the data I need to do a getAttr, in order to do that I need the logicalID

    Cannot for the life of me figure out how to get either the physical or more importantly the logical ID of a custom resource construct.

    I tried this

    self.get_logical_id(test)


    and I get this error

    Traceback (most recent call last):
    File "/Users/user/bbot/CDK_PoC/app.py", line 16, in <module>
    CdkPoCStack(app, "DjangoAppStack",
    File "/Users/user/bbot/CDK_PoC/.venv/lib/python3.9/site-packages/jsii/_runtime.py", line 83, in __call__
    inst = super().__call__(*args, **kwargs)
    File "/Users/user/bbot/CDK_PoC/cdk_poc/cdk_poc_stack.py", line 35, in __init__
    print(self.get_logical_id(test.cr))
    File "/Users/user/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/user/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/user/bbot/CDK_PoC/.venv/lib/python3.9/site-packages/jsii/_kernel/__init__.py", line 340, in invoke
    response = self.provider.invoke(
    File "/Users/user/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/user/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

    Continue reading...

Compartilhe esta Página