generated from opsd-io/terraform-module-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
69 lines (60 loc) · 1.93 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
terraform {
required_version = ">= 1.5.5"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
resource "aws_ecr_repository" "main" {
name = var.name
force_delete = false
image_tag_mutability = var.image_tag_mutable ? "MUTABLE" : "IMMUTABLE"
encryption_configuration {
encryption_type = var.encryption_kms_key != null ? "KMS" : "AES256"
kms_key = var.encryption_kms_key
}
image_scanning_configuration {
scan_on_push = var.scan_on_push
}
tags = merge(var.common_tags, {
Name = var.name
})
}
data "aws_iam_policy_document" "repository_policy" {
override_policy_documents = var.policy_documents
}
resource "aws_ecr_repository_policy" "main" {
count = length(var.policy_documents) > 0 ? 1 : 0
repository = aws_ecr_repository.main.name
policy = data.aws_iam_policy_document.repository_policy.json
}
# https://docs.aws.amazon.com/AmazonECR/latest/userguide/LifecyclePolicies.html
resource "aws_ecr_lifecycle_policy" "main" {
count = length(var.lifecycle_rules) > 0 ? 1 : 0
repository = aws_ecr_repository.main.name
policy = jsonencode({
rules = [
for rule in var.lifecycle_rules : {
for key, val in {
rulePriority = rule.priority
description = rule.description
selection = {
for key, val in {
tagStatus = rule.tag_status
tagPatternList = rule.tag_status == "tagged" ? rule.tag_patterns : null
tagPrefixList = rule.tag_status == "tagged" ? rule.tag_prefixes : null
countType = rule.count_type
countUnit = rule.count_type == "sinceImagePushed" ? "days" : null
countNumber = rule.count_number
} : key => val if val != null
}
action = {
type = "expire"
}
} : key => val if val != null
}
]
})
}