main.tf
5.21 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
backend "s3" {
bucket = "terraformstatefile090909"
key = "ecr_state.tfstate"
region = "us-east-1"
# dynamodb_table = "terraform-locks"
encrypt = true
}
}
provider "aws" {
region = "us-east-1"
}
# ECR Repository for Frontend
resource "aws_ecr_repository" "ecommerce_frontend" {
name = "ecommerce-frontend"
image_tag_mutability = "MUTABLE"
image_scanning_configuration {
scan_on_push = true
}
encryption_configuration {
encryption_type = "AES256"
}
tags = {
Project = "ecommerce"
Environment = "production"
Component = "frontend"
}
}
# ECR Repository for User Service
resource "aws_ecr_repository" "ecommerce_user_service" {
name = "ecommerce-user-service"
image_tag_mutability = "MUTABLE"
image_scanning_configuration {
scan_on_push = true
}
encryption_configuration {
encryption_type = "AES256"
}
tags = {
Project = "ecommerce"
Environment = "production"
Component = "backend"
Service = "user-service"
}
}
# ECR Repository for Product Service
resource "aws_ecr_repository" "ecommerce_product_service" {
name = "ecommerce-product-service"
image_tag_mutability = "MUTABLE"
image_scanning_configuration {
scan_on_push = true
}
encryption_configuration {
encryption_type = "AES256"
}
tags = {
Project = "ecommerce"
Environment = "production"
Component = "backend"
Service = "product-service"
}
}
# ECR Repository for Order Service
resource "aws_ecr_repository" "ecommerce_order_service" {
name = "ecommerce-order-service"
image_tag_mutability = "MUTABLE"
image_scanning_configuration {
scan_on_push = true
}
encryption_configuration {
encryption_type = "AES256"
}
tags = {
Project = "ecommerce"
Environment = "production"
Component = "backend"
Service = "order-service"
}
}
# ECR Repository for Payment Service
resource "aws_ecr_repository" "ecommerce_payment_service" {
name = "ecommerce-payment-service"
image_tag_mutability = "MUTABLE"
image_scanning_configuration {
scan_on_push = true
}
encryption_configuration {
encryption_type = "AES256"
}
tags = {
Project = "ecommerce"
Environment = "production"
Component = "backend"
Service = "payment-service"
}
}
# ECR Repository for Inventory Service
resource "aws_ecr_repository" "ecommerce_inventory_service" {
name = "ecommerce-inventory-service"
image_tag_mutability = "MUTABLE"
image_scanning_configuration {
scan_on_push = true
}
encryption_configuration {
encryption_type = "AES256"
}
tags = {
Project = "ecommerce"
Environment = "production"
Component = "backend"
Service = "inventory-service"
}
}
# ECR Repository for Notification Service
resource "aws_ecr_repository" "ecommerce_notification_service" {
name = "ecommerce-notification-service"
image_tag_mutability = "MUTABLE"
image_scanning_configuration {
scan_on_push = true
}
encryption_configuration {
encryption_type = "AES256"
}
tags = {
Project = "ecommerce"
Environment = "production"
Component = "backend"
Service = "notification-service"
}
}
# ECR Repository for API Gateway (if needed)
resource "aws_ecr_repository" "ecommerce_api_gateway" {
name = "ecommerce-api-gateway"
image_tag_mutability = "MUTABLE"
image_scanning_configuration {
scan_on_push = true
}
encryption_configuration {
encryption_type = "AES256"
}
tags = {
Project = "ecommerce"
Environment = "production"
Component = "backend"
Service = "api-gateway"
}
}
# ECR Lifecycle Policies
resource "aws_ecr_lifecycle_policy" "ecr_lifecycle_policy" {
for_each = {
frontend = aws_ecr_repository.ecommerce_frontend.repository_url
user = aws_ecr_repository.ecommerce_user_service.repository_url
product = aws_ecr_repository.ecommerce_product_service.repository_url
order = aws_ecr_repository.ecommerce_order_service.repository_url
payment = aws_ecr_repository.ecommerce_payment_service.repository_url
inventory = aws_ecr_repository.ecommerce_inventory_service.repository_url
notification = aws_ecr_repository.ecommerce_notification_service.repository_url
api_gateway = aws_ecr_repository.ecommerce_api_gateway.repository_url
}
repository = each.key
policy = <<EOF
{
"rules": [
{
"rulePriority": 1,
"description": "Keep last 30 images",
"selection": {
"tagStatus": "any",
"countType": "imageCountMoreThan",
"countNumber": 30
},
"action": {
"type": "expire"
}
},
{
"rulePriority": 2,
"description": "Remove untagged images older than 7 days",
"selection": {
"tagStatus": "untagged",
"countType": "sinceImagePushed",
"countUnit": "days",
"countNumber": 7
},
"action": {
"type": "expire"
}
}
]
}
EOF
}