Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- resource "aws_launch_template" "public_ecs_ec2_launch_template" {
- name_prefix = var.public_ecs.asg.instance_prefix
- image_id = data.aws_ami.aws_linux_2.id
- instance_type = var.public_ecs.asg.instance_type
- key_name = var.aws_keypair_name
- vpc_security_group_ids = [aws_security_group.allow_http.id, aws_security_group.allow_https.id, aws_security_group.allow_ssh.id]
- iam_instance_profile {
- name = "ecsInstanceRole"
- }
- block_device_mappings {
- device_name = "/dev/xvda"
- ebs {
- volume_size = 30
- volume_type = "gp3"
- }
- }
- tag_specifications {
- resource_type = "instance"
- tags = {
- Name = "public-ecs-ec2-instance"
- }
- }
- user_data = filebase64("${path.module}/ecs.sh")
- }
- resource "aws_autoscaling_group" "public_ecs_asg" {
- name = var.public_ecs.asg.name
- vpc_zone_identifier = [aws_subnet.main_vpc_public_subnet_1.id, aws_subnet.main_vpc_public_subnet_2.id]
- min_size = var.public_ecs.asg.ec2_min_instances
- max_size = var.public_ecs.asg.ec2_max_instances
- desired_capacity = 1
- launch_template {
- id = aws_launch_template.public_ecs_ec2_launch_template.id
- version = "$Latest"
- }
- tag {
- key = var.public_ecs.asg.name
- value = true
- propagate_at_launch = true
- }
- health_check_type = "ELB"
- # Required to redeploy without an outage.
- lifecycle {
- create_before_destroy = true
- }
- metrics_granularity = "1Minute"
- enabled_metrics = [
- "GroupMinSize",
- "GroupMaxSize",
- "GroupDesiredCapacity",
- "GroupInServiceInstances",
- "GroupTotalInstances"
- ]
- }
- resource "aws_lb" "public_lb" {
- name = var.public_load_balancer.name
- internal = false
- load_balancer_type = "application"
- security_groups = [aws_security_group.allow_http.id, aws_security_group.allow_https.id, aws_security_group.allow_ssh.id]
- subnets = [aws_subnet.main_vpc_public_subnet_1.id, aws_subnet.main_vpc_public_subnet_2.id]
- tags = {
- Name = var.public_load_balancer.name
- }
- }
- resource "aws_lb_listener" "ecs_lb_listener" {
- load_balancer_arn = aws_lb.public_lb.arn
- port = 80
- protocol = "HTTP"
- default_action {
- type = "forward"
- target_group_arn = aws_lb_target_group.public_ecs_lb_target_group.arn
- }
- }
- resource "aws_lb_target_group" "public_ecs_lb_target_group" {
- name = var.public_load_balancer.target_group.name
- port = 80
- protocol = "HTTP"
- target_type = "instance"
- vpc_id = aws_vpc.main.id
- health_check {
- interval = 30
- path = "/"
- protocol = "HTTP"
- timeout = 5
- healthy_threshold = 5
- unhealthy_threshold = 2
- matcher = "200-299"
- }
- }
- resource "aws_ecs_cluster" "public_ecs_cluster" {
- name = var.public_ecs.cluster_name
- setting {
- name = "containerInsights"
- value = "disabled"
- }
- }
- resource "aws_ecs_capacity_provider" "public_ecs_capacity_provider" {
- name = var.public_ecs.capacity_provider_name
- auto_scaling_group_provider {
- auto_scaling_group_arn = aws_autoscaling_group.public_ecs_asg.arn
- managed_scaling {
- maximum_scaling_step_size = 1
- minimum_scaling_step_size = 1
- status = "ENABLED"
- target_capacity = 1
- }
- }
- }
- resource "aws_ecs_cluster_capacity_providers" "public_ecs_cluster_capacity_provider" {
- cluster_name = aws_ecs_cluster.public_ecs_cluster.name
- capacity_providers = [aws_ecs_capacity_provider.public_ecs_capacity_provider.name]
- default_capacity_provider_strategy {
- base = 1
- weight = 100
- capacity_provider = aws_ecs_capacity_provider.public_ecs_capacity_provider.name
- }
- }
- resource "aws_ecs_task_definition" "public_ecs_task_definition" {
- family = var.public_ecs.task_definition_name
- network_mode = "bridge"
- execution_role_arn = "arn:aws:iam::${var.aws_account_id}:role/ecsTaskExecutionRole"
- cpu = 256
- runtime_platform {
- operating_system_family = "LINUX"
- cpu_architecture = "X86_64"
- }
- container_definitions = jsonencode([
- {
- name = var.public_ecs.asg.instance_name
- image = "${var.aws_account_id}.dkr.ecr.${var.aws_main_region}.amazonaws.com/${var.public_ecs.ecr_repository_production_microservice}:latest"
- cpu = 256
- memory = 512
- essential = true
- portMappings = [
- {
- containerPort = 80
- hostPort = 80
- protocol = "tcp"
- }
- ]
- logConfiguration = {
- logDriver = "awslogs"
- options = {
- "awslogs-group" = aws_cloudwatch_log_group.public_ecs_log_group.name
- "awslogs-region" = var.aws_main_region
- }
- }
- }
- ])
- }
- resource "aws_ecs_service" "public_ecs_service" {
- name = var.public_ecs.service_name
- cluster = aws_ecs_cluster.public_ecs_cluster.id
- task_definition = aws_ecs_task_definition.public_ecs_task_definition.arn
- desired_count = 1
- force_new_deployment = true
- placement_constraints {
- type = "distinctInstance"
- }
- triggers = {
- redeployment = timestamp()
- }
- capacity_provider_strategy {
- capacity_provider = aws_ecs_capacity_provider.public_ecs_capacity_provider.name
- weight = 100
- }
- load_balancer {
- target_group_arn = aws_lb_target_group.public_ecs_lb_target_group.arn
- container_name = var.public_ecs.asg.instance_name
- container_port = 80
- }
- depends_on = [aws_autoscaling_group.public_ecs_asg]
- }
- resource "aws_cloudwatch_log_group" "public_ecs_log_group" {
- name = var.public_ecs.log_group
- retention_in_days = 3
- tags = {
- name = "logs"
- }
- }
Add Comment
Please, Sign In to add comment