From 517409dfe0555ad7e73b0d023493b72699a8a05f Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Tue, 22 Aug 2023 09:19:56 +0100 Subject: [PATCH] added ecr module to terraform repo --- infrastructure/terraform/main.tf | 8 ++++- infrastructure/terraform/modules/ecr/main.tf | 29 +++++++++++++++++++ .../terraform/modules/ecr/outputs.tf | 4 +++ .../terraform/modules/ecr/variables.tf | 4 +++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 infrastructure/terraform/modules/ecr/main.tf create mode 100644 infrastructure/terraform/modules/ecr/outputs.tf create mode 100644 infrastructure/terraform/modules/ecr/variables.tf diff --git a/infrastructure/terraform/main.tf b/infrastructure/terraform/main.tf index f48da21f..95fa5e06 100644 --- a/infrastructure/terraform/main.tf +++ b/infrastructure/terraform/main.tf @@ -98,4 +98,10 @@ module "route53" { providers = { aws.aws_use1 = aws.aws_use1 } -} \ No newline at end of file +} + +# Create an ECR repository for storage of the lambda's docker images +module "ecr" { + source = "./modules/ecr" + environment = var.stage +} diff --git a/infrastructure/terraform/modules/ecr/main.tf b/infrastructure/terraform/modules/ecr/main.tf new file mode 100644 index 00000000..5a30c3cf --- /dev/null +++ b/infrastructure/terraform/modules/ecr/main.tf @@ -0,0 +1,29 @@ +resource "aws_ecr_repository" "my_repository" { + name = "fastapi-repository-${var.environment}" + image_tag_mutability = "MUTABLE" # Allows overwriting image tags, change to IMMUTABLE if you want to prevent overwriting + + image_scanning_configuration { + scan_on_push = true + } +} + +resource "aws_ecr_lifecycle_policy" "my_repository_policy" { + repository = aws_ecr_repository.my_repository.name + + policy = jsonencode({ + rules = [ + { + rulePriority = 1 + description = "Retain only the last 10 images" + selection = { + tagStatus = "any" + countType = "imageCountMoreThan" + countNumber = 10 + } + action = { + type = "expire" + } + } + ] + }) +} \ No newline at end of file diff --git a/infrastructure/terraform/modules/ecr/outputs.tf b/infrastructure/terraform/modules/ecr/outputs.tf new file mode 100644 index 00000000..53839718 --- /dev/null +++ b/infrastructure/terraform/modules/ecr/outputs.tf @@ -0,0 +1,4 @@ +output "ecr_repository_name" { + description = "Name of the EPR repo in AWS" + value = aws_ecr_repository.my_repository.name +} \ No newline at end of file diff --git a/infrastructure/terraform/modules/ecr/variables.tf b/infrastructure/terraform/modules/ecr/variables.tf new file mode 100644 index 00000000..108bb626 --- /dev/null +++ b/infrastructure/terraform/modules/ecr/variables.tf @@ -0,0 +1,4 @@ +variable "environment" { + description = "The environment for the ECR repository (dev or prod)" + type = string +} \ No newline at end of file