66 region = " us-east-1"
77 name = " ecr-ex-${ replace (basename (path. cwd ), " _" , " -" )} "
88
9+ account_id = data. aws_caller_identity . current . account_id
10+
911 tags = {
1012 Name = local.name
1113 Example = local.name
@@ -14,7 +16,6 @@ locals {
1416}
1517
1618data "aws_caller_identity" "current" {}
17- data "aws_partition" "current" {}
1819
1920# ###############################################################################
2021# ECR Repository
@@ -101,16 +102,25 @@ data "aws_iam_policy_document" "registry" {
101102 statement {
102103 principals {
103104 type = " AWS"
104- identifiers = [" arn:${ data . aws_partition . current . partition } :iam::${ data . aws_caller_identity . current . account_id } :root" ]
105+ identifiers = [" arn:aws :iam::${ local . account_id } :root" ]
105106 }
106107
107- actions = [
108- " ecr:ReplicateImage" ,
109- ]
108+ actions = [" ecr:ReplicateImage" ]
109+ resources = [module . ecr . repository_arn ]
110+ }
111+
112+ statement {
113+ sid = " dockerhub"
110114
111- resources = [
112- module . ecr . repository_arn ,
115+ principals {
116+ type = " AWS"
117+ identifiers = [" arn:aws:iam::${ local . account_id } :root" ]
118+ }
119+ actions = [
120+ " ecr:CreateRepository" ,
121+ " ecr:BatchImportUpstreamImage"
113122 ]
123+ resources = [" arn:aws:ecr-public::${ local . account_id } :repository/dockerhub/*" ]
114124 }
115125}
116126
@@ -129,6 +139,11 @@ module "ecr_registry" {
129139 ecr_repository_prefix = " ecr-public"
130140 upstream_registry_url = " public.ecr.aws"
131141 }
142+ dockerhub = {
143+ ecr_repository_prefix = " dockerhub"
144+ upstream_registry_url = " registry-1.docker.io"
145+ credential_arn = module.secrets_manager_dockerhub_credentials.secret_arn
146+ }
132147 }
133148
134149 # Registry Scanning Configuration
@@ -159,22 +174,53 @@ module "ecr_registry" {
159174
160175 # Registry Replication Configuration
161176 create_registry_replication_configuration = true
162- registry_replication_rules = [
163- {
164- destinations = [{
165- region = " us-west-2"
166- registry_id = data.aws_caller_identity.current.account_id
167- }, {
168- region = " eu-west-1"
169- registry_id = data.aws_caller_identity.current.account_id
170- }]
177+ registry_replication_rules = [{
178+ destinations = [{
179+ region = " us-west-2"
180+ registry_id = local.account_id
181+ }, {
182+ region = " eu-west-1"
183+ registry_id = local.account_id
184+ }]
185+
186+ repository_filters = [{
187+ filter = " prod-microservice"
188+ filter_type = " PREFIX_MATCH"
189+ }]
190+ }]
191+
192+ tags = local. tags
193+ }
171194
172- repository_filters = [{
173- filter = " prod-microservice"
174- filter_type = " PREFIX_MATCH"
195+ module "secrets_manager_dockerhub_credentials" {
196+ source = " terraform-aws-modules/secrets-manager/aws"
197+ version = " ~> 1.0"
198+
199+ # Secret names must contain 1-512 Unicode characters and be prefixed with ecr-pullthroughcache/
200+ name_prefix = " ecr-pullthroughcache/dockerhub-credentials"
201+ description = " Dockerhub credentials"
202+
203+ # For example only
204+ recovery_window_in_days = 0
205+ secret_string = jsonencode ({
206+ username = " example"
207+ accessToken = " YouShouldNotStoreThisInPlainText"
208+ })
209+
210+ # Policy
211+ create_policy = true
212+ block_public_policy = true
213+ policy_statements = {
214+ read = {
215+ sid = " AllowAccountRead"
216+ principals = [{
217+ type = " AWS"
218+ identifiers = [" arn:aws:iam::${ local . account_id } :root" ]
175219 }]
220+ actions = [" secretsmanager:GetSecretValue" ]
221+ resources = [" *" ]
176222 }
177- ]
223+ }
178224
179225 tags = local. tags
180226}
0 commit comments