// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: MIT-0 data "aws_iam_role" "ecs_task_execution_role" { name = "team-app-rotation-ecs-task-execution-role" } resource "aws_ecs_cluster" "ecs-cluster" { name = "${var.APP}-${var.COMPONENT}-ecs-cluster" setting { name = "containerInsights" value = "enabled" } tags = { Industry = "GFS" Program = "AppRotation" Application = var.APP Component = var.COMPONENT Environment = var.ENV } } resource "aws_iam_instance_profile" "ecs-ec2-role" { name = "team-${var.APP}-${var.COMPONENT}-${var.AWS_REGION}-ecs-ec2-role" role = "team-app-rotation-ecs-ec2-role" } resource "aws_launch_configuration" "launch-config" { name_prefix = "${var.APP}-${var.COMPONENT}-launch-config" image_id = var.ECS_AMIS[var.AWS_REGION] instance_type = var.ECS_INSTANCE_TYPE iam_instance_profile = aws_iam_instance_profile.ecs-ec2-role.id security_groups = [var.ECS_SECURITY_GROUP_ID] user_data = "#!/bin/bash\necho 'ECS_CLUSTER=${var.APP}-${var.COMPONENT}-ecs-cluster' > /etc/ecs/ecs.config\nstart ecs" lifecycle { create_before_destroy = true } root_block_device { encrypted = true } #checkov:skip=CKV_AWS_79:Ensure Instance Metadata Service Version 1 is not enabled } resource "aws_autoscaling_group" "auto-scaling-group" { name = "${var.APP}-${var.COMPONENT}-asg" vpc_zone_identifier = var.SUBNET_IDS launch_configuration = aws_launch_configuration.launch-config.name min_size = var.CONTAINER_COUNT max_size = 10 health_check_grace_period = 300 health_check_type = "ELB" tag { key = "Name" value = "${var.APP}-${var.COMPONENT}-container" propagate_at_launch = true } tag { key = "Industry" value = "GFS" propagate_at_launch = true } tag { key = "Program" value = "AppRotation" propagate_at_launch = true } tag { key = "Application" value = var.APP propagate_at_launch = true } tag { key = "Component" value = var.COMPONENT propagate_at_launch = true } tag { key = "Environment" value = var.ENV propagate_at_launch = true } } resource "aws_autoscaling_policy" "autoscaling_policy" { name = "${var.APP}-${var.COMPONENT}-asg-policy" scaling_adjustment = 1 adjustment_type = "ChangeInCapacity" cooldown = 300 autoscaling_group_name = "${aws_autoscaling_group.auto-scaling-group.name}" } resource "aws_cloudwatch_metric_alarm" "cpu_alarm" { alarm_name = "${var.APP}-${var.COMPONENT}-asg-cpu-alarm" comparison_operator = "GreaterThanOrEqualToThreshold" evaluation_periods = "2" metric_name = "CPUUtilization" namespace = "AWS/EC2" period = "120" statistic = "Average" threshold = "60" dimensions = { AutoScalingGroupName = "${aws_autoscaling_group.auto-scaling-group.name}" } } resource "aws_kms_key" "ecr_key" { description = "${var.APP}-${var.COMPONENT}-ecr-kms-key" enable_key_rotation = true tags = { Environment = var.ENV Name = "${var.APP}-${var.COMPONENT}-ecr-kms-key" } } resource "aws_kms_alias" "ecr_key_alias" { name = "alias/${var.APP}-${var.COMPONENT}-ecr-kms-key" target_key_id = aws_kms_key.ecr_key.key_id } resource "aws_ecr_repository" "approtation" { name = "${var.APP}-${var.COMPONENT}-ecr" image_tag_mutability = "MUTABLE" encryption_configuration { encryption_type = "KMS" kms_key = aws_kms_key.ecr_key.arn } image_scanning_configuration { scan_on_push = true } tags = { Name = "${var.APP}-${var.COMPONENT}-ecr" Industry = "GFS" Program = "AppRotation" Application = var.APP Component = var.COMPONENT Environment = var.ENV } #checkov:skip=CKV_AWS_51:Ensure ECR Image Tags are immutable } locals { REPOSITORY = replace(aws_ecr_repository.approtation.repository_url, "https://", "") } resource "aws_ecs_task_definition" "task-definition" { family = "${var.APP}-${var.COMPONENT}" container_definitions = <