diff --git a/main.tf b/main.tf index c3f623d..0ec596a 100644 --- a/main.tf +++ b/main.tf @@ -38,7 +38,8 @@ module load_balancer { ############ module container_definition { - source = "./modules/fargate_container_definition" + source = "transcend-io/fargate-container/aws" + version = "1.5.1" name = "${var.deploy_env}-${var.project_id}-container" image = var.ecr_image @@ -124,7 +125,8 @@ module container_definition { ############### module service { - source = "./modules/fargate_service" + source = "transcend-io/fargate-service/aws" + version = "0.4.0" name = "${var.deploy_env}-${var.project_id}-sombra-service" desired_count = var.desired_count @@ -143,9 +145,10 @@ module service { ) additional_task_policy_arns = concat([ - module.container_definition.secrets_policy_arn, - aws_iam_policy.kms_policy.arn, - ], var.extra_task_policy_arns) + aws_iam_policy.kms_policy.arn], + module.container_definition.secrets_policy_arns, + var.extra_task_policy_arns + ) additional_task_policy_arns_count = 2 + length(var.extra_task_policy_arns) load_balancers = [ diff --git a/modules/fargate_container_definition/main.tf b/modules/fargate_container_definition/main.tf deleted file mode 100644 index 339a804..0000000 --- a/modules/fargate_container_definition/main.tf +++ /dev/null @@ -1,110 +0,0 @@ -resource "aws_ssm_parameter" "params" { - for_each = var.secret_environment - - description = "Param for the ${each.key} env var in the container: ${var.name}" - - name = "${var.deploy_env}-${var.ssm_prefix}-${each.key}" - value = each.value - - type = "SecureString" - tier = length(each.value) > 4096 ? "Advanced" : "Standard" - - tags = var.tags -} - -resource "aws_ssm_parameter" "secret_log_options" { - for_each = var.log_secrets - - description = "Log option named ${each.key} in the container: ${var.name}" - - name = "${var.deploy_env}-logOptions-${var.ssm_prefix}-${each.key}" - value = each.value - - type = "SecureString" - tier = length(each.value) > 4096 ? "Advanced" : "Standard" - - tags = var.tags -} - -data "aws_iam_policy_document" "secret_access_policy_doc" { - statement { - effect = "Allow" - actions = [ - "ssm:GetParameters", - "secretsmanager:GetSecretValue", - ] - resources = [ - for name, outputs in merge( - aws_ssm_parameter.params, - aws_ssm_parameter.secret_log_options, - ) : - outputs.arn - ] - } -} - -resource "aws_iam_policy" "secret_access_policy" { - name_prefix = "${var.deploy_env}-${var.name}-secret-access-policy" - description = "Gives access to read ssm env vars" - policy = data.aws_iam_policy_document.secret_access_policy_doc.json -} - -module "definition" { - source = "cloudposse/ecs-container-definition/aws" - version = "v0.21.0" - - container_name = var.name - container_image = var.image - - container_cpu = var.cpu - container_memory = var.memory - - port_mappings = [ - for port in var.containerPorts : - { - containerPort = port - hostPort = port - protocol = "tcp" - } - ] - - log_configuration = var.use_cloudwatch_logs ? { - logDriver = "awslogs" - options = { - "awslogs-region" = var.aws_region - "awslogs-group" = aws_cloudwatch_log_group.log_group[0].name - "awslogs-stream-prefix" = "ecs--${var.name}" - } - secretOptions = [] - } : merge(var.log_configuration, { - secretOptions = [ - for name, outputs in aws_ssm_parameter.secret_log_options : - { - name = name - valueFrom = outputs.arn - } - ] - }) - - environment = [ - for name in sort(keys(var.environment)) : - { - name = name - value = var.environment[name] - } - ] - - secrets = [ - for name, outputs in aws_ssm_parameter.params : - { - name = name - valueFrom = outputs.arn - } - ] -} - -resource "aws_cloudwatch_log_group" "log_group" { - count = var.use_cloudwatch_logs ? 1 : 0 - name = "${var.name}-log-group" - tags = var.tags -} diff --git a/modules/fargate_container_definition/outputs.tf b/modules/fargate_container_definition/outputs.tf deleted file mode 100644 index db64646..0000000 --- a/modules/fargate_container_definition/outputs.tf +++ /dev/null @@ -1,19 +0,0 @@ -output json { - value = module.definition.json -} - -output json_map { - value = module.definition.json_map -} - -output secrets_policy_arn { - value = aws_iam_policy.secret_access_policy.arn -} - -output container_name { - value = var.name -} - -output container_ports { - value = var.containerPorts -} diff --git a/modules/fargate_container_definition/variables.tf b/modules/fargate_container_definition/variables.tf deleted file mode 100644 index 42d958a..0000000 --- a/modules/fargate_container_definition/variables.tf +++ /dev/null @@ -1,130 +0,0 @@ -variable name { - type = string - description = < 0 ? 60 : 0 - - dynamic "load_balancer" { - for_each = var.load_balancers - content { - target_group_arn = load_balancer.value.target_group_arn - container_name = load_balancer.value.container_name - container_port = load_balancer.value.container_port - } - } - - # Allow external changes to service count without Terraform plan difference - lifecycle { - ignore_changes = [desired_count] - } - - propagate_tags = "SERVICE" - tags = var.tags -} - -resource "aws_ecs_task_definition" "task" { - family = "${var.name}-task" - requires_compatibilities = ["FARGATE"] - network_mode = "awsvpc" - cpu = var.cpu - memory = var.memory - execution_role_arn = aws_iam_role.execution_role.arn - task_role_arn = aws_iam_role.execution_role.arn - container_definitions = var.container_definitions - tags = var.tags -} - -resource "aws_security_group" "service_security_group" { - name = "${var.name}-ecs-security-group" - description = "Allows inbound access to an ECS service only through its alb" - vpc_id = var.vpc_id - - dynamic "ingress" { - for_each = var.load_balancers - content { - from_port = ingress.value.container_port - to_port = ingress.value.container_port - security_groups = var.alb_security_group_ids - protocol = "tcp" - } - } - - # Allow all outgoing access - egress { - from_port = 0 - to_port = 0 - protocol = "-1" - cidr_blocks = ["0.0.0.0/0"] - } - - timeouts { - create = "45m" - delete = "45m" - } - - tags = var.tags -} diff --git a/modules/fargate_service/outputs.tf b/modules/fargate_service/outputs.tf deleted file mode 100644 index a09d29a..0000000 --- a/modules/fargate_service/outputs.tf +++ /dev/null @@ -1,9 +0,0 @@ -output "role_arn" { - value = aws_iam_role.execution_role.arn - description = "Arn of the task execution role" -} - -output "policy_arns" { - value = local.policy_arns - description = "Amazon resource names of all policies set on the IAM Role execution task" -} \ No newline at end of file diff --git a/modules/fargate_service/variables.tf b/modules/fargate_service/variables.tf deleted file mode 100644 index 1ded52b..0000000 --- a/modules/fargate_service/variables.tf +++ /dev/null @@ -1,90 +0,0 @@ -variable name { - description = "The name of the service. Used as a prefix for other resource names" -} - -variable cluster_id { - description = <