infrablocks
terraform-aws-ecs-cluster
Ruby

Terraform module for building an ECS cluster in AWS

Last updated May 18, 2026
51
Stars
50
Forks
8
Issues
0
Stars/day
Attention Score
63
Language breakdown
Ruby 64.7%
HCL 30.7%
Shell 4.6%
Files click to expand
README

Terraform AWS ECS Cluster =========================

CircleCI

A Terraform module for building an ECS Cluster in AWS.

The ECS cluster requires:

  • An existing VPC
  • Some existing subnets
The ECS cluster consists of:
  • A cluster in ECS
  • A launch template and auto-scaling group for a cluster of ECS container
instances
  • An SSH key to connect to the ECS container instances
  • A security group for the container instances optionally allowing:
* Outbound internet access for all containers * Inbound TCP access on any port from the VPC network
  • An IAM role and policy for the container instances allowing:
* ECS interactions * ECR image pulls * S3 object fetches * Logging to cloudwatch
  • An IAM role and policy for ECS services allowing:
* Elastic load balancer registration / deregistration * EC2 describe actions and security group ingress rule creation
  • A CloudWatch log group
Diagram of infrastructure managed by this module

Usage


To use the module, include something like the following in your Terraform configuration:

-terraform
module "ecs_cluster" {
  source  = "infrablocks/ecs-cluster/aws"
  version = "5.0.0"

region = "eu-west-2" vpc_id = "vpc-fb7dc365" subnet_ids = [ "subnet-eb32c271", "subnet-64872d1f" ]

component = "important-component" deployment_identifier = "production"

cluster_name = "services" clusterinstancesshpublickeypath = "~/.ssh/idrsa.pub" clusterinstancetype = "t3.small"

clusterminimumsize = 2 clustermaximumsize = 10 clusterdesiredcapacity = 4 }

As mentioned above, the ECS cluster deploys into an existing base network. Whilst the base network can be created using any mechanism you like, the AWS Base Networking module will create everything you need. See the docs for usage instructions.

See the Terraform registry entry for more details.

Inputs

| Name | Description | Default | Required | |-----------------------------------------------------|--------------------------------------------------------------------------------------------------------|:-----------------:|:-------------------------------:| | region | The region into which to deploy the cluster | - | yes | | vpc_id | The ID of the VPC into which to deploy the cluster | - | yes | | subnet_ids | The IDs of the subnets for container instances | - | yes | | component | The component this cluster will contain | - | yes | | deployment_identifier | An identifier for this instantiation | - | yes | | tags | A map of additional tags to add to all resources | - | no | | cluster_name | The name of the cluster to create | default | yes | | clusterinstancesshpublickey_path | The path to the public key to use for the container instances | - | yes | | clusterinstancetype | The instance type of the container instances | t2.medium | yes | | clusterinstancerootblockdevice_size | The size in GB of the root block device on cluster instances | 30 | yes | | clusterinstancerootblockdevice_path | Path of the instance root block storage volume | /dev/xvda | yes | | clusterinstancerootblockdevice_type | The type of the root block device on cluster instances ('standard', 'gp2', or 'io1') | standard | yes | | clusterinstanceuserdatatemplate | The contents of a template for container instance user data | see user-data | no | | clusterinstanceami | AMI for the container instances | ECS optimised AMI | yes | | clusterinstancemetadata_options | A map of metadata options for cluster instances. | - | no | | clusterinstanceiampolicycontents | The contents of the cluster instance IAM policy | see policies | no | | clusterserviceiampolicycontents | The contents of the cluster service IAM policy | see policies | no | | clusterminimumsize | The minimum size of the ECS cluster | 1 | yes | | clustermaximumsize | The maximum size of the ECS cluster | 10 | yes | | clusterdesiredcapacity | The desired capacity of the ECS cluster | 3 | yes | | associatepublicip_addresses | Whether or not to associate public IP addresses with ECS container instances | false | no | | includedefaultingress_rule | Whether or not to include the default ingress rule on the ECS container instances security group | true | no | | includedefaultegress_rule | Whether or not to include the default egress rule on the ECS container instances security group | true | no | | defaultingresscidrs | The CIDRs allowed access to containers | ["10.0.0.0/8"] | if includedefaultingress_rule | | defaultegresscidrs | The CIDRs accessible from containers | ["0.0.0.0/0"] | if includedefaultegress_rule | | security_groups | The list of security group IDs to associate with the cluster in addition to the default security group | [] | no | | clusterloggroup_retention | The number of days logs will be retained in the CloudWatch log group of the cluster (0 = unlimited) | 0 | no | | enabledetailedmonitoring | Enable detailed monitoring of EC2 instance(s) | true | no | | enablecontainerinsights | Whether or not to enable container insights on the ECS cluster | false | no | | protectclusterinstancesfromscale_in | Whether or not to protect cluster instances in the autoscaling group from scale in | false | no | | includeasgcapacity_provider | Whether or not to add the created ASG as a capacity provider for the ECS cluster | false | no | | asgcapacityprovidermanagetermination_protection | Whether or not to allow ECS to manage termination protection for the ASG capacity provider | true | no | | asgcapacityprovidermanagescaling | Whether or not to allow ECS to manage scaling for the ASG capacity provider | true | no | | asgcapacityproviderminimumscalingstepsize | The minimum scaling step size for ECS managed scaling of the ASG capacity provider | 1 | no | | asgcapacityprovidermaximumscalingstepsize | The maximum scaling step size for ECS managed scaling of the ASG capacity provider | 1000 | no | | asgcapacityprovidertargetcapacity | The target capacity, as a percentage from 1 to 100, for the ASG capacity provider | 100 | no | | clusterinstanceenableebsvolume_encryption | Determines whether encryption is enabled on the EBS volume | true | no | | clusterinstanceebsvolumekmskeyid | KMS key to use for encryption of the EBS volume when enabled | alias/aws/ebs | no |

