Cdktf golang - how to expose arn value?

Greetings,

I am trying to wrap my head around how to retrieve the “arn” property of a resource I’m declaring in cdktf (golang specifically). I’ll try to keep the example code succinct.

(for reference tfaws is aliasing the tfaws "cdk.tf/go/stack/generated/hashicorp/aws" import)

func BuildIamRoleWithConfig(stack cdktf.TerraformStack, config tfaws.IamRoleConfig, id string) tfaws.IamRole {
	role := tfaws.NewIamRole(stack, jsii.String(id), &config)
	/*
	the following throws a 
	Error: Missing resource instance key error. 
	Because aws_iam_role.control-plane-iam has "count" set, its attributes must be accessed on specific instances.
	*/
	test := role.Arn()
	
	return role
}

Could someone break down how I am meant to retrieve the arn from a declared IamRole interface?

Thank you,

Hi @caleb.williams!

did you set a count in the config passed to your BuildIamRoleWithConfig function?
When using CDKTF you’d usually construct multiple instances of the same resources using a e.g. a loop in Go instead of count which you’d have to use when writing HCL.

That said, there are special cases where you need count. If that is the case you need to specify the index for the resource to retrieve. You can then construct the address manually like this: jsii.String("${" + *role.Fqn() + "[0].arn}")
Full example: test-cdktf-go-count/main.go at main · ansgarm/test-cdktf-go-count · GitHub

Hope that clarifies things a bit
Ansgar

1 Like

Hey @ansgarm!

Thanks for the reply - to answer your question, no I didn’t explicitly set the count, however, I believe it automagically gets set when creating the resource.
Thank you for the example, it was really helpful!

Hi @caleb.williams!

If it automatically sets count that’s a bug :sweat_smile: If you can reproduce that in a small example, we’d be happy for an issue on that :slight_smile:

Ansgar