Get ALL CodeCommit repository ARNs

76 Views Asked by At

I want to get a list of ARNs of every repository that exists in my account using Terraform.

I know I can use aws_codecommit_repository data source to get arn of a specific repository by specifying repository_name.

But how can I get arns of every repository without specifying any particular repository name? Is it possible?

1

There are 1 best solutions below

2
Paolo On

Whenever you don't find a data source that does what you need you have to resort to an external data source.

To get all code commit repository ARNs, you may use:

util.sh:

#!/bin/bash
repo_arns=$(aws codecommit list-repositories --query 'repositories[].repositoryName' --output text \
  | tr '\t' '\n' \
  | xargs -I {} aws codecommit get-repository --repository-name {} --query 'repositoryMetadata.Arn' --output text)
jq -r -n --arg repo_arns "$repo_arns" '{"repo_arns":($repo_arns|split("\n")|tojson)}'

main.tf:

data "external" "util" {
  program = ["bash","util.sh"]
}

output "arns" {
  value = jsondecode(data.external.util.result.repo_arns)
}
$ terraform apply
...
Outputs:

arns = [
  "arn:aws:codecommit:us-east-1:...:bar",
  "arn:aws:codecommit:us-east-1:...:foo",
]

Note that this requires jq installed.