Skip to content

Commit 2868fe6

Browse files
authored
Merge pull request #18 from DNXLabs/feature/ecr-lifecycle
Add scan on push, ecr lifecycle and kms optional
2 parents 0b0a113 + ef50ea6 commit 2868fe6

File tree

4 files changed

+29
-66
lines changed

4 files changed

+29
-66
lines changed

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ The following resources will be created:
1111
- Set the Amazon ECR image scanning on push = true
1212
- Amazon ECR image scanning helps in identifying software vulnerabilities in your container images.
1313
- ECR policies
14-
- Create a ECR lifecyle
15-
- Expire images older than 14 days
16-
- Expire images with feature tag
17-
- Expire images with the same tag
14+
- ECR lifecyle
1815

1916
<!--- BEGIN_TF_DOCS --->
2017

@@ -35,8 +32,10 @@ The following resources will be created:
3532
| Name | Description | Type | Default | Required |
3633
|------|-------------|------|---------|:--------:|
3734
| image\_tag\_mutability | The tag mutability setting for the repository. Must be one of: MUTABLE or IMMUTABLE. Defaults to MUTABLE. | `string` | `"MUTABLE"` | no |
38-
| kms\_key\_arn | KMS Key ARN to use a CMK instead of default key | `string` | n/a | yes |
35+
| kms\_key\_arn | KMS Key ARN to use a CMK instead of default key | `string` | `""` | no |
36+
| lifecycle\_policy | JSON formatted string ECR repository lifecycle policy. | `string` | `""` | no |
3937
| name | Name for ECR repository | `any` | n/a | yes |
38+
| scan\_on\_push | Configuration block that defines image scanning configuration for the repository. | `bool` | `true` | no |
4039
| tags | Map of tags that will be added to created resources. By default resources will be tagged with name and environment. | `map(string)` | `{}` | no |
4140
| trust\_accounts | Accounts to trust and allow ECR fetch | `list(string)` | n/a | yes |
4241

_variables.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ variable "trust_accounts" {
1010
variable "kms_key_arn" {
1111
type = string
1212
description = "KMS Key ARN to use a CMK instead of default key"
13+
default = ""
1314
}
1415

1516
variable "image_tag_mutability" {
@@ -18,6 +19,18 @@ variable "image_tag_mutability" {
1819
default = "MUTABLE"
1920
}
2021

22+
variable "scan_on_push" {
23+
description = "Configuration block that defines image scanning configuration for the repository."
24+
type = bool
25+
default = true
26+
}
27+
28+
variable "lifecycle_policy" {
29+
description = "JSON formatted string ECR repository lifecycle policy."
30+
type = string
31+
default = ""
32+
}
33+
2134
variable "tags" {
2235
description = "Map of tags that will be added to created resources. By default resources will be tagged with name and environment."
2336
type = map(string)

ecr-lifecycle.tf

Lines changed: 5 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,6 @@
1-
# resource "aws_ecr_lifecycle_policy" "default" {
2-
# repository = "${aws_ecr_repository.default.name}"
1+
resource "aws_ecr_lifecycle_policy" "default" {
2+
count = var.lifecycle_policy != "" ? 1 : 0
3+
repository = aws_ecr_repository.default.name
34

4-
# policy = <<EOF
5-
# {
6-
# "rules": [
7-
# {
8-
# "rulePriority": 1,
9-
# "description": "Expire images older than 14 days",
10-
# "selection": {
11-
# "countUnit": "days",
12-
# "countType": "sinceImagePushed",
13-
# "countNumber": 14,
14-
# "tagStatus": "untagged"
15-
# },
16-
# "action": {
17-
# "type": "expire"
18-
# }
19-
# },
20-
# {
21-
# "rulePriority": 2,
22-
# "description": "Expire images with feature tag",
23-
# "selection": {
24-
# "countType": "imageCountMoreThan",
25-
# "tagPrefixList": [
26-
# "feature",
27-
# "prod",
28-
# "deploy",
29-
# "qa",
30-
# "nonprod",
31-
# "staging",
32-
# "preprod",
33-
# "dev",
34-
# "test",
35-
# "production"
36-
# ],
37-
# "countNumber": 1,
38-
# "tagStatus": "tagged"
39-
# },
40-
# "action": {
41-
# "type": "expire"
42-
# }
43-
# },
44-
# {
45-
# "rulePriority": 3,
46-
# "description": "Expire images with the same tag",
47-
# "selection": {
48-
# "countType": "imageCountMoreThan",
49-
# "countNumber": 1,
50-
# "tagStatus": "any"
51-
# },
52-
# "action": {
53-
# "type": "expire"
54-
# }
55-
# }
56-
# ]
57-
# }
58-
# EOF
59-
# }
5+
policy = var.lifecycle_policy
6+
}

ecr-repositories.tf

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@ resource "aws_ecr_repository" "default" {
33
image_tag_mutability = var.image_tag_mutability
44

55
encryption_configuration {
6-
encryption_type = "KMS"
7-
kms_key = length(var.kms_key_arn) > 0 ? var.kms_key_arn : ""
6+
encryption_type = var.kms_key_arn != "" ? "KMS" : "AES256"
7+
kms_key = var.kms_key_arn
8+
}
9+
10+
image_scanning_configuration {
11+
scan_on_push = var.scan_on_push
812
}
913

1014
tags = merge(
1115
var.tags,
1216
{
13-
"Name" = "${var.name}"
17+
"Name" = var.name
1418
},
1519
)
1620

0 commit comments

Comments
 (0)