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

Get one element from module output list(target_group_arns of terraform-aws-modules/alb/aws) #1087

Closed
Anton7K opened this issue Sep 28, 2021 · 4 comments · Fixed by #1725
Closed
Labels
bug Something isn't working feature/tokens needs-research priority/important-longterm Medium priority, to be worked on within the following 1-2 business quarters.

Comments

@Anton7K
Copy link

Anton7K commented Sep 28, 2021

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

cdktf & Language Versions

cdktf 0.6.3
cdktf-cdktf-provider-aws 2.0.11
terraform-aws-modules/alb/aws@5.16.0
Python 3.8.10

Affected Resource(s)

I created several aws albs via module and want to get single element from their output list (target_group_arns) to create subsequent resources(ecs services).
I tried different approaches, the last one was as following(for test purposes I hardcoded index values):

lb = self.load_balancers[0]
target_group_arn = Fn.element(Token.as_list(lb.target_group_arns_output), 0)  

Expected Behavior

I thought, that in json output will appear something like:
"target_group_arn": "${element(module.stack-name-lb2.target_group_arns, 0)}"
And then this string will be substituted with needed value during apply

Actual Behavior

I can synthesize code successfully, but during the plan I get several errors like this one:

Error: Invalid character

│ on cdk.tf.json line 2704, in resource.aws_ecs_service.stack-nameecs-cluster-construct-service-33_EEFE3F15.load_balancer[0]:
│ 2704: "target_group_arn": "${element(${module.stack-name-lb2.target_group_arns}, 0)}"

│ This character is not used within the language.


│ Error: Invalid expression

│ on cdk.tf.json line 2704, in resource.aws_ecs_service.stack-nameecs-cluster-construct-service-33_EEFE3F15.load_balancer[0]:
│ 2704: "target_group_arn": "${element(${module.stack-name-lb2.target_group_arns}, 0)}"

│ Expected the start of an expression, but found an invalid expression token.

@Anton7K Anton7K added bug Something isn't working new Un-triaged issue labels Sep 28, 2021
@jsteinich
Copy link
Collaborator

Related to #287 and #525, but does seem to be a standalone issue.

One workaround:

  • Pass lb.fqn + '.target_group_arns' to the element function

@Anton7K
Copy link
Author

Anton7K commented Oct 15, 2021

Thank you very much! It seems like it is working.
But I have one more question: how to deal with nested lists in general and ones in outputs especially?

I divided my configuration in several stacks. One of them has nested list as output. Now I need to get this list from another stack and extract separate elements.

Output variable is in the following format:

nested_output : 
[
	[
		“string…”,
		“string…”
	],
	[	
		“string…”,
		“string…”,
		“string…”,
	],
	... 
] 

When I tried to use nested Fn.element, I got the same error during the plan as previously:

code:

remote_state = DataTerraformRemoteState(
	self,
	"remote_state_1",
	workspaces=NamedRemoteWorkspace(“workspace_name”),
	organization=”org_name”
)
self.nested_output = remote_state.get_list("nested_output")

Fn.element(
	Token.as_list(Fn.element(self.nested_output,0))
	1
)

error:

Error: Invalid character

│ on cdk.tf.json line 97, in output.test:
│ 97: "value": "${element(${element(data.terraform_remote_state.remote_state_1.outputs.nested_output, 0)}, 1)}"

│ This character is not used within the language.


│ Error: Invalid expression

│ on cdk.tf.json line 97, in output.test:
│ 97: "value": "${element(${element(data.terraform_remote_state.remote_state_1.outputs.nested_output, 0)}, 1)}"
│Expected the start of an expression, but found an invalid expression token.

I didn’t find fqn property of DataTerraformRemoteState, so I’m using following solution now:


Fn.element(
              Token.as_list("data.terraform_remote_state.remote_state_1.outputs.nested_output[0]"),
              1)
)

It seems like it is working, but I think it is not the best solution. Are there any other options?

Thank you in advance!

@jsteinich
Copy link
Collaborator

It seems like it is working, but I think it is not the best solution. Are there any other options?

Seems like you're hitting a bug, so the workaround you have is fine.

@ansgarm ansgarm added needs-priority Issue has not yet been prioritized; this will prompt team review and removed new Un-triaged issue labels Nov 4, 2021
@DanielMSchmidt DanielMSchmidt added needs-research priority/important-longterm Medium priority, to be worked on within the following 1-2 business quarters. and removed needs-priority Issue has not yet been prioritized; this will prompt team review labels Dec 9, 2021
@github-actions
Copy link
Contributor

I'm going to lock this issue because it has been closed for 30 days. This helps our maintainers find and focus on the active issues. If you've found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working feature/tokens needs-research priority/important-longterm Medium priority, to be worked on within the following 1-2 business quarters.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants