# ECR repo for lambda_example resource "aws_ecr_repository" "lambda_example" { name = "lambda_example" } # ECR policy to allow Lambda access resource "aws_ecr_repository_policy" "lambda_example_ecr_access" { repository = aws_ecr_repository.lambda_example.name policy = jsonencode({ Version = "2008-10-17", Statement = [{ Sid = "AllowLambdaPull", Effect = "Allow", Principal = { Service = "lambda.amazonaws.com" }, Action = [ "ecr:GetDownloadUrlForLayer", "ecr:BatchGetImage", "ecr:BatchCheckLayerAvailability" ] }] }) } # ECR lifecycle policy to delete tagged images older than 14 days resource "aws_ecr_lifecycle_policy" "lambda_example_ecr_lifecycle" { repository = aws_ecr_repository.lambda_example.name policy = jsonencode({ "rules": [ { "rulePriority": 2, "description": "Expire images older than 14 days", "selection": { "tagStatus": "untagged", "countType": "sinceImagePushed", "countUnit": "days", "countNumber": 1 }, "action": { "type": "expire" } }, { "rulePriority": 1, "description": "Keep last 5 images", "selection": { "tagStatus": "tagged", "tagPrefixList": ["feature"], "countType": "imageCountMoreThan", "countNumber": 5 }, "action": { "type": "expire" } } ] }) }