aws / jsii

jsii allows code in any language to naturally interact with JavaScript classes. It is the technology that enables the AWS Cloud Development Kit to deliver polyglot libraries from a single codebase!
https://aws.github.io/jsii
Apache License 2.0
2.63k stars 244 forks source link

(Go aws-cdk-lib): (Unable to call OverrideLogicalId) #4534

Open matthelliwell2 opened 9 months ago

matthelliwell2 commented 9 months ago

Describe the bug

I am trying to set the logical id for a cdk resource so it doesn't get changed by the cdk, eg for a lambda I am doing:

lambda := awslambda.NewFunction(stack, jsii.String("MyId"),... awscdk.CfnResource(lambda.Node().DefaultChild()).OverrideLogicalId(jsii.String("MyId")) This fails to compile with the error "cannot convert lambda.Node().DefaultChild() (value of type constructs.IConstruct) to type awscdk.CfnResource: constructs.IConstruct does not implement awscdk.CfnResource (missing method AddDeletionOverride)"

Expected Behavior

The code builds successfully and the logical id is overridden.

Current Behavior

The code fails to compile with error "cannot convert lambda.Node().DefaultChild() (value of type constructs.IConstruct) to type awscdk.CfnResource: constructs.IConstruct does not implement awscdk.CfnResource (missing method AddDeletionOverride)"

Reproduction Steps

Create lambda with the awslambda.NewFunction. Try to override the logical id with the code

awscdk.CfnResource(lambda.Node().DefaultChild()).OverrideLogicalId(jsii.String("MyId"))

Possible Solution

No response

Additional Information/Context

This isn't limited to lambdas but looks like a more general type problem. Possibly I am just doing something wrong but I can't see how to do a safe cast to call OverrideLogicalId.

CDK CLI Version

2.114.1 (build 02bbb1d)

Framework Version

No response

Node.js Version

v20.10.0

OS

MacOs 14.2

Language

Go

Language Version

1.21

Other information

No response

pahud commented 9 months ago

We need to improve more document on CDK in Golang and I can't answer that off the top of my head.

But this should work like this if in TypeScript

    const fn = getLambdaFunction(this);
    const cfnfn = fn.node.tryFindChild('Resource') as lambda.CfnFunction
    cfnfn.overrideLogicalId('MyId');

And when you run cdk diff you should see MyId in the output.

[+] AWS::Lambda::Function Func MyId 
matthelliwell2 commented 9 months ago

That doesn't work. This code

cfnfn := awslambda.CfnFunction(fn.Node().TryFindChild(jsii.String("Resource")))

fails compilation with cannot convert lambda.Node().TryFindChild(jsii.String("Resource")) (value of type constructs.IConstruct) to type awslambda.CfnFunction: constructs.IConstruct does not implement awslambda.CfnFunction (missing method AddDeletionOverride)

However, I did get it working by writing by own copy of getDefaultChild:

overrideLogicalId(fn.Node())

func overrideLogicalId(node constructs.Node) {
    if node.Id() != nil {
        if dc := getDefaultChild(node); dc != nil {
            dc.OverrideLogicalId(node.Id())
        }
    }
}

func getDefaultChild(node constructs.Node) awscdk.CfnElement {
    var returns awscdk.CfnElement
    _jsii_.Get(
        node,
        "defaultChild",
        &returns,
    )
    return returns
}
pahud commented 3 months ago

This seems to be relevant to JSII rather than CDK itself. Transferring to jsii repo.