Skip to content

Rishang/terraform-aws-fargate

Repository files navigation

aws ecs fargate terraform module

Usage

For examples and refrences click here.

# quick usage look

# things to be created
# ecs service (memory scaling) >> TargetGroup >> Alb HTTPS rule >> Route53 >> app.example.com

module "fargate" {
  source  = "Rishang/fargate/aws"
  version = "1.4.3"

  EnvironmentName = "test"

  # ecs fargate
  cluster             = aws_ecs_cluster.app.name
  service             = "whoami"
  container_port      = 80
  task_definition_arn = module.fargate_task_definition.arn
  scale_min_capacity  = 3
  scale_max_capacity  = 10
  
  # keep 1 FARGATE for each 5 FARGATE_SPOT
  capacity_provider_strategy = [
    {
      base              = 1
      capacity_provider = "FARGATE"
      weight            = 1
    },
    {
      base              = 0
      capacity_provider = "FARGATE_SPOT"
      weight            = 5
    }
  ]

  # networking
  assign_public_ip = true
  vpc_id           = "vpc-demos7"
  subnets          = ["subnet-a2b3","subnet-c9da","subnet-0b23"]
  security_groups  = ["sg-f34d92"]

  # load balancer (optional)
  point_to_lb        = true
  listener_arn_https = aws_lb_listener.https.arn
  subdomain          = "app.example.com"

  # route53 (optional)
  point_to_r53 = true

  # autoscale (optional)
  create_autoscale_target = true
  cpu_scale_target  = 60
  # memory_scale_target = 60

  # scheduled scaling (optional)
  scaling_schedule = [
    {
      # Scale count to zero every night at 19:00
      schedule     = "cron(0 19 * * ? *)"
      min_capacity = 0
      max_capacity = 0
    },
    {
      # Scale count to 3 every morning at 7:00
      schedule     = "cron(0 7 * * ? *)"
      min_capacity = 3
      max_capacity = 3
    }
  ]

  # service discovery (optional)
  enable_discovery = true
  namespace_id     = aws_service_discovery_private_dns_namespace.service.id

  tags = {
    Name         = "whoami"
    Version      = "latest"
    cluster_name = local.cluster_name
  }
}

Providers

Name Version
aws 4.8.0

Outputs

Name Description
cluster_name ecs fargate application cluster name.
discovery_arn application service discovery name. (if provided)
discovery_id application service discovery name. (if provided)
discovery_name application service discovery name. (if provided)
domain_id application route53 endpoint id. (if provided)
domain_name application service domain name. (if provided)
domain_type application route53 endpoint domain type eg. [A, CNAME]. (if provided)
id ecs fargate application service id.
name ecs fargate application service name.

available tfvar inputs

# null are required inputs, 
# others are optional default values

EnvironmentName  = null
assign_public_ip = false
capacity_provider_strategy = [{
  base              = "1"
  capacity_provider = "FARGATE"
  weight            = "1"
  }, {
  base              = "0"
  capacity_provider = "FARGATE_SPOT"
  weight            = "0"
}]
cluster                            = null
container_name                     = ""
container_port                     = -1
cpu_scale_target                   = -1
create_autoscale_target            = false
deployment_maximum_percent         = 200
deployment_minimum_healthy_percent = 100
enable_discovery                   = false
enable_ecs_managed_tags            = false
force_new_deployment               = false
health_check_interval              = 20
health_check_matcher               = "200,202"
health_check_path                  = "/"
lb_scale_target                    = -1
listener_arn_https                 = ""
memory_scale_target                = -1
namespace_id                       = ""
path_pattern                       = ["/", "/*"]
point_to_lb                        = false
point_to_r53                       = false
scale_in_cooldown                  = 250
scale_max_capacity                 = 20
scale_min_capacity                 = 1
scale_out_cooldown                 = 250
scaling_schedule                   = []
security_groups                    = []
service                            = null
subdomain                          = ""
subnets                            = null
tags                               = {}
task_definition_arn                = null
vpc_id                             = ""

Inputs

Name Description Type Default Required
EnvironmentName The name of the infra environment to deploy to eg. dev, prod, test string n/a yes
cluster The name of the cluster that hosts the service any n/a yes
service Fargate service name any n/a yes
subnets List of subnets for ecs service list(string) n/a yes
task_definition_arn The ARN of the task definition to use for the ECS service string n/a yes
assign_public_ip Auto assign public ip for ecs containers bool false no
capacity_provider_strategy Capacity provider strategy for ecs service here base parameter defines the minimum number of tasks that should be launched using the specified capacity provider before considering the weight. weight parameter defines the relative percentage of tasks to be launched using the specified capacity provider after the base tasks have been satisfied. list(map(any))
[
{
"base": 1,
"capacity_provider": "FARGATE",
"weight": 1
},
{
"base": 0,
"capacity_provider": "FARGATE_SPOT",
"weight": 0
}
]
no
container_name Required if service name is different than main application container_name of task defination string "" no
container_port container application port number -1 no
cpu_scale_target Treshold cpu target value for autoscaling ecs service number -1 no
create_autoscale_target Enable to create autoscale for ecs service bool false no
deployment_maximum_percent Deployment max healthy percent of container count number 200 no
deployment_minimum_healthy_percent Deployment min healthy percent of container count number 100 no
enable_discovery Enable service discovery, requires namespace_id and container_name bool false no
enable_ecs_managed_tags Specifies whether to enable Amazon ECS managed tags for the service. bool false no
force_new_deployment Enable to force a new task deployment of the service bool false no
health_check_interval target group health check interval time in sec number 20 no
health_check_matcher Service health check response matcher string "200,202" no
health_check_path Health check path for ecs running containers string "/" no
lb_scale_target Treshold target requests traffic value from alb, for autoscaling ecs service number -1 no
listener_arn_https HTTPS listner arn for Application Load Balencer (required if 'point_to_lb' is true) string "" no
memory_scale_target Treshold memory target value for autoscaling ecs service number -1 no
namespace_id Namespace id (private) for service discovery, Note: discovery endpoint's subdomain will be same as service name string "" no
path_pattern List of paths for alb to route traffic at ecs target group list(string)
[
"/",
"/*"
]
no
point_to_lb Enable to point to ALB (load balancer) bool false no
point_to_r53 Enable to point to R53 bool false no
scale_in_cooldown The amount of time, in sec, after a scale in activity completes before another scale in activity can start. number 250 no
scale_max_capacity Max count of containers number 20 no
scale_min_capacity Min count of containers number 1 no
scale_out_cooldown The amount of time, in sec, after a scale out activity completes before another scale in activity can start. number 250 no
scaling_schedule Schedule scaling for ecs service [{"schedule":"cron(0 3 * * ? *)", "min_capacity": 1, "max_capacity": 1}] list(any) [] no
security_groups Extra security groups to attach to ecs service list(string) [] no
subdomain Subdomain name you want to give eg: test.example.com (required if 'point_to_r53' is true) string "" no
tags Tags to apply to the resources map(any) {} no
vpc_id aws vpc id string "" no

README.md created by: terraform-docs