Here’s a sketch of how you can wire all of this together in SST, with pointers to the docs for each piece:
────────────────────────────────────────────────────────────────────
tsCopy code// sst.config.ts
import * as sst from "sst";
import * as ec2 from "aws-cdk-lib/aws-ec2";
export default {
config() {
return {
name: "my-elixir-app",
region: "us-east-1",
};
},
stacks(app) {
ack(MyStack);
},
};
function ({ stack }) {
//
// 1) VPC
//
// Creates a VPC with 2 AZs, private subnets, NAT gateways, etc.
// Tasks and RDS will live inside it. :contentReference[oaicite:0]{index=0}
const vpc = new s.Vpc(stack, "Vpc");
//
// 2) ECS Cluster
//
// An ECS cluster in that VPC. Add Services (below) to it. :contentReference[oaicite:1]{index=1}
const cluster = new sst.aws.Cluster(stack, "Cluster", { vpc });
//
// 3) Postgres(-compatible) database
//
// RDS Postgres in private subnets. Locked down by default. :contentReference[oaicite:2]{index=2}
const db = new sst.aws.Postgres(stack, "Database", {
vpc,
// you can tweak instance size, multiAz, proxy, etc.
});
//
// 4) Tailscale exit-node + connector on VPC
//
// We spin up an EC2 in a private subnet and install Tailscale.
// Store your key in Secrets Manager and pull it via SST Secret.
const tailscaleKey = new sst.Secret(stack, "TailscaleAuthKey");
const tsGroup = new ec2.SecurityGroup(stack, "TSG", { vpc });
const tailscaleNode = new ec2.Instance(stack, "TailscaleNode", {
vpc,
vpcSubnets: { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS },
instanceType: ec2.InstanceType.of(ec2.InstanceClass.T3, ec2.InstanceSize.MICRO),
machineImage: ec2.MachineImage.latestAmazonLinux(),
securityGroup: tsGroup,
});
tailscaleNode.userData.addCommands(
"curl -fsSL | sh",
tailscale up --authkey ${tailscaleKey.value} --advertise-exit-node
);
//
// 5) Elixir containers clustered on ECS + zero cold starts
//
// Fargate services are “always-running” by default—no cold starts.
// We build your Dockerfile (in ../backend) and point it at port 4000.
// Link the DB for IAM permissions, and inject secrets/env-vars.
//
// Auto-scaling, health checks, and sidecars are all configurable here.
//
const service = new sst.aws.Service(stack, "AppService", {
cluster,
image: {
context: "../backend",
dockerfile: "Dockerfile",
},
cpu: "512",
memory: "1024",
containers: [
{
name: "app",
port: 4000,
environment: {
DATABASE_URL: db.url,
TAILSCALE_AUTH_KEY: tailscaleKey.value,
: new sst.Secret(stack, "CFToken").value,
},
secrets: {
DB_PASSWORD: db.password,
},
},
],
link: [db],
//
// 6) Load balancing + automated TLS (TLD + wildcard subdomain)
//
// SST will:
// • provision an ALB
// • create/validate an ACM cert for “example.com” + “.example.com”
// • manage DNS in Cloudflare via the adapter
//
loadBalancer: {
domain: {
name: "example.com",
aliases: [".example.com"],
dns: sst.cloudflare.dns({
// your Cloudflare zone ID here
zone: "YOUR_CLOUDFLARE_ZONE_ID",
}),
},
rules: [
{ listen: "80/http", redirect: "443/https" },
{ listen: "443/https", forward: "4000/http" },
],
},
});
service.attachPermissions([db]); // grants RDS access
//
// 7) Cloudflare R2 for file uploads
//
// Creates an R2 bucket and injects the binding into your service.
// You can then call Resource.MyR2.list(), .put(), etc. :contentReference[oaicite:3]{index=3}
const r2 = new sst.cloudflare.Bucket(stack, "R2Bucket");
service.addEnvironment("R2_BUCKET", r2.name);
//
// 8) Cloudflare Images & Stream
//
// These aren’t first-class SST resources yet—you’d typically
// provision them via Cloudflare’s Terraform/Pulumi provider or
// manage them in the dashboard and just inject API tokens here.
// e.g. add CLOUDFLARE_IMAGES_ACCOUNT_ID and CLOUDFLARE_STREAM_TOKEN
// as secrets on your service.
//
// 9) Restricting external DB + “admin.*” access to VPC via Tailnet
//
// • RDS lives in private subnets—only the ECS tasks and the Tailscale
// node’s SG can reach it.
// • For your “admin.example.com” UI, you can spin up a second
// Service with loadBalancer.public: false:
//
// loadBalancer: { public: false, … }
//
// which creates an internal ALB. Don’t expose it to Cloudflare;
// instead, point at your VPC’s private hosted zone
// so that within your tailnet “admin.example.com” resolves to the
// internal ALB. Outside your tailnet it’s unreachable.
//
}
How this maps to your requirements
Elixir containers that can be clusteredUsesst.aws.Cluster+sst.aws.Service, running on Fargate with auto-restart and (optionally) auto-scalingSSTSST.
Postgres(-compatible) databasesst.aws. up an RDS Postgres in your VPC’s private subnetsSST.
Zero cold startsFargate Services are always-on—no Lambda-style cold starts.
VPCsst.aws.Vpcsets up a multi-AZ VPC with public/private subnets, NAT, etc.SST.
Load balancingloadBalancerinServiceprovisioned automatically, with health checks and listener rules.
Automated TLS certificatesSpecifyingloadBalancer.domainwithdns: sst.cloudflare.dns()wires up ACM cert issuance & DNS validation for both your root domain and wildcard subdomainsSST.
Tailscale exit node + app connectorAn EC2 instance installs Tailscale (via user-data) and advertises itself as an exit node. You’ll need to supply your auth key viasst.Secret.
Restrict external DB and admin.subdomain*RDS in private subnets: only your ECS tasks and the Tailscale SG can talk to it.For your admin UI, use a secondServicewithloadBalancer.public: false(internal ALB).Use Tailscale MagicDNS or a private zone (forwarded into tailnet) so thatadmin.example.comresolvesonlyinside your tailnet.
RDS in private subnets: only your ECS tasks and the Tailscale SG can talk to it.
For your admin UI, use a secondServicewithloadBalancer.public: false(internal ALB).
Use Tailscale MagicDNS or a private zone (forwarded into tailnet) so thatadmin.example.comresolvesonlyinside your tailnet.
Cloudflare for DNS / R2 / Images / StreamDNS:sst.cloudflare.dns()adapter automates record creation in CloudflareSST.R2:sst.cloudflare.Bucketfor your file uploadsSST.Images & Stream: manage via Cloudflare’s API or Terraform, inject tokens into your container via SST Secrets.
DNS:sst.cloudflare.dns()adapter automates record creation in CloudflareSST.
R2:sst.cloudflare.Bucketfor your file uploadsSST.
Images & Stream: manage via Cloudflare’s API or Terraform, inject tokens into your container via SST Secrets.
Elixir containers that can be clusteredUsesst.aws.Cluster+sst.aws.Service, running on Fargate with auto-restart and (optionally) auto-scalingSSTSST.
Postgres(-compatible) databasesst.aws. up an RDS Postgres in your VPC’s private subnetsSST.
Zero cold startsFargate Services are always-on—no Lambda-style cold starts.
VPCsst.aws.Vpcsets up a multi-AZ VPC with public/private subnets, NAT, etc.SST.
Load balancingloadBalancerinServiceprovisioned automatically, with health checks and listener rules.
Automated TLS certificatesSpecifyingloadBalancer.domainwithdns: sst.cloudflare.dns()wires up ACM cert issuance & DNS validation for both your root domain and wildcard subdomainsSST.
Tailscale exit node + app connectorAn EC2 instance installs Tailscale (via user-data) and advertises itself as an exit node. You’ll need to supply your auth key viasst.Secret.
Restrict external DB and admin.subdomain*
RDS in private subnets: only your ECS tasks and the Tailscale SG can talk to it.
For your admin UI, use a secondServicewithloadBalancer.public: false(internal ALB).
Use Tailscale MagicDNS or a private zone (forwarded into tailnet) so thatadmin.example.comresolvesonlyinside your tailnet.
RDS in private subnets: only your ECS tasks and the Tailscale SG can talk to it.
For your admin UI, use a secondServicewithloadBalancer.public: false(internal ALB).
Use Tailscale MagicDNS or a private zone (forwarded into tailnet) so thatadmin.example.comresolvesonlyinside your tailnet.
Cloudflare for DNS / R2 / Images / Stream
DNS:sst.cloudflare.dns()adapter automates record creation in CloudflareSST.
R2:sst.cloudflare.Bucketfor your file uploadsSST.
Images & Stream: manage via Cloudflare’s API or Terraform, inject tokens into your container via SST Secrets.
DNS:sst.cloudflare.dns()adapter automates record creation in CloudflareSST.
R2:sst.cloudflare.Bucketfor your file uploadsSST.
Images & Stream: manage via Cloudflare’s API or Terraform, inject tokens into your container via SST Secrets.
With this pattern, all of your infra is defined in onesst.config.ts, you get full IaC control, automatic certs, VPC isolation, and the ability to cluster and scale your Elixir workloads seamlessly on ECS.