Skip to main content

Overview

ComputeFactory is Fjall’s factory for compute resources. One call covers three compute types: ECS containers, Lambda functions, and standalone EC2 instances. ComputeFactory.build(id, props) returns a builder that app.addCompute() invokes. The type field is the required discriminant, so props are checked against the matching overload at compile time.

Basic Usage

Omitting containers gives the service one implicit container on the application’s default ECR image, with no port and no load-balancer attachment. That is the normal shape for a Fjall-scaffolded application.

Compute Types

ECS (Elastic Container Service)

Deploy containerised services with three capacity providers.

Fargate Spot (cost savings)

EC2-backed ECS

Lambda Functions

Code deployment requires code. Import Code from the package root, which re-exports it from aws-cdk-lib/aws-lambda.
Container deployment builds from a Dockerfile:
Or track an image you build and push yourself:
ecrRepository is optional. With docker set it defaults to the application’s shared container registry, the same repository ECS services push into. Supply it explicitly only when you manage the image out of band.

EC2 Instances

Deploy an Auto Scaling group of virtual machines:
The group is reached over Session Manager unless you set sshAccess, which opens port 22 to the ranges you name and nothing else:
allowedIpCidrs is the only required field. Omit keyName to have a key pair created, and public to keep the group in private subnets. "0.0.0.0/0" deploys with a synth warning. See EC2 Instance for the full contract and what synth rejects.

ECS Reference

Compute-Root Parameters

Service Configuration

Each entry in the services array:
The service-level cpu and memoryLimitMiB fields are Fargate only. On an EC2 service, size the container with ec2Config.memoryLimitMiB. Setting either on EC2 capacity is rejected at synth, naming the field and the service.

Container Configuration

Each entry in the containers array:
No container health check is injected by default. Without one, and without an ALB target group, ECS treats a container as healthy the moment it is RUNNING, so a post-start exit is invisible to the deployment circuit breaker. Declare a healthCheck on workers whose liveness should gate deployments.

Routing Configuration

routing also accepts an array, so one service can answer several rules through one target group:

Scaling Configuration

Auto-scaling is on by default. Omitting scaling gives minCapacity = desiredCount and maxCapacity = max(desiredCount + 1, 3), so a service left at the default desiredCount: 2 scales between two and three tasks. Set scaling: false to opt out. scalingType takes the ScalingType enum, not a string:

Queue-backed workers

ScalingType.QUEUE is the only mode that scales to zero. CPU and memory target-tracking publish no datapoint at zero tasks, so they cannot wake an idle service. Queue scaling alarms on SQS backlog instead, which publishes every minute regardless of running tasks.

Cluster Configuration

Cluster settings control the shared ALB for every service in the cluster.

Load balancer options

Custom Domains

domainConfig needs a domainName plus a zone source. Fjall never creates a hosted zone inside an application stack, because such a zone is undelegated: certificate validation hangs until CloudFormation rolls back, and destroying the stack deletes the zone.
A domain or domainConfig.domainName with no resolvable zone source fails at synth with an error naming both cures. There is no certificateArn, hostedZoneId, or hostedZoneName field on domainConfig.

Fjall-managed domain

This is the normal path. Run fjall domain deploy example.com once, then name the domain. The CLI injects the zone id and certificate ARN as CDK context at deploy time, so no literal zone id lives in your infrastructure file.

Bring your own hosted zone

Import a zone you already own. HostedZoneFactory.import takes a Fjall stack, the zone id, and the zone name.
For a bare CDK synth in the same account and region, managedDomain can instead import the domain stack’s CloudFormation exports:

EC2 Capacity Configuration

ec2Config applies when a service sets capacityProvider: "EC2".

Services that share an Auto Scaling group

slot is the sharing key. Two services declaring the same slot, including two services that both omit it and land on "primary", share one Auto Scaling group built from the first service’s ec2Config. Every other ec2Config field must then agree across those services. A mismatch is rejected at synth naming both services and each differing field, because the second service’s value would otherwise be discarded silently.
Two cures, meaning different things:
  • Align the values when the services were always meant to share capacity. The group already runs on the first service’s numbers, so copying those onto the second changes nothing. Copying the second service’s numbers onto the first resizes a running group.
  • Give one service its own slot when they were meant to scale independently. That creates a second Auto Scaling group, which is a real cost change.
machineImage, userData and blockDevices compare on presence only, set against unset, because CDK gives them no cheap structural identity. Two services that each supply an equivalent userData are accepted.

Memory fit

Synth rejects an EC2 service whose container hard-memory limits sum to at least the instance type’s nominal memory, whether instanceType was set or defaulted. Registered memory always sits below nominal memory because the ECS agent and the OS reserve some, so such a task can never be placed on any instance count. The stack deploys cleanly at desiredCount: 0 and then wedges at the first scale-up. t4g.micro registers roughly 896 MiB, below the 1024 MiB default container limit. Explicit t4g.micro stays supported, which is what the free-tier Tinkerer preset pins with memoryLimitMiB: 400. Keep the container limits comfortably under 896 MiB total.

Lambda Reference

architecture takes the Architecture enum from aws-cdk-lib/aws-lambda. For a container deployment with docker set it also selects the platform fjall deploy builds for, so set it here rather than passing --platform to Docker.

Function URLs

EC2 Reference

Connections

connections wires a service or function to databases, buckets, queues, and tables. Fjall creates the security-group rules for connectable resources and the IAM grants for the rest, scoped to that service alone.
Read connection details straight from the resource constructs for environment variables and secrets:

Multi-Container Service

Cost Optimisation

Development and batch workloads:
Production right-sizing:
Three levers carry most of the saving:

Next Steps

ECS Cluster Resources

Configure ECS clusters for advanced workloads.

Lambda Function Resources

Configure Lambda functions at the resource layer.

Database Factory

Provision Aurora, RDS, DynamoDB, and ClickHouse databases.

Standard Pattern

Compose full applications from these factories.