cluster
View SourceECS clusters in AWS provide compute capacity for running containerized applications. Stackattack’s cluster
component provides an easy way to set up working ECS clusters for running applications on EC2 instances with auto-scaling and private inter-service communication.
Stackattack creates ECS clusters with:
- EC2 instances used for compute. By default, the configuration will use spot instances. Pass
noSpot: true
to disable spot instances, oronDemandPercentage
with a percentage value to split your EC2 instances between on demand and spot. The number of instances will always match the requirements of your cluster (within the constraints you set viaminSize
(default 0) andmaxSize
(default 1)). Currently Fargate is not supported. - A private DNS namespace is created so that your services can communicate internally via ECS service discovery. ECS service connect is currently not supported.
import * as saws from "@stackattack/aws";
const ctx = saws.context();const vpc = saws.vpc(ctx);const compute = saws.cluster(ctx, { network: vpc.network("private") });
export const clusterName = compute.cluster.name;
After deploying a cluster, you can deploy services into it to run code in docker containers.
SSH Access to EC2 Instances: NOTE: if your instances are within a private subnet (as is likely the case), you need a way to access your instances. If you created your vpc with the vpc component, an EC2 instance connect endpoint is set up automatically.
# Connect to an instance using Instance Connect (no key pairs needed)aws ec2-instance-connect ssh --instance-id i-1234567890abcdef0 --region us-east-1# If you do not have an EC2 instance connect endpoint but have direct access to your instance (e.g. via a VPN), pass --connection-type directaws ec2-instance-connect ssh --instance-id i-1234567890abcdef0 --region us-east-1 --connection-type direct
Related Components
Section titled “Related Components”Clusters work together with other Stackattack components:
- vpc - Provides networking foundation with private/public subnets
- service - Runs containerized applications on the cluster
- load-balancer - Routes external traffic to services on the cluster
ECS cluster costs depend on the underlying EC2 instances and are usage-based:
-
EC2 instances - This will depend on your configuration. Your auto-scaling cluster will scale up and down based on the desired capacity of the services you have deployed, meaning instances will be terminated if they are unused and created as you deploy and scale your services. By default Stackattack will use spot instances with an ARM architecture, 1-2 CPUs, 2-4 GB of memory, and a 2:1 memory:cpu ratio (this uses a mixed instances policy so the specific instance type(s) that will be launched is not known. This allows for greater availability of spot instances, as it does not rely as much on the capacity of any one specific instance type ). This can be configured with the
instances
parameter. See the Spot Instance Pricing documentation for specific figures. -
EBS storage - Each instance gets 25GB of storage by default (~$2/month per instance). Block devices are deleted when instances are terminated.
-
Data transfer - Service-to-service communication within the VPC is free. External data transfer follows standard AWS rates (~$0.09/GB out).
-
Service Discovery - The private DNS namespace is free.
Cost optimization strategies:
- Use spot instances for non-critical workloads (up to 90% savings)
- Enable cluster auto-scaling to scale to zero during low usage
- Monitor instance utilization and rightsize instance types
- Use service placement strategies to maximize instance utilization
See EC2 Pricing for current rates.
cluster
Section titled “cluster”Creates a complete ECS cluster with capacity provider, auto scaling group, and private namespace.
function cluster(ctx: Context, args: ClusterArgs): ClusterOutput
Parameters
Section titled “Parameters”ctx
(Context
) - The context for resource naming and taggingargs
(ClusterArgs
) - Arguments for cluster configuration
Returns
Section titled “Returns”- (
ClusterOutput
) - Creates a complete ECS cluster with capacity provider, auto scaling group, and private namespace.
Functions
Section titled “Functions”clusterCapacity
Section titled “clusterCapacity”Creates ECS cluster capacity including auto scaling group and capacity provider.
function clusterCapacity(ctx: Context, args: ClusterCapacityArgs): { autoScalingGroup: Group; capacityProvider: CapacityProvider }
Parameters
Section titled “Parameters”ctx
(Context
) - The context for resource naming and taggingargs
(ClusterCapacityArgs
) - Arguments for capacity configuration
Returns
Section titled “Returns”- (
{ autoScalingGroup: Group; capacityProvider: CapacityProvider }
) - Creates ECS cluster capacity including auto scaling group and capacity provider.
clusterInstanceInitScript
Section titled “clusterInstanceInitScript”Generates a bash initialization script for ECS cluster instances.
function clusterInstanceInitScript(args: ClusterInstanceInitScriptArgs): Output<string>
Parameters
Section titled “Parameters”args
(ClusterInstanceInitScriptArgs
) - Arguments containing cluster and parameter information
Returns
Section titled “Returns”- (
Output<string>
) - Generates a bash initialization script for ECS cluster instances.
clusterInstanceRole
Section titled “clusterInstanceRole”Creates an IAM role for ECS cluster instances with necessary policies attached.
function clusterInstanceRole(ctx: Context, args?: ClusterInstanceRoleArgs): Role
Parameters
Section titled “Parameters”ctx
(Context
) - The context for resource naming and taggingargs?
(ClusterInstanceRoleArgs
) - Optional arguments for role configuration
Returns
Section titled “Returns”- (
Role
) - Creates an IAM role for ECS cluster instances with necessary policies attached.
clusterSecurityGroup
Section titled “clusterSecurityGroup”Creates a security group for cluster instances with SSH access and full egress.
function clusterSecurityGroup(ctx: Context, args: ClusterSecurityGroupArgs): SecurityGroup
Parameters
Section titled “Parameters”ctx
(Context
) - The context for resource naming and taggingargs
(ClusterSecurityGroupArgs
) - Arguments for security group configuration
Returns
Section titled “Returns”- (
SecurityGroup
) - Creates a security group for cluster instances with SSH access and full egress.
clusterToIds
Section titled “clusterToIds”Converts a ClusterOutput to ClusterIds by extracting resource identifiers.
function clusterToIds(cluster: ClusterOutput): ClusterIds
Parameters
Section titled “Parameters”cluster
(ClusterOutput
) - The cluster output containing all resources
Returns
Section titled “Returns”- (
ClusterIds
) - Converts a ClusterOutput to ClusterIds by extracting resource identifiers.
getCapacityProviderId
Section titled “getCapacityProviderId”Extracts the capacity provider ID from a CapacityProviderInput.
function getCapacityProviderId(input: Input<CapacityProviderInput>): Output<string>
Parameters
Section titled “Parameters”input
(Input<
CapacityProviderInput
>
) - The capacity provider input to extract the ID from
Returns
Section titled “Returns”- (
Output<string>
) - Extracts the capacity provider ID from a CapacityProviderInput.
getClusterAttributes
Section titled “getClusterAttributes”Retrieves the full cluster attributes from a ClusterInput.
function getClusterAttributes(input: Input<ClusterInput>): Output<Cluster | GetClusterResult>
Parameters
Section titled “Parameters”input
(Input<
ClusterInput
>
) - The cluster input to get attributes from
Returns
Section titled “Returns”- (
Output<Cluster | GetClusterResult>
) - Retrieves the full cluster attributes from a ClusterInput.
getClusterId
Section titled “getClusterId”Extracts the cluster ID from a ClusterInput.
function getClusterId(input: Input<ClusterInput>): Output<string>
Parameters
Section titled “Parameters”input
(Input<
ClusterInput
>
) - The cluster input to extract the ID from
Returns
Section titled “Returns”- (
Output<string>
) - Extracts the cluster ID from a ClusterInput.
getHttpNamespaceId
Section titled “getHttpNamespaceId”Extracts the HTTP namespace ID from an HttpNamespaceInput.
function getHttpNamespaceId(input: Input<HttpNamespaceInput>): Output<string>
Parameters
Section titled “Parameters”input
(Input<
HttpNamespaceInput
>
) - The HTTP namespace input to extract the ID from
Returns
Section titled “Returns”- (
Output<string>
) - Extracts the HTTP namespace ID from an HttpNamespaceInput.
getInstanceTypeArchitecture
Section titled “getInstanceTypeArchitecture”Gets the architecture (e.g., x86_64, arm64) for a given EC2 instance type.
function getInstanceTypeArchitecture(instanceType: Input<string>): Output<string>
Parameters
Section titled “Parameters”instanceType
(Input<string>
) - The EC2 instance type to get the architecture for
Returns
Section titled “Returns”- (
Output<string>
) - Gets the architecture (e.g., x86_64, arm64) for a given EC2 instance type.
getPrivateDnsNamespaceAttributes
Section titled “getPrivateDnsNamespaceAttributes”Retrieves the full private DNS namespace attributes from a PrivateDnsNamespaceInput.
function getPrivateDnsNamespaceAttributes(input: Input<PrivateDnsNamespaceInput>): Output<PrivateDnsNamespace | GetDnsNamespaceResult>
Parameters
Section titled “Parameters”input
(Input<
PrivateDnsNamespaceInput
>
) - The private DNS namespace input to get attributes from
Returns
Section titled “Returns”- (
Output<PrivateDnsNamespace | GetDnsNamespaceResult>
) - Retrieves the full private DNS namespace attributes from a PrivateDnsNamespaceInput.
getPrivateDnsNamespaceId
Section titled “getPrivateDnsNamespaceId”Extracts the private DNS namespace ID from a PrivateDnsNamespaceInput.
function getPrivateDnsNamespaceId(input: Input<PrivateDnsNamespaceInput>): Output<string>
Parameters
Section titled “Parameters”input
(Input<
PrivateDnsNamespaceInput
>
) - The private DNS namespace input to extract the ID from
Returns
Section titled “Returns”- (
Output<string>
) - Extracts the private DNS namespace ID from a PrivateDnsNamespaceInput.
Interfaces
Section titled “Interfaces”ClusterArgs
Section titled “ClusterArgs”Arguments for creating a complete ECS cluster with capacity.
Properties
Section titled “Properties”diskSize?
(number
) - Size of the root disk in GBinstances?
(ClusterInstancesConfig
) - Instance configuration (type or requirements)maxSize?
(Input<number>
) - Maximum number of instances in the auto scaling group; defaults tominSize
unlessminSize
is 0, then it defaults to 1minSize?
(Input<number>
) - Minimum number of instances in the auto scaling group; defaults to 0network
(NetworkInput
) - Network configuration for the clusternoPrefix?
(boolean
) - Whether to skip adding a prefix to resource namesnoSpot?
(boolean
) - Whether to disable spot instancesonDemandBase?
(number
) - Number of on-demand instances to maintain as base capacityonDemandPercentage?
(number
) - Percentage of on-demand instances above base capacitysourceSecurityGroupId?
(Input<string>
) - Security group ID that should be allowed SSH access to the instancesspotAllocationStrategy?
(string
) - Strategy for allocating spot instances
ClusterCapacityArgs
Section titled “ClusterCapacityArgs”Arguments for creating cluster capacity including network and cluster references.
Properties
Section titled “Properties”cluster
(Input<
ClusterInput
>
) - The ECS cluster to create capacity fordiskSize?
(number
) - Size of the root disk in GBinstances?
(ClusterInstancesConfig
) - Instance configuration (type or requirements)maxSize?
(Input<number>
) - Maximum number of instances in the auto scaling group; defaults tominSize
unlessminSize
is 0, then it defaults to 1minSize?
(Input<number>
) - Minimum number of instances in the auto scaling group; defaults to 0network
(NetworkInput
) - Network configuration for the clusternoPrefix?
(boolean
) - Whether to skip adding a prefix to resource namesnoSpot?
(boolean
) - Whether to disable spot instancesonDemandBase?
(number
) - Number of on-demand instances to maintain as base capacityonDemandPercentage?
(number
) - Percentage of on-demand instances above base capacitysourceSecurityGroupId?
(Input<string>
) - Security group ID that should be allowed SSH access to the instancesspotAllocationStrategy?
(string
) - Strategy for allocating spot instances
ClusterCapacityConfig
Section titled “ClusterCapacityConfig”Configuration for cluster capacity and scaling behavior.
Properties
Section titled “Properties”diskSize?
(number
) - Size of the root disk in GBinstances?
(ClusterInstancesConfig
) - Instance configuration (type or requirements)maxSize?
(Input<number>
) - Maximum number of instances in the auto scaling group; defaults tominSize
unlessminSize
is 0, then it defaults to 1minSize?
(Input<number>
) - Minimum number of instances in the auto scaling group; defaults to 0noPrefix?
(boolean
) - Whether to skip adding a prefix to resource namesnoSpot?
(boolean
) - Whether to disable spot instancesonDemandBase?
(number
) - Number of on-demand instances to maintain as base capacityonDemandPercentage?
(number
) - Percentage of on-demand instances above base capacitysourceSecurityGroupId?
(Input<string>
) - Security group ID that should be allowed SSH access to the instancesspotAllocationStrategy?
(string
) - Strategy for allocating spot instances
ClusterIds
Section titled “ClusterIds”Interface containing the IDs of all cluster-related resources.
Properties
Section titled “Properties”autoScalingGroup
(Output<string>
) - The auto scaling group IDcapacityProvider
(Output<string>
) - The capacity provider namecluster
(Output<string>
) - The cluster IDprivateNamespace
(Output<string>
) - The private namespace name
ClusterInstanceInitScriptArgs
Section titled “ClusterInstanceInitScriptArgs”Arguments for generating a cluster instance initialization script.
Properties
Section titled “Properties”cluster
(Input<
ClusterInput
>
) - The ECS cluster to joinparamName
(Input<string>
) - Name of the SSM parameter containing CloudWatch agent configuration
ClusterInstanceRoleArgs
Section titled “ClusterInstanceRoleArgs”Arguments for creating a cluster instance role.
Properties
Section titled “Properties”noPrefix?
(boolean
) - Whether to skip adding a prefix to the resource name
ClusterInstanceTypeConfig
Section titled “ClusterInstanceTypeConfig”Configuration for using a specific EC2 instance type.
Properties
Section titled “Properties”type
(Input<string>
) - The specific EC2 instance type to use
ClusterOutput
Section titled “ClusterOutput”Output interface containing all cluster-related resources.
Properties
Section titled “Properties”autoScalingGroup
(Group
) - The auto scaling group resourcecapacityProvider
(CapacityProvider
) - The capacity provider resourcecluster
(Cluster
) - The ECS cluster resourceprivateNamespace
(PrivateDnsNamespace
) - The private DNS namespace resource
ClusterRequirementsConfig
Section titled “ClusterRequirementsConfig”Configuration for using instance requirements instead of specific instance types.
Properties
Section titled “Properties”allowNoEniTrunking?
(boolean
) - Whether to allow instance types that don’t support ENI trunkingarchitecture
(Input<string>
) - The CPU architecture (e.g., x86_64, arm64)
ClusterResourcesInput
Section titled “ClusterResourcesInput”Input interface for cluster resources.
Properties
Section titled “Properties”capacityProvider
(Input<
CapacityProviderInput
>
) - The capacity provider for the clustercluster
(Input<
ClusterInput
>
) - The ECS clusterprivateNamespace?
(Input<
PrivateDnsNamespaceInput
>
) - Optional private DNS namespace for service discovery
ClusterSecurityGroupArgs
Section titled “ClusterSecurityGroupArgs”Arguments for creating a cluster security group.
Properties
Section titled “Properties”noInstanceConnect?
(boolean
) - Indicate whether the instances should be configured to allow SSH traffic from EC2 instance connectnoPrefix?
(boolean
) - Whether to skip adding a prefix to the resource namesourceSecurityGroupId?
(Input<string>
) - Source security group ID to allow access from (defaults to VPC default security group)vpc
(Input<
VpcInput
>
) - The VPC to create the security group in
CapacityProviderInput
Section titled “CapacityProviderInput”Union type representing different ways to specify an ECS capacity provider. Can be a capacity provider name (string) or an actual CapacityProvider resource.
type CapacityProviderInput = string | aws.ecs.CapacityProvider
ClusterInput
Section titled “ClusterInput”Union type representing different ways to specify an ECS cluster. Can be a cluster name (string), an actual Cluster resource, cluster data, or cluster output.
type ClusterInput = string | aws.ecs.Cluster | aws.ecs.GetClusterResult | ClusterOutput
HttpNamespaceInput
Section titled “HttpNamespaceInput”Union type representing different ways to specify an HTTP namespace. Can be a namespace name (string), an actual HttpNamespace resource, or namespace data.
type HttpNamespaceInput = string | aws.servicediscovery.HttpNamespace | aws.servicediscovery.GetHttpNamespaceResult
PrivateDnsNamespaceInput
Section titled “PrivateDnsNamespaceInput”Union type representing different ways to specify a private DNS namespace. Can be a namespace name (string) or an actual PrivateDnsNamespace resource.
type PrivateDnsNamespaceInput = string | aws.servicediscovery.PrivateDnsNamespace