Skip to content

cn-terraform/terraform-aws-ecs-fargate-service

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AWS ECS Fargate Terraform Module

This Terraform module deploys an AWS ECS Fargate service.

Usage

Check valid versions on:

Other modules that you may need to use this module

The Networking module:

The ECS cluster module:

The ECS Task Definition:

The ECS ALB module:

Install pre commit hooks.

Please run this command right after cloning the repository.

    pre-commit install

For that you may need to install the following tools:

In order to run all checks at any point run the following command:

    pre-commit run --all-files

Requirements

Name Version
terraform >= 0.13
aws >= 4
local >= 2

Providers

Name Version
aws 4.48.0

Modules

Name Source Version
ecs-alb cn-terraform/ecs-alb/aws 1.0.32
ecs-autoscaling cn-terraform/ecs-service-autoscaling/aws 1.0.6

Resources

Name Type
aws_ecs_service.service resource
aws_security_group.ecs_tasks_sg resource
aws_security_group_rule.egress resource
aws_security_group_rule.ingress_through_http_and_https resource

Inputs

Name Description Type Default Required
access_logs_prefix (Optional) if access logging to an S3 bucket, this sets a prefix in the bucket beneath which this LB's logs will be organized. string null no
additional_certificates_arn_for_https_listeners (Optional) List of SSL server certificate ARNs for HTTPS listener. Use it if you need to set additional certificates besides default_certificate_arn list(any) [] no
assign_public_ip (Optional) Assign a public IP address to the ENI (Fargate launch type only). If true service will be associated with public subnets. Default false. bool false no
block_s3_bucket_public_access (Optional) If true, public access to the S3 bucket will be blocked. Ignored if log_bucket_id is provided. bool true no
container_name Name of the running container any n/a yes
custom_lb_arn ARN of the Load Balancer to use in the ECS service. If provided, this module will not create a load balancer and will use the one provided in this variable string null no
default_certificate_arn (Optional) The ARN of the default SSL server certificate. Required if var.https_ports is set. string null no
deployment_circuit_breaker_enabled (Optional) You can enable the deployment circuit breaker to cause a service deployment to transition to a failed state if tasks are persistently failing to reach RUNNING state or are failing healthcheck. bool false no
deployment_circuit_breaker_rollback (Optional) The optional rollback option causes Amazon ECS to roll back to the last completed deployment upon a deployment failure. bool false no
deployment_controller (Optional) Deployment controller list(string) [] no
deployment_maximum_percent (Optional) The upper limit (as a percentage of the service's desiredCount) of the number of running tasks that can be running in a service during a deployment. number 200 no
deployment_minimum_healthy_percent (Optional) The lower limit (as a percentage of the service's desiredCount) of the number of running tasks that must remain running and healthy in a service during a deployment. number 100 no
desired_count (Optional) The number of instances of the task definition to place and keep running. Defaults to 0. number 1 no
ecs_cluster_arn ARN of an ECS cluster any n/a yes
ecs_cluster_name (Optional) Name of the ECS cluster. Required only if autoscaling is enabled string null no
ecs_tasks_sg_allow_egress_to_anywhere (Optional) If true an egress rule will be created to allow traffic to anywhere (0.0.0.0/0). If false no egress rule will be created. Defaults to true bool true no
enable_autoscaling (Optional) If true, autoscaling alarms will be created. bool true no
enable_ecs_managed_tags (Optional) Specifies whether to enable Amazon ECS managed tags for the tasks within the service. bool false no
enable_execute_command (Optional) Specifies whether to enable Amazon ECS Exec for the tasks within the service. bool false no
enable_s3_bucket_server_side_encryption (Optional) If true, server side encryption will be applied. Ignored if log_bucket_id is provided. bool true no
enable_s3_logs (Optional) If true, all LoadBalancer logs will be send to S3. If true, and log_bucket_id is not provided, this module will create the bucket with other provided s3 bucket configuration options bool true no
force_new_deployment (Optional) Enable to force a new task deployment of the service. This can be used to update tasks to use a newer Docker image with same image/tag combination (e.g. myimage:latest), roll Fargate tasks onto a newer platform version, or immediately deploy ordered_placement_strategy and placement_constraints updates. bool false no
health_check_grace_period_seconds (Optional) Seconds to ignore failing load balancer health checks on newly instantiated tasks to prevent premature shutdown, up to 2147483647. Only valid for services configured to use load balancers. number 0 no
lb_deregistration_delay (Optional) The amount time for Elastic Load Balancing to wait before changing the state of a deregistering target from draining to unused. The range is 0-3600 seconds. The default value is 300 seconds. number 300 no
lb_drop_invalid_header_fields (Optional) Indicates whether HTTP headers with header fields that are not valid are removed by the load balancer (true) or routed to targets (false). The default is false. Elastic Load Balancing requires that message header names contain only alphanumeric characters and hyphens. bool false no
lb_enable_cross_zone_load_balancing (Optional) If true, cross-zone load balancing of the load balancer will be enabled. Defaults to false. bool false no
lb_enable_deletion_protection (Optional) If true, deletion of the load balancer will be disabled via the AWS API. This will prevent Terraform from deleting the load balancer. Defaults to false. bool false no
lb_enable_http2 (Optional) Indicates whether HTTP/2 is enabled in the load balancer. Defaults to true. bool true no
lb_http_ingress_cidr_blocks List of CIDR blocks to allowed to access the Load Balancer through HTTP list(string)
[
"0.0.0.0/0"
]
no
lb_http_ingress_prefix_list_ids List of prefix list IDs blocks to allowed to access the Load Balancer through HTTP list(string) [] no
lb_http_ports Map containing objects to define listeners behaviour based on type field. If type field is forward, include listener_port and the target_group_port. For redirect type, include listener port, host, path, port, protocol, query and status_code. For fixed-response, include listener_port, content_type, message_body and status_code map(any)
{
"default-http": {
"listener_port": 80,
"target_group_port": 80,
"type": "forward"
}
}
no
lb_https_ingress_cidr_blocks List of CIDR blocks to allowed to access the Load Balancer through HTTPS list(string)
[
"0.0.0.0/0"
]
no
lb_https_ingress_prefix_list_ids List of prefix list IDs blocks to allowed to access the Load Balancer through HTTPS list(string) [] no
lb_https_ports Map containing objects to define listeners behaviour based on type field. If type field is forward, include listener_port and the target_group_port. For redirect type, include listener port, host, path, port, protocol, query and status_code. For fixed-response, include listener_port, content_type, message_body and status_code map(any)
{
"default-https": {
"listener_port": 443,
"target_group_port": 443,
"type": "forward"
}
}
no
lb_idle_timeout (Optional) The time in seconds that the connection is allowed to be idle. Default: 60. number 60 no
lb_internal (Optional) If true, the LB will be internal. bool false no
lb_ip_address_type (Optional) The type of IP addresses used by the subnets for your load balancer. The possible values are ipv4 and dualstack. Defaults to ipv4 string "ipv4" no
lb_load_balancing_algorithm_type (Optional) Determines how the load balancer selects targets when routing requests. The value is round_robin or least_outstanding_requests. The default is round_robin. string "round_robin" no
lb_security_groups (Optional) A list of security group IDs to assign to the LB. list(string) [] no
lb_slow_start (Optional) The amount time for targets to warm up before the load balancer sends them a full share of requests. The range is 30-900 seconds or 0 to disable. The default value is 0 seconds. number 0 no
lb_stickiness (Optional) A Stickiness block. Provide three fields. type, the type of sticky sessions. The only current possible value is lb_cookie. cookie_duration, the time period, in seconds, during which requests from a client should be routed to the same target. After this time period expires, the load balancer-generated cookie is considered stale. The range is 1 second to 1 week (604800 seconds). The default value is 1 day (86400 seconds). enabled, boolean to enable / disable stickiness. Default is true.
object({
type = string
cookie_duration = string
enabled = bool
})
{
"cookie_duration": 86400,
"enabled": true,
"type": "lb_cookie"
}
no
lb_target_group_health_check_enabled (Optional) Indicates whether health checks are enabled. Defaults to true. bool true no
lb_target_group_health_check_healthy_threshold (Optional) The number of consecutive health checks successes required before considering an unhealthy target healthy. Defaults to 3. number 3 no
lb_target_group_health_check_interval (Optional) The approximate amount of time, in seconds, between health checks of an individual target. Minimum value 5 seconds, Maximum value 300 seconds. Default 30 seconds. number 30 no
lb_target_group_health_check_matcher The HTTP codes to use when checking for a successful response from a target. You can specify multiple values (for example, "200,202") or a range of values (for example, "200-299"). Default is 200. string "200" no
lb_target_group_health_check_path The destination for the health check request. string "/" no
lb_target_group_health_check_timeout (Optional) The amount of time, in seconds, during which no response means a failed health check. The range is 2 to 120 seconds, and the default is 5 seconds. number 5 no
lb_target_group_health_check_unhealthy_threshold (Optional) The number of consecutive health check failures required before considering the target unhealthy. Defaults to 3. number 3 no
log_bucket_id (Optional) if provided, the ID of a previously-defined S3 bucket to send LB logs to. string null no
max_cpu_evaluation_period The number of periods over which data is compared to the specified threshold for max cpu metric alarm string "3" no
max_cpu_period The period in seconds over which the specified statistic is applied for max cpu metric alarm string "60" no
max_cpu_threshold Threshold for max CPU usage string "85" no
min_cpu_evaluation_period The number of periods over which data is compared to the specified threshold for min cpu metric alarm string "3" no
min_cpu_period The period in seconds over which the specified statistic is applied for min cpu metric alarm string "60" no
min_cpu_threshold Threshold for min CPU usage string "10" no
name_prefix Name prefix for resources on AWS any n/a yes
ordered_placement_strategy (Optional) Service level strategy rules that are taken into consideration during task placement. List from top to bottom in order of precedence. The maximum number of ordered_placement_strategy blocks is 5. This is a list of maps where each map should contain "id" and "field" list(any) [] no
placement_constraints (Optional) rules that are taken into consideration during task placement. Maximum number of placement_constraints is 10. This is a list of maps, where each map should contain "type" and "expression" list(any) [] no
platform_version (Optional) The platform version on which to run your service. Defaults to 1.4.0. More information about Fargate platform versions can be found in the AWS ECS User Guide. string "1.4.0" no
private_subnets The private subnets associated with the task or service. list(any) n/a yes
propagate_tags (Optional) Specifies whether to propagate the tags from the task definition or the service to the tasks. The valid values are SERVICE and TASK_DEFINITION. Default to SERVICE string "SERVICE" no
public_subnets The public subnets associated with the task or service. list(any) n/a yes
s3_bucket_server_side_encryption_key (Optional) The AWS KMS master key ID used for the SSE-KMS encryption. This can only be used when you set the value of sse_algorithm as aws:kms. The default aws/s3 AWS KMS master key is used if this element is absent while the sse_algorithm is aws:kms. Ignored if log_bucket_id is provided. string null no
s3_bucket_server_side_encryption_sse_algorithm (Optional) The server-side encryption algorithm to use. Valid values are AES256 and aws:kms. Ignored if log_bucket_id is provided. string "AES256" no
scale_target_max_capacity The max capacity of the scalable target number 5 no
scale_target_min_capacity The min capacity of the scalable target number 1 no
security_groups (Optional) The security groups associated with the task or service. If you do not specify a security group, the default security group for the VPC is used. list(any) [] no
service_registries (Optional) The service discovery registries for the service. The maximum number of service_registries blocks is 1. This is a map that should contain the following fields "registry_arn", "port", "container_port" and "container_name" map(any) {} no
ssl_policy (Optional) The name of the SSL Policy for the listener. . Required if var.https_ports is set. string null no
tags Resource tags map(string) {} no
task_definition_arn (Required) The full ARN of the task definition that you want to run in your service. any n/a yes
vpc_id ID of the VPC any n/a yes
waf_web_acl_arn ARN of a WAFV2 to associate with the ALB string "" no

Outputs

Name Description
aws_ecs_service_service_cluster The Amazon Resource Name (ARN) of cluster which the service runs on.
aws_ecs_service_service_desired_count The number of instances of the task definition
aws_ecs_service_service_id The Amazon Resource Name (ARN) that identifies the service.
aws_ecs_service_service_name The name of the service.
aws_lb_lb_arn The ARN of the load balancer (matches id).
aws_lb_lb_arn_suffix The ARN suffix for use with CloudWatch Metrics.
aws_lb_lb_dns_name The DNS name of the load balancer.
aws_lb_lb_id The ARN of the load balancer (matches arn).
aws_lb_lb_zone_id The canonical hosted zone ID of the load balancer (to be used in a Route 53 Alias record).
aws_security_group_lb_access_sg_arn The ARN of the security group
aws_security_group_lb_access_sg_description The description of the security group
aws_security_group_lb_access_sg_egress The egress rules.
aws_security_group_lb_access_sg_id The ID of the security group
aws_security_group_lb_access_sg_ingress The ingress rules.
aws_security_group_lb_access_sg_name The name of the security group
aws_security_group_lb_access_sg_owner_id The owner ID.
aws_security_group_lb_access_sg_vpc_id The VPC ID.
ecs_tasks_sg_arn ${var.name_prefix} ECS Tasks Security Group - The ARN of the security group
ecs_tasks_sg_description ${var.name_prefix} ECS Tasks Security Group - The description of the security group
ecs_tasks_sg_id ${var.name_prefix} ECS Tasks Security Group - The ID of the security group
ecs_tasks_sg_name ${var.name_prefix} ECS Tasks Security Group - The name of the security group
lb_http_listeners_arns List of HTTP Listeners ARNs
lb_http_listeners_ids List of HTTP Listeners IDs
lb_http_tgs_arns List of HTTP Target Groups ARNs
lb_http_tgs_ids List of HTTP Target Groups IDs
lb_http_tgs_names List of HTTP Target Groups Names
lb_https_listeners_arns List of HTTPS Listeners ARNs
lb_https_listeners_ids List of HTTPS Listeners IDs
lb_https_tgs_arns List of HTTPS Target Groups ARNs
lb_https_tgs_ids List of HTTPS Target Groups IDs
lb_https_tgs_names List of HTTPS Target Groups Names