generated from clowdhaus/terraform-aws-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathmain.tf
209 lines (174 loc) · 4.96 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
provider "aws" {
region = local.region
}
locals {
region = "us-east-1"
name = "ecr-ex-${replace(basename(path.cwd), "_", "-")}"
account_id = data.aws_caller_identity.current.account_id
tags = {
Name = local.name
Example = local.name
Repository = "https://github.com/terraform-aws-modules/terraform-aws-ecr"
}
}
data "aws_caller_identity" "current" {}
################################################################################
# ECR Repository
################################################################################
module "ecr_disabled" {
source = "../.."
create = false
}
module "ecr" {
source = "../.."
repository_name = local.name
repository_read_write_access_arns = [data.aws_caller_identity.current.arn]
create_lifecycle_policy = true
repository_lifecycle_policy = jsonencode({
rules = [
{
rulePriority = 1,
description = "Keep last 30 images",
selection = {
tagStatus = "tagged",
tagPrefixList = ["v"],
countType = "imageCountMoreThan",
countNumber = 30
},
action = {
type = "expire"
}
}
]
})
repository_force_delete = true
tags = local.tags
}
module "public_ecr" {
source = "../.."
repository_name = local.name
repository_type = "public"
repository_read_write_access_arns = [data.aws_caller_identity.current.arn]
public_repository_catalog_data = {
description = "Docker container for some things"
about_text = file("${path.module}/files/ABOUT.md")
usage_text = file("${path.module}/files/USAGE.md")
operating_systems = ["Linux"]
architectures = ["x86"]
logo_image_blob = filebase64("${path.module}/files/clowd.png")
}
tags = local.tags
}
################################################################################
# ECR Registry
################################################################################
data "aws_iam_policy_document" "registry" {
statement {
principals {
type = "AWS"
identifiers = ["arn:aws:iam::${local.account_id}:root"]
}
actions = ["ecr:ReplicateImage"]
resources = [module.ecr.repository_arn]
}
statement {
sid = "dockerhub"
principals {
type = "AWS"
identifiers = ["arn:aws:iam::${local.account_id}:root"]
}
actions = [
"ecr:CreateRepository",
"ecr:BatchImportUpstreamImage"
]
resources = ["arn:aws:ecr-public::${local.account_id}:repository/dockerhub/*"]
}
}
module "ecr_registry" {
source = "../.."
create_repository = false
# Registry Policy
create_registry_policy = true
registry_policy = data.aws_iam_policy_document.registry.json
# Registry Pull Through Cache Rules
registry_pull_through_cache_rules = {
pub = {
ecr_repository_prefix = "ecr-public"
upstream_registry_url = "public.ecr.aws"
}
dockerhub = {
ecr_repository_prefix = "dockerhub"
upstream_registry_url = "registry-1.docker.io"
credential_arn = module.secrets_manager_dockerhub_credentials.secret_arn
}
}
# Registry Scanning Configuration
manage_registry_scanning_configuration = true
registry_scan_type = "ENHANCED"
registry_scan_rules = [
{
scan_frequency = "SCAN_ON_PUSH"
filter = [
{
filter = "example1"
filter_type = "WILDCARD"
},
{ filter = "example2"
filter_type = "WILDCARD"
}
]
}, {
scan_frequency = "CONTINUOUS_SCAN"
filter = [
{
filter = "example"
filter_type = "WILDCARD"
}
]
}
]
# Registry Replication Configuration
create_registry_replication_configuration = true
registry_replication_rules = [{
destinations = [{
region = "us-west-2"
registry_id = local.account_id
}, {
region = "eu-west-1"
registry_id = local.account_id
}]
repository_filters = [{
filter = "prod-microservice"
filter_type = "PREFIX_MATCH"
}]
}]
tags = local.tags
}
module "secrets_manager_dockerhub_credentials" {
source = "terraform-aws-modules/secrets-manager/aws"
version = "~> 1.0"
# Secret names must contain 1-512 Unicode characters and be prefixed with ecr-pullthroughcache/
name_prefix = "ecr-pullthroughcache/dockerhub-credentials"
description = "Dockerhub credentials"
# For example only
recovery_window_in_days = 0
secret_string = jsonencode({
username = "example"
accessToken = "YouShouldNotStoreThisInPlainText"
})
# Policy
create_policy = true
block_public_policy = true
policy_statements = {
read = {
sid = "AllowAccountRead"
principals = [{
type = "AWS"
identifiers = ["arn:aws:iam::${local.account_id}:root"]
}]
actions = ["secretsmanager:GetSecretValue"]
resources = ["*"]
}
}
tags = local.tags
}