Notes:

  • By default, the latest available Amazon Linux 2 AMI is used.
  • For Amazon Linux 1 AMIs use version <= 0.6.0 of this module for terraform 0.11
or version = 1.0.0 for terraform 0.12.
  • When a specific AMI is provided via clusterinstanceami, only the root
block device can be customised, using the clusterinstancerootblockdevice_size and clusterinstancerootblockdevice_type variables.
  • The user data template will get the cluster name as cluster_name. If
none is supplied, a default will be used.

Outputs

| Name | Description | |------------------------|----------------------------------------------------------------------------------| | cluster_id | The ID of the created ECS cluster | | cluster_name | The name of the created ECS cluster | | cluster_arn | The ARN of the created ECS cluster | | autoscalinggroupname | The name of the autoscaling group for the ECS container instances | | launchtemplatename | The name of the launch template for the ECS container instances | | launchtemplateid | The ID of the launch template for the ECS container instances | | securitygroupid | The ID of the default security group associated with the ECS container instances | | instancerolearn | The ARN of the container instance role | | instanceroleid | The ID of the container instance role | | instancepolicyarn | The ARN of the container instance policy | | instancepolicyid | The ID of the container instance policy | | servicerolearn | The ARN of the ECS service role | | serviceroleid | The ID of the ECS service role | | servicepolicyarn | The ARN of the ECS service policy | | servicepolicyid | The ID of the ECS service policy | | log_group | The name of the default log group for the cluster |

Compatibility

This module is compatible with Terraform versions greater than or equal to Terraform 1.0.

Required Permissions

  • iam:GetPolicy
  • iam:GetPolicyVersion
  • iam:ListPolicyVersions
  • iam:ListEntitiesForPolicy
  • iam:CreatePolicy
  • iam:DeletePolicy
  • iam:GetRole
  • iam:PassRole
  • iam:CreateRole
  • iam:DeleteRole
  • iam:ListRolePolicies
  • iam:AttachRolePolicy
  • iam:DetachRolePolicy
  • iam:GetInstanceProfile
  • iam:CreateInstanceProfile
  • iam:ListInstanceProfilesForRole
  • iam:AddRoleToInstanceProfile
  • iam:RemoveRoleFromInstanceProfile
  • iam:DeleteInstanceProfile
  • ec2:DescribeSecurityGroups
  • ec2:CreateSecurityGroup
  • ec2:DeleteSecurityGroup
  • ec2:AuthorizeSecurityGroupIngress
  • ec2:AuthorizeSecurityGroupEgress
  • ec2:RevokeSecurityGroupEgress
  • ec2:ImportKeyPair
  • ec2:DescribeKeyPairs
  • ec2:DeleteKeyPair
  • ec2:CreateTags
  • ec2:DescribeImages
  • ec2:DescribeNetworkInterfaces
  • ecs:DescribeClusters
  • ecs:CreateCluster
  • ecs:DeleteCluster
  • autoscaling:DescribeLaunchConfigurations
  • autoscaling:CreateLaunchConfiguration
  • autoscaling:DeleteLaunchConfiguration
  • autoscaling:DescribeScalingActivities
  • autoscaling:DescribeAutoScalingGroups
  • autoscaling:CreateAutoScalingGroup
  • autoscaling:UpdateAutoScalingGroup
  • autoscaling:DeleteAutoScalingGroup
  • logs:CreateLogGroup
  • logs:DescribeLogGroups
  • logs:ListTagsLogGroup
  • logs:DeleteLogGroup
