Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
matthelliwell2 opened this issue Dec 15, 2023 · 3 comments
Open

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

matthelliwell2 opened this issue Dec 15, 2023 · 3 comments
Labels
bug This issue is a bug. documentation This is a problem with documentation. effort/medium Medium work item – a couple days of effort language/go Regarding GoLang bindings p2

Comments

@matthelliwell2
Copy link

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 matthelliwell2 added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Dec 15, 2023
@pahud
Copy link
Contributor

pahud commented Dec 15, 2023

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 pahud added p2 documentation This is a problem with documentation. language/go Regarding GoLang bindings effort/medium Medium work item – a couple days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Dec 15, 2023
@matthelliwell2
Copy link
Author

matthelliwell2 commented Dec 18, 2023

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
Copy link
Contributor

pahud commented Jun 4, 2024

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

@pahud pahud transferred this issue from aws/aws-cdk Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. documentation This is a problem with documentation. effort/medium Medium work item – a couple days of effort language/go Regarding GoLang bindings p2
Projects
None yet
Development

No branches or pull requests

2 participants