1

trying to run a aws cli call as a terraform external data but getting a error for unmarshal string

Terraform:

data "external" "cognito" {
program = ["sh", "-c", "aws cognito-idp list-user-pool-clients --user-pool-id eu-west-xxx | jq '.UserPoolClients | .[] | select(.ClientName | contains(\"AmazonOpenSearchService\")) | .ClientId'"] 
}

running the command directly from bash returns:

"123456"

Via terraform errors with:

│ The data source received unexpected results after executing the program.
│ 
│ Program output must be a JSON encoded map of string keys and string values.
│ 
│ If the error is unclear, the output can be viewed by enabling Terraform's logging at TRACE level. Terraform documentation on logging: https://www.terraform.io/internals/debugging
│ 
│ Program: /bin/sh
│ Result Error: json: │ The data source received unexpected results after executing the program.
│ 
│ Program output must be a JSON encoded map of string keys and string values.
│ 
│ If the error is unclear, the output can be viewed by enabling Terraform's logging at TRACE level. Terraform documentation on logging: https://www.terraform.io/internals/debugging
│ 
│ Program: /bin/sh
│ Result Error: json: cannot unmarshal string into Go value of type map[string]string
3
  • Why would you do that, when this exists? registry.terraform.io/providers/hashicorp/aws/latest/docs/… Commented Aug 26, 2022 at 16:34
  • Hey Mark, no as that sadly brings out 2 lists one for ids and one for names, so i cant filter based on the name (like i am in the aws cli call with jq) Commented Aug 30, 2022 at 9:45
  • Got it, yeah that data provider isn't very useful now that I look at it. Commented Aug 30, 2022 at 12:15

1 Answer 1

1

When running an ad hoc script using the Terraform external data source you need to return a valid json object (The docs also mention a map of string) which "123456" is not. You'd need something such as '{"clientId":"123456"}'.

If you want to avoid the script completely, what Mark B suggested in his comment above is the best way to go about it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.