To use aws cli, its common to invoke built-in features like --filters
and --query
however they aren't avaiable for all the commands.
for my use case i wanted to get a role matching a name, i.e. regex pattern . that was very easy using jq
if i want to get a role matching name eksctl-dev-cluster-addon-iamserviceaccount
then to get ARN
of that role is simply doing
aws iam list-roles | jq -r '.Roles[] | select(.RoleName|match("eksctl-dev-cluster-addon-iamserviceaccount.")) | .Arn'
aws iam list-policies | jq -r '.Policies[] | select(.PolicyName|match("kube-external-secrets")) | .Arn'
arn:aws:iam::<ID>:policy/kube-external-secrets
one-liner without jq(using built-in JMESPath https://jmespath.org/tutorial.html )
aws iam list-policies --query 'Policies[?PolicyName!=`null`]|[?starts_with(PolicyName,`kube-external-secrets`)==`true`][].[Arn]' --output text
arn:aws:iam::<ID>:policy/kube-external-secrets
Hi,
Now, as of aws provider version 3.55.0, we have a data source to get the functions.
"Use this data source to get the ARNs and names of IAM functions."
https://registry.terraform.io/providers/hashicorp/aws/3.55.0/docs/data-sources/iam_roles