0
0
TerraformHow-ToBeginner · 3 min read

How to Create an ECS Cluster with Terraform

To create an ECS cluster with terraform, use the aws_ecs_cluster resource specifying a cluster name. Apply the configuration with terraform init and terraform apply to provision the cluster in AWS.
📐

Syntax

The aws_ecs_cluster resource defines an ECS cluster in Terraform. You specify a name for the cluster. Optionally, you can add tags or settings.

  • resource "aws_ecs_cluster" "example": Declares the ECS cluster resource.
  • name: The name of your ECS cluster.
terraform
resource "aws_ecs_cluster" "example" {
  name = "my-ecs-cluster"
}
💻

Example

This example creates a simple ECS cluster named my-ecs-cluster. Running terraform apply will create the cluster in your AWS account.

terraform
provider "aws" {
  region = "us-east-1"
}

resource "aws_ecs_cluster" "my_cluster" {
  name = "my-ecs-cluster"
}
Output
Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Outputs: ecs_cluster_name = my-ecs-cluster
⚠️

Common Pitfalls

Common mistakes when creating ECS clusters with Terraform include:

  • Not setting the AWS provider region, causing errors or resources in unexpected regions.
  • Using the same cluster name multiple times, which can cause conflicts.
  • Forgetting to run terraform init before terraform apply.

Always check your AWS credentials and region settings before applying.

terraform
/* Wrong: Missing provider region */
provider "aws" {}

resource "aws_ecs_cluster" "bad_cluster" {
  name = "my-ecs-cluster"
}

/* Right: Specify region */
provider "aws" {
  region = "us-east-1"
}

resource "aws_ecs_cluster" "good_cluster" {
  name = "my-ecs-cluster"
}
📊

Quick Reference

Remember these tips when creating ECS clusters with Terraform:

  • Use aws_ecs_cluster resource with a unique name.
  • Set the AWS provider region explicitly.
  • Run terraform init before applying changes.
  • Check AWS credentials and permissions.

Key Takeaways

Use the aws_ecs_cluster resource with a unique name to create an ECS cluster.
Always specify the AWS provider region to avoid deployment issues.
Run terraform init before terraform apply to initialize your working directory.
Check your AWS credentials and permissions before applying Terraform changes.