jsii icon indicating copy to clipboard operation
jsii copied to clipboard

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

Open matthelliwell2 opened this issue 2 years ago • 3 comments

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

matthelliwell2 avatar Dec 15 '23 15:12 matthelliwell2

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 

pahud avatar Dec 15 '23 18:12 pahud

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
}

matthelliwell2 avatar Dec 18 '23 10:12 matthelliwell2

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

pahud avatar Jun 04 '24 14:06 pahud