Development

Machine Requirements

In order for the build to run correctly, a few tools will need to be installed on your development machine:

  • Ruby (3.1)
  • Bundler
  • git
  • git-crypt
  • gnupg
  • direnv
  • aws-vault

Mac OS X Setup

Installing the required tools is best managed by homebrew.

To install homebrew:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Then, to install the required tools:

# ruby
brew install rbenv
brew install ruby-build
echo 'eval "$(rbenv init - bash)"' >> ~/.bash_profile
echo 'eval "$(rbenv init - zsh)"' >> ~/.zshrc
eval "$(rbenv init -)"
rbenv install 3.1.1
rbenv rehash
rbenv local 3.1.1
gem install bundler

git, git-crypt, gnupg

brew install git brew install git-crypt brew install gnupg

aws-vault

brew cask install

direnv

brew install direnv echo "$(direnv hook bash)" >> ~/.bash_profile echo "$(direnv hook zsh)" >> ~/.zshrc eval "$(direnv hook $SHELL)"

direnv allow <repository-directory>

Running the build

Running the build requires an AWS account and AWS credentials. You are free to configure credentials however you like as long as an access key ID and secret access key are available. These instructions utilise aws-vault which makes credential management easy and secure.

To provision module infrastructure, run tests and then destroy that infrastructure, execute:

aws-vault exec <profile> -- ./go

To provision the module prerequisites:

aws-vault exec <profile> -- ./go deployment:prerequisites:provision[<deployment_identifier>]

To provision the module contents:

aws-vault exec <profile> -- ./go deployment:root:provision[<deployment_identifier>]

To destroy the module contents:

aws-vault exec <profile> -- ./go deployment:root:destroy[<deployment_identifier>]

To destroy the module prerequisites:

aws-vault exec <profile> -- ./go deployment:prerequisites:destroy[<deployment_identifier>]

Configuration parameters can be overridden via environment variables:

DEPLOYMENT_IDENTIFIER=testing aws-vault exec <profile> -- ./go

When a deployment identifier is provided via an environment variable, infrastructure will not be destroyed at the end of test execution. This can be useful during development to avoid lengthy provision and destroy cycles.

Common Tasks

Generating an SSH key pair

To generate an SSH key pair:

ssh-keygen -m PEM -t rsa -b 4096 -C integration-test@example.com -N '' -f config/secrets/keys/bastion/ssh

Generating a self-signed certificate

To generate a self signed certificate:

openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365

To decrypt the resulting key:

openssl rsa -in key.pem -out ssl.key

Managing CircleCI keys

To encrypt a GPG key for use by CircleCI:

openssl aes-256-cbc \
  -e \
  -md sha1 \
  -in ./config/secrets/ci/gpg.private \
  -out ./.circleci/gpg.private.enc \
  -k "<passphrase>"

To check decryption is working correctly:

openssl aes-256-cbc \
  -d \
  -md sha1 \
  -in ./.circleci/gpg.private.enc \
  -k "<passphrase>"

Contributing


Bug reports and pull requests are welcome on GitHub at https://github.com/infrablocks/terraform-aws-assumable-roles-policy. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License


The library is available as open source under the terms of the MIT License.

🔗 More in this category

© 2026 GitRepoTrend · infrablocks/terraform-aws-ecs-cluster · Updated daily from GitHub