Script to Expire AWS ECR repository Docker images | How to set ECR lifecycle policy via script

Why this post?

{     
"rules": [
{
"rulePriority": integer,
"description": "string",
"selection": {
"tagStatus": "tagged"|"untagged"|"any",
"tagPrefixList": list<string>,
"countType": "imageCountMoreThan"|"sinceImagePushed",
"countUnit": "string",
"countNumber": integer
},
"action": {
"type": "expire"
}
}
]
}

The Script

{
"rules": [
{
"rulePriority": 1,
"description": "Expire images more than 5",
"selection": {
"tagStatus": "any",
"countType": "imageCountMoreThan",

"countNumber": 5
},
"action": {
"type": "expire"
}
}
]
}
#!/bin/bash
# An anilaugustinechalissery initiative ;)
read -p "Enter the aws profile name please: " profile
export AWS_PROFILE=$profile
echo "aws profile is "$AWS_PROFILE
aws ecr describe-repositories --output yaml --query 'repositories[*].repositoryName[]' | awk '{print $2}' > repolist.txt
echo "Repo list "
echo "========================================="
cat repolist.txt
echo "========================================="
echo "to cancel Ctrl + z in 10s"
sleep 10s
for i in $(cat repolist.txt)
do
echo "Setting lifecycle policy for "$i
aws ecr put-lifecycle-policy --repository-name $i --lifecycle-policy-text "file://policy.json"
done

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Anil Augustine Chalissery

Anil Augustine Chalissery

DevOps Engineer | AWS Enthusiast | Analyse -> Solve -> Automate