Skip to content

Commit df6b404

Browse files
author
Premdeep Saini
committed
feat: add terraform resources to setup ecs cluster
1 parent bd7288b commit df6b404

File tree

7 files changed

+151
-0
lines changed

7 files changed

+151
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.DS_Store
2+
.idea
3+
.envrc
4+
.terraform/

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
### ECS Terraform Module
2+
3+
This module provides resources for the setup of ECS cluster. This includes:
4+
5+
- ECS cluster
6+
- Capacity Providers
7+
- Auto scaling groups
8+
- Launch Configurations

auto_scaling_groups.tf

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
resource "aws_launch_configuration" "ecs_launch_config" {
2+
for_each = { for launch_config_name, launch_config in var.launch_configs : launch_config.name => launch_config }
3+
name = "${var.environment}_ecs_${each.value.name}"
4+
image_id = each.value.image_id
5+
instance_type = each.value.instance_type
6+
user_data_base64 = each.value.user_data_base64
7+
iam_instance_profile = each.value.iam_instance_profile_name
8+
security_groups = each.value.security_group_ids
9+
lifecycle {
10+
create_before_destroy = true
11+
}
12+
}
13+
14+
resource "aws_autoscaling_group" "ecs_cluster_asg" {
15+
for_each = { for asg_name, asg in var.asg : asg.name => asg }
16+
name = "${var.environment}_ecs_${each.value.name}"
17+
vpc_zone_identifier = each.value.vpc_zone_identifier
18+
health_check_type = each.value.health_check_type
19+
health_check_grace_period = each.value.health_check_grace_period
20+
launch_configuration = aws_launch_configuration.ecs_launch_config[each.key].name
21+
max_size = each.value.max_size
22+
min_size = each.value.max_size
23+
protect_from_scale_in = each.value.protect_from_scale_in
24+
tag {
25+
key = "Name"
26+
propagate_at_launch = true
27+
value = "${var.environment}_ecs_${each.value.name}"
28+
}
29+
tag {
30+
key = "ManagedBy"
31+
propagate_at_launch = false
32+
value = local.managed_by
33+
}
34+
tag {
35+
key = "Environment"
36+
propagate_at_launch = true
37+
value = var.environment
38+
}
39+
lifecycle {
40+
create_before_destroy = true
41+
}
42+
depends_on = [aws_launch_configuration.ecs_launch_config]
43+
}

main.tf

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
locals {
2+
managed_by = "Terraform"
3+
}
4+
5+
resource "aws_ecs_cluster" "ecs_cluster" {
6+
name = var.cluster_name
7+
tags = {
8+
"Name" = var.cluster_name
9+
"ManagedBy" = local.managed_by
10+
"Environment" = var.environment
11+
}
12+
}
13+
14+
resource "aws_ecs_capacity_provider" "capacity_providers" {
15+
for_each = { for name, capacity_provider in var.capacity_providers : capacity_provider.name => capacity_provider }
16+
name = "${var.environment}_ecs_${each.value.name}"
17+
auto_scaling_group_provider {
18+
auto_scaling_group_arn = aws_autoscaling_group.ecs_cluster_asg[each.key].arn
19+
managed_scaling {
20+
target_capacity = each.value.target_capacity
21+
status = each.value.managed_scaling_status
22+
}
23+
managed_termination_protection = each.value.managed_termination_protection
24+
}
25+
tags = {
26+
"ManagedBy" = local.managed_by
27+
}
28+
depends_on = [aws_autoscaling_group.ecs_cluster_asg]
29+
}
30+
31+
resource "aws_ecs_cluster_capacity_providers" "ecs_cluster_capacity_provider" {
32+
cluster_name = aws_ecs_cluster.ecs_cluster.name
33+
capacity_providers = [for cp in aws_ecs_capacity_provider.capacity_providers : cp.name]
34+
depends_on = [aws_ecs_capacity_provider.capacity_providers]
35+
}

outputs.tf

Whitespace-only changes.

variables.tf

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
variable "environment" {
2+
type = string
3+
description = "Environment. Example: staging, production."
4+
}
5+
variable "cluster_name" {
6+
type = string
7+
description = "Name of ECS cluster."
8+
}
9+
variable "vpc_id" {
10+
type = string
11+
description = "AWS VPC identifier for ECS cluster"
12+
}
13+
variable "vpc_cidr_block" {
14+
type = string
15+
description = "CIDR block for VPC."
16+
}
17+
variable "capacity_providers" {
18+
type = list(object({
19+
name = string
20+
target_capacity = number
21+
managed_scaling_status = string
22+
managed_termination_protection = string
23+
}))
24+
description = "Capacity provider configuration."
25+
}
26+
variable "asg" {
27+
type = list(object({
28+
name = string
29+
vpc_zone_identifier = list(string)
30+
health_check_type = string
31+
health_check_grace_period = number
32+
max_size = number
33+
min_size = number
34+
protect_from_scale_in = bool
35+
}))
36+
description = "Autoscaling group configuration."
37+
}
38+
variable "launch_configs" {
39+
type = list(object({
40+
name = string
41+
image_id = string
42+
instance_type = string
43+
user_data_base64 = string
44+
iam_instance_profile_name = string
45+
security_group_ids = list(string)
46+
}))
47+
description = "Launch configuration for EC2 instances."
48+
}

versions.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = "~> 4.16"
6+
}
7+
cloudinit = {
8+
source = "hashicorp/cloudinit"
9+
version = ">=2.2.0"
10+
}
11+
}
12+
required_version = ">= 1.2.0"
13+
}

0 commit comments

Comments
 (0)