Skip to content

Instantly share code, notes, and snippets.

@afro-coder
Created April 18, 2022 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afro-coder/ddcb578dc6048f75071399b2001fbd3f to your computer and use it in GitHub Desktop.
Save afro-coder/ddcb578dc6048f75071399b2001fbd3f to your computer and use it in GitHub Desktop.
ECR create repo files Terraform services
#!/bin/bash
# Usage./create-ecr.sh create repo-name
set -x
set -e
ECR_IMAGE_NAME=""
if [ "$1" = "" ];
then
echo "No Args passed"
exit 1
fi
if [ "$#" -eq 1 ] ; then
echo "Image name not passed"
exit 1;
else
ECR_IMAGE_NAME="$2"
fi
if [ "$1" = "create" ];
then
if [ "$ECR_IMAGE_NAME" = "" ]; then echo "Specify ECR image name";exit 2;fi
#touch $(pwd)/{terraform.tf,terraform.tfvars,main.tf,outputs.tf,variables.tf}
if test -f "$(pwd)/terraform.tf";then
echo "terraform.tf found skipping..."
else
echo "Creating terraform.tf"
cat <<EOF > "$(pwd)/terraform.tf"
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "4.4.0"
}
}
backend "s3" {
key = "ecr/$ECR_IMAGE_NAME/terraform.tfstate"
bucket = "prod-tfstate"
encrypt = true
profile = "yourtf-tf"
region = "eu-central-1"
dynamodb_endpoint = "dynamodb.eu-central-1.amazonaws.com"
dynamodb_table = "prod-terraform-state-lock"
}
}
provider "aws" {
region = "eu-central-1"
profile = "yourtf-tf"
}
EOF
fi
#touch "$(pwd)"/{outputs.tf,main.tf,terraform.tfvars,variables.tf}
echo "Creating Outputs.tf"
cat <<EOF > "$(pwd)/outputs.tf"
output "ecr_repo_name" {
value = aws_ecr_repository.ecr-repo.name
}
output "ecr_repo_url" {
value = aws_ecr_repository.ecr-repo.repository_url
}
EOF
echo "Creating variables.tf"
cat <<EOF > "$(pwd)/variables.tf"
variable "ecr-repository-name" {}
variable "tagged-image-count" {}
variable "untagged-image-days" {}
variable "tags" {}
EOF
echo "Creating terraform.tfvars"
cat <<EOF > "$(pwd)/terraform.tfvars"
ecr-repository-name = "$ECR_IMAGE_NAME"
tagged-image-count = 20
untagged-image-days = 14
tags = {
Terraform = true
Environment = "Production"
}
EOF
echo "Creating main.tf"
cat <<'EOD' > "$(pwd)/main.tf"
resource "aws_ecr_repository" "ecr-repo" {
name = var.ecr-repository-name
image_tag_mutability = "MUTABLE"
image_scanning_configuration {
scan_on_push = true
}
tags = merge(var.tags)
}
resource "aws_ecr_lifecycle_policy" "ecr-repo-policy" {
repository = aws_ecr_repository.ecr-repo.name
policy = <<EOF
{
"rules": [
{
"rulePriority": 1,
"description": "Keep last 30 images",
"selection": {
"tagStatus": "tagged",
"tagPrefixList": ["latest"],
"countType": "imageCountMoreThan",
"countNumber": ${var.tagged-image-count}
},
"action": {
"type": "expire"
}
},
{
"rulePriority": 2,
"description": "Expire untagged images older than 14 days",
"selection": {
"tagStatus": "untagged",
"countType": "sinceImagePushed",
"countUnit": "days",
"countNumber": ${var.untagged-image-days}
},
"action": {
"type": "expire"
}
}
]
}
EOF
}
EOD
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment