|
| 1 | +provider "aws" { |
| 2 | + region = local.region |
| 3 | +} |
| 4 | + |
| 5 | +locals { |
| 6 | + region = "us-east-1" |
| 7 | + name = "ecr-ex-${basename(path.cwd)}" |
| 8 | + |
| 9 | + account_id = data.aws_caller_identity.current.account_id |
| 10 | + |
| 11 | + tags = { |
| 12 | + Name = local.name |
| 13 | + Example = local.name |
| 14 | + Repository = "https://github.com/terraform-aws-modules/terraform-aws-ecr" |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +data "aws_caller_identity" "current" {} |
| 19 | + |
| 20 | +################################################################################ |
| 21 | +# ECR Repository Template |
| 22 | +################################################################################ |
| 23 | + |
| 24 | +module "public_ecr_pull_through_cache_repository_template" { |
| 25 | + source = "../../modules/repository-template" |
| 26 | + |
| 27 | + # Template |
| 28 | + description = "Pull through cache repository template for Public ECR artifacts" |
| 29 | + prefix = "ecr-public" |
| 30 | + resource_tags = local.tags |
| 31 | + lifecycle_policy = jsonencode({ |
| 32 | + rules = [ |
| 33 | + { |
| 34 | + rulePriority = 1, |
| 35 | + description = "Keep last 30 images", |
| 36 | + selection = { |
| 37 | + tagStatus = "tagged", |
| 38 | + tagPrefixList = ["v"], |
| 39 | + countType = "imageCountMoreThan", |
| 40 | + countNumber = 30 |
| 41 | + }, |
| 42 | + action = { |
| 43 | + type = "expire" |
| 44 | + } |
| 45 | + } |
| 46 | + ] |
| 47 | + }) |
| 48 | + |
| 49 | + # Pull through cache rule |
| 50 | + create_pull_through_cache_rule = true |
| 51 | + upstream_registry_url = "public.ecr.aws" |
| 52 | + |
| 53 | + tags = local.tags |
| 54 | +} |
| 55 | + |
| 56 | +module "dockerhub_pull_through_cache_repository_template" { |
| 57 | + source = "../../modules/repository-template" |
| 58 | + |
| 59 | + # Template |
| 60 | + description = "Pull through cache repository template for Dockerhub artifacts" |
| 61 | + prefix = "docker-hub" |
| 62 | + resource_tags = local.tags |
| 63 | + |
| 64 | + # Pull through cache rule |
| 65 | + create_pull_through_cache_rule = true |
| 66 | + upstream_registry_url = "registry-1.docker.io" |
| 67 | + credential_arn = module.secrets_manager_dockerhub_credentials.secret_arn |
| 68 | + |
| 69 | + tags = local.tags |
| 70 | +} |
| 71 | + |
| 72 | +module "disabled" { |
| 73 | + source = "../../modules/repository-template" |
| 74 | + |
| 75 | + create = false |
| 76 | +} |
| 77 | + |
| 78 | +################################################################################ |
| 79 | +# Supporting Resources |
| 80 | +################################################################################ |
| 81 | + |
| 82 | +module "secrets_manager_dockerhub_credentials" { |
| 83 | + source = "terraform-aws-modules/secrets-manager/aws" |
| 84 | + version = "~> 1.0" |
| 85 | + |
| 86 | + # Secret names must contain 1-512 Unicode characters and be prefixed with ecr-pullthroughcache/ |
| 87 | + name_prefix = "ecr-pullthroughcache/dockerhub-credentials" |
| 88 | + description = "Dockerhub credentials" |
| 89 | + |
| 90 | + # For example only |
| 91 | + recovery_window_in_days = 0 |
| 92 | + secret_string = jsonencode({ |
| 93 | + username = "example" |
| 94 | + accessToken = "YouShouldNotStoreThisInPlainText" |
| 95 | + }) |
| 96 | + |
| 97 | + # Policy |
| 98 | + create_policy = true |
| 99 | + block_public_policy = true |
| 100 | + policy_statements = { |
| 101 | + read = { |
| 102 | + sid = "AllowAccountRead" |
| 103 | + principals = [{ |
| 104 | + type = "AWS" |
| 105 | + identifiers = ["arn:aws:iam::${local.account_id}:root"] |
| 106 | + }] |
| 107 | + actions = ["secretsmanager:GetSecretValue"] |
| 108 | + resources = ["*"] |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + tags = local.tags |
| 113 | +} |
0 commit comments