SourceFuse's AWS Reference Architecture Terraform module leverages the terraform-aws-modules/terraform-aws-ecs GitHub repository to facilitate the deployment and management of an AWS ECS (Elastic Container Service) cluster. It streamlines the configuration of ECS tasks, services, and related components, providing a scalable and efficient solution for orchestrating containerized applications. With customizable settings for logging and load balancing the module promotes seamless deployment and management of containerized workloads on AWS.
The module assumes that upstream dependencies, namely networking dependencies, are created upstream and the values are passed into this module via mechanisms such as Terraform data source queries.
The module provisions
- ECS Cluster - we are focusing on the Fargate launch type, so we do not provision any underlying EC2 instances for the ECS launch type for the time being.
- Application Load Balancer - default port 80.
- Health Check Service - vanilla HTTP echo service that is used as the default target group for the load balancer. The purpose of the health check service is to ensure that the core infrastructure, networking, security groups, etc. are configured correctly.
- Task execution IAM role - used by downstream services for task execution.
- Tags - the module tags resources for easy reference in the AWS console.
Our approach to ECS Fargate clusters is to provision a cluster and allow downstream services to attach to it via convention based data source queries.
Before using this module, ensure you have the following:
- AWS credentials configured.
- Terraform installed.
- A working knowledge of Terraform.
- Network
- Define the Module
Initially, it's essential to define a Terraform module, which is organized as a distinct directory encompassing Terraform configuration files. Within this module directory, input variables and output values must be defined in the variables.tf and outputs.tf files, respectively. The following illustrates an example directory structure:
ecs/
|-- main.tf
|-- variables.tf
|-- outputs.tf
- Define Input Variables
Inside the variables.tf
or in *.tfvars
file, you should define values for the variables that the module requires.
- Use the Module in Your Main Configuration In your main Terraform configuration file (e.g., main.tf), you can use the module. Specify the source of the module, and version, For Example
module "ecs_cluster" {
source = "./modules/ecs_cluster"
ecs_cluster = {
name = var.ecs_cluster.name
configuration = var.ecs_cluster.configuration
create_cloudwatch_log_group = var.ecs_cluster.create_cloudwatch_log_group
service_connect_defaults = var.ecs_cluster.service_connect_defaults
settings = var.ecs_cluster.settings
}
capacity_provider = {
autoscaling_capacity_providers = var.capacity_provider.autoscaling_capacity_providers
default_capacity_provider_use_fargate = var.capacity_provider.default_capacity_provider_use_fargate
fargate_capacity_providers = var.capacity_provider.fargate_capacity_providers
}
}
- Output Values
Inside the outputs.tf
file of the module, you can define output values that can be referenced in the main configuration. For example:
output "cluster_name" {
description = "Name of the ECS Cluster"
value = module.ecs.cluster_name
}
- Execute Terraform Commands
After defining your main configuration, navigate to the directory containing your Terraform files and run the following commands:
terraform init
terraform apply
- Review and Confirm
Terraform will display a plan showing the changes it intends to make. Review the plan and confirm by typing 'yes' when prompted.
Name | Version |
---|---|
terraform | ~> 1.5 |
aws | ~> 5.0 |
No providers.
Name | Source | Version |
---|---|---|
alb | ./modules/alb | n/a |
ecs_cluster | ./modules/ecs_cluster | n/a |
ecs_service | ./modules/ecs_service | n/a |
No resources.
Name | Description | Type | Default | Required |
---|---|---|---|---|
alb | Configuration settings for the Application Load Balancer (ALB). This includes attributes related to the ALB itself, such as its name, port, protocol, and other optional settings like access logs and tags. | object({ |
n/a | yes |
alb_target_group | List of target groups to create | list(object({ |
n/a | yes |
capacity_provider | Configuration settings for the ECS capacity providers, including the capacity providers used for autoscaling and Fargate. This variable defines the properties of each capacity provider and how they are managed, such as scaling policies and termination protection. | object({ |
n/a | yes |
cidr_blocks | CIDR blocks for security group ingress rules | list(string) |
[ |
no |
ecs_cluster | The ECS-specific values to use such as cluster, service, and repository names. Keys: - cluster_name: The name of the ECS cluster. - cluster_configuration: The execute command configuration for the cluster. - cluster_settings: A list of cluster settings (e.g., container insights). Default is an empty list. - cluster_service_connect_defaults: Configures a default Service Connect namespace. - create_cloudwatch_log_group: Boolean flag to specify whether to create a CloudWatch log group for the ECS cluster. |
object({ |
n/a | yes |
ecs_service | The ECS-specific values to use such as cluster, service, and repository names. | object({ |
n/a | yes |
environment | The environment associated with the ECS service | string |
n/a | yes |
lb | ALB-related information (listening port, deletion protection, security group) | object({ |
n/a | yes |
listener_rules | List of listener rules to create | list(object({ |
n/a | yes |
task | Task-related information (vCPU, memory, # of tasks, port, and health check info.) | object({ |
n/a | yes |
vpc_id | ID of VPC in which all resources need to be created | string |
n/a | yes |
Name | Description |
---|---|
alb_name | The names of the ALBs. |
ecs_cluster_name | The name of the ECS cluster. |
ecs_service_name | The service names of the ECS services. |
ecs_task_definition_arn | The ARNs of the ECS task definitions. |
while Contributing or doing git commit please specify the breaking change in your commit message whether its major,minor or patch
For Example
git commit -m "your commit message #major"
By specifying this , it will bump the version and if you don't specify this in your commit message then by default it will consider patch and will bump that accordingly
- Configure pre-commit hooks
pre-commit install
- Tests are available in
test
directory - Configure the dependencies
cd test/ go mod init github.com/sourcefuse/terraform-aws-refarch-ecs go get github.com/gruntwork-io/terratest/modules/terraform
- Now execute the test
go test -timeout 30m
This project is authored by:
- SourceFuse ARC Team