Here introduces some useful cli snippets & scenarios when using awscli. Presentation (Korean) presented on 2022-11-14.
- First awscli output
- Using output option
- Extract fields (jq)
- Extract fields (query)
- More usecases
- Referenecs
The example below is a bit poor to use. JSON is great, but a bit tricky to deal with in the world of shell scripting.
$ aws ecr list-images --repository-name curl
{
"imageIds": [
{
"imageDigest": "sha256:71cf92d...",
"imageTag": "v1.5"
},
{
"imageDigest": "sha256:5ddbcd0",
"imageTag": "infinity"
}
]
}
There is an option to change output in plain text or yaml, but it is still a bit difficult.
$ aws ecr list-images --repository-name curl --output text
IMAGEIDS sha256:71cf92d... v1.5
IMAGEIDS sha256:5ddbcd0... infinity
$ aws ecr list-images --repository-name curl --output yaml
imageIds:
- imageDigest: sha256:71cf92d...
imageTag: v1.5
- imageDigest: sha256:5ddbcd0...
imageTag: infinity
Using jq? However, CI/CD may not have jq. You may not want to install jq separately. Is there any simpler way?
$ aws ecr list-images --repository-name curl | jq '.imageIds[].imageTag'
"v1.5"
"infinity"
That's the awscli query option.
$ aws ecr list-images --repository-name curl --query 'imageIds[].imageTag'
[
"v1.5",
"infinity"
]
$ aws ecr list-images --repository-name curl --query 'imageIds[].[imageTag]' --output text
v1.5
infinity
Great.
$ for arn in $(aws ecr describe-repositories \
--query 'repositories[].[repositoryArn]' \
--output text); do
echo "[info] processing $arn"
aws ecr tag-resource --resource-arn "$arn" --tags "user:Company=global"
done
[info] processing arn:aws:ecr:ap-northeast-2:***:repository/curl
[info] processing arn:aws:ecr:ap-northeast-2:***:repository/openjdk
...
Below document contains all about awscli output filtering ways. There also awscli server-side filtering is available. (when the api supports)