AWSTemplateFormatVersion: "2010-09-09" Description: >- Gantry on ECS Fargate behind an Application Load Balancer, with RDS for PostgreSQL as its store and the provider key in Secrets Manager. A first try or a small team: one task, one database, no NAT gateway. The admin key is printed once in the log group named in the outputs. Production: https://gantry-docs.enterplace.dev/deploy/aws/ Metadata: AWS::CloudFormation::Interface: ParameterGroups: - Label: { default: Provider } Parameters: [ProviderKeyName, ProviderKey] - Label: { default: Optional } Parameters: [CertificateArn, Image, DbInstanceClass] ParameterLabels: ProviderKeyName: { default: Which provider } ProviderKey: { default: Provider API key } CertificateArn: { default: ACM certificate ARN for HTTPS (optional) } Parameters: ProviderKeyName: Type: String Default: OPENAI_API_KEY AllowedValues: [OPENAI_API_KEY, ANTHROPIC_API_KEY, GEMINI_API_KEY] Description: The provider whose key you give below. Add others later in the console. ProviderKey: Type: String NoEcho: true MinLength: 1 Description: Kept in Secrets Manager and handed to the task as the variable above. CertificateArn: Type: String Default: "" Description: An ACM certificate in this region for your own domain. Empty serves plain HTTP on the load balancer's name, for a first try only. Image: Type: String Default: ghcr.io/eliransu/gantry:latest Description: The Gantry image. Pin a version for anything that lasts. DbInstanceClass: Type: String Default: db.t4g.micro AllowedValues: [db.t4g.micro, db.t4g.small, db.t4g.medium] Conditions: Https: !Not [!Equals [!Ref CertificateArn, ""]] Resources: Vpc: Type: AWS::EC2::VPC Properties: CidrBlock: 10.80.0.0/16 EnableDnsSupport: true EnableDnsHostnames: true Tags: [{ Key: Name, Value: !Ref AWS::StackName }] InternetGateway: Type: AWS::EC2::InternetGateway GatewayAttachment: Type: AWS::EC2::VPCGatewayAttachment Properties: { VpcId: !Ref Vpc, InternetGatewayId: !Ref InternetGateway } PublicRouteTable: Type: AWS::EC2::RouteTable Properties: { VpcId: !Ref Vpc } PublicRoute: Type: AWS::EC2::Route DependsOn: GatewayAttachment Properties: RouteTableId: !Ref PublicRouteTable DestinationCidrBlock: 0.0.0.0/0 GatewayId: !Ref InternetGateway # The task runs in public subnets with a public IP so it reaches the registry and the providers without a NAT # gateway; its security group admits only the load balancer. PublicSubnetA: Type: AWS::EC2::Subnet Properties: VpcId: !Ref Vpc CidrBlock: 10.80.0.0/24 AvailabilityZone: !Select [0, !GetAZs ""] MapPublicIpOnLaunch: true PublicSubnetB: Type: AWS::EC2::Subnet Properties: VpcId: !Ref Vpc CidrBlock: 10.80.1.0/24 AvailabilityZone: !Select [1, !GetAZs ""] MapPublicIpOnLaunch: true PublicSubnetARoute: Type: AWS::EC2::SubnetRouteTableAssociation Properties: { SubnetId: !Ref PublicSubnetA, RouteTableId: !Ref PublicRouteTable } PublicSubnetBRoute: Type: AWS::EC2::SubnetRouteTableAssociation Properties: { SubnetId: !Ref PublicSubnetB, RouteTableId: !Ref PublicRouteTable } # The database's subnets have no route out. PrivateSubnetA: Type: AWS::EC2::Subnet Properties: VpcId: !Ref Vpc CidrBlock: 10.80.10.0/24 AvailabilityZone: !Select [0, !GetAZs ""] PrivateSubnetB: Type: AWS::EC2::Subnet Properties: VpcId: !Ref Vpc CidrBlock: 10.80.11.0/24 AvailabilityZone: !Select [1, !GetAZs ""] LoadBalancerSecurityGroup: Type: AWS::EC2::SecurityGroup Properties: GroupDescription: Gantry load balancer VpcId: !Ref Vpc SecurityGroupIngress: - { IpProtocol: tcp, FromPort: 80, ToPort: 80, CidrIp: 0.0.0.0/0 } - { IpProtocol: tcp, FromPort: 443, ToPort: 443, CidrIp: 0.0.0.0/0 } TaskSecurityGroup: Type: AWS::EC2::SecurityGroup Properties: GroupDescription: Gantry task, reached only from the load balancer VpcId: !Ref Vpc SecurityGroupIngress: - { IpProtocol: tcp, FromPort: 8080, ToPort: 8080, SourceSecurityGroupId: !Ref LoadBalancerSecurityGroup } DatabaseSecurityGroup: Type: AWS::EC2::SecurityGroup Properties: GroupDescription: Gantry store, reached only from the task VpcId: !Ref Vpc SecurityGroupIngress: - { IpProtocol: tcp, FromPort: 5432, ToPort: 5432, SourceSecurityGroupId: !Ref TaskSecurityGroup } DatabasePassword: Type: AWS::SecretsManager::Secret Properties: Description: Gantry store password GenerateSecretString: SecretStringTemplate: '{"username": "gantry"}' GenerateStringKey: password PasswordLength: 32 ExcludePunctuation: true DatabaseSubnets: Type: AWS::RDS::DBSubnetGroup Properties: DBSubnetGroupDescription: Gantry store SubnetIds: [!Ref PrivateSubnetA, !Ref PrivateSubnetB] Database: Type: AWS::RDS::DBInstance DeletionPolicy: Snapshot UpdateReplacePolicy: Snapshot Properties: Engine: postgres # The major version CI tests; RDS retires old minors, so raise this when cfn-lint or RDS says it is gone. EngineVersion: "16.9" DBInstanceClass: !Ref DbInstanceClass AllocatedStorage: "20" StorageType: gp3 StorageEncrypted: true DBName: gantry MasterUsername: gantry MasterUserPassword: !Sub "{{resolve:secretsmanager:${DatabasePassword}:SecretString:password}}" DBSubnetGroupName: !Ref DatabaseSubnets VPCSecurityGroups: [!Ref DatabaseSecurityGroup] PubliclyAccessible: false BackupRetentionPeriod: 7 # RDS's certificate is signed by an Amazon CA that is not among the roots built into Gantry, so the connection is # encrypted (sslmode=require) inside the VPC without checking it. To check it, see the AWS guide's RDS bundle. DatabaseUrl: Type: AWS::SecretsManager::Secret Properties: Description: Gantry DATABASE_URL SecretString: !Sub "postgres://gantry:{{resolve:secretsmanager:${DatabasePassword}:SecretString:password}}@${Database.Endpoint.Address}:${Database.Endpoint.Port}/gantry?sslmode=require" ProviderKeySecret: Type: AWS::SecretsManager::Secret Properties: Description: Gantry provider key SecretString: !Ref ProviderKey LogGroup: Type: AWS::Logs::LogGroup Properties: LogGroupName: !Sub "/gantry/${AWS::StackName}" RetentionInDays: 30 ExecutionRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: "2012-10-17" Statement: - { Effect: Allow, Principal: { Service: ecs-tasks.amazonaws.com }, Action: sts:AssumeRole } ManagedPolicyArns: - arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy Policies: - PolicyName: read-gantry-secrets PolicyDocument: Version: "2012-10-17" Statement: - Effect: Allow Action: secretsmanager:GetSecretValue Resource: [!Ref DatabaseUrl, !Ref ProviderKeySecret] Cluster: Type: AWS::ECS::Cluster TaskDefinition: Type: AWS::ECS::TaskDefinition Properties: Family: !Ref AWS::StackName RequiresCompatibilities: [FARGATE] NetworkMode: awsvpc Cpu: "256" Memory: "512" RuntimePlatform: { CpuArchitecture: X86_64, OperatingSystemFamily: LINUX } ExecutionRoleArn: !GetAtt ExecutionRole.Arn ContainerDefinitions: - Name: gantry Image: !Ref Image Essential: true PortMappings: [{ ContainerPort: 8080, Protocol: tcp }] Secrets: - { Name: DATABASE_URL, ValueFrom: !Ref DatabaseUrl } - { Name: !Ref ProviderKeyName, ValueFrom: !Ref ProviderKeySecret } LogConfiguration: LogDriver: awslogs Options: awslogs-group: !Ref LogGroup awslogs-region: !Ref AWS::Region awslogs-stream-prefix: gantry LoadBalancer: Type: AWS::ElasticLoadBalancingV2::LoadBalancer Properties: Type: application Scheme: internet-facing Subnets: [!Ref PublicSubnetA, !Ref PublicSubnetB] SecurityGroups: [!Ref LoadBalancerSecurityGroup] LoadBalancerAttributes: # Streamed answers can pause longer than the default 60 seconds. - { Key: idle_timeout.timeout_seconds, Value: "600" } TargetGroup: Type: AWS::ElasticLoadBalancingV2::TargetGroup Properties: VpcId: !Ref Vpc TargetType: ip Protocol: HTTP Port: 8080 HealthCheckPath: /healthz HealthCheckIntervalSeconds: 15 TargetGroupAttributes: - { Key: deregistration_delay.timeout_seconds, Value: "30" } HttpListener: Type: AWS::ElasticLoadBalancingV2::Listener Properties: LoadBalancerArn: !Ref LoadBalancer Port: 80 Protocol: HTTP DefaultActions: !If - Https - - Type: redirect RedirectConfig: { Protocol: HTTPS, Port: "443", StatusCode: HTTP_301 } - - { Type: forward, TargetGroupArn: !Ref TargetGroup } HttpsListener: Type: AWS::ElasticLoadBalancingV2::Listener Condition: Https Properties: LoadBalancerArn: !Ref LoadBalancer Port: 443 Protocol: HTTPS Certificates: [{ CertificateArn: !Ref CertificateArn }] SslPolicy: ELBSecurityPolicy-TLS13-1-2-2021-06 DefaultActions: [{ Type: forward, TargetGroupArn: !Ref TargetGroup }] # One task: it keeps its audit chain on its own disk, and keys and the policy in the store. Service: Type: AWS::ECS::Service DependsOn: HttpListener Properties: Cluster: !Ref Cluster LaunchType: FARGATE DesiredCount: 1 TaskDefinition: !Ref TaskDefinition HealthCheckGracePeriodSeconds: 60 DeploymentConfiguration: { MinimumHealthyPercent: 0, MaximumPercent: 100 } NetworkConfiguration: AwsvpcConfiguration: AssignPublicIp: ENABLED Subnets: [!Ref PublicSubnetA, !Ref PublicSubnetB] SecurityGroups: [!Ref TaskSecurityGroup] LoadBalancers: - { ContainerName: gantry, ContainerPort: 8080, TargetGroupArn: !Ref TargetGroup } Outputs: ConsoleUrl: Description: Sign in with the admin key. Value: !If - Https - !Sub "https:///console" - !Sub "http://${LoadBalancer.DNSName}/console" GatewayUrl: Description: The base URL for clients (add /v1 for OpenAI SDKs). Value: !If [Https, "https://", !Sub "http://${LoadBalancer.DNSName}"] LoadBalancerDnsName: Value: !GetAtt LoadBalancer.DNSName AdminKeyLogs: Description: The admin key is printed once here, in the first task's log stream (search for GANTRY_KEY). Value: !Sub "https://${AWS::Region}.console.aws.amazon.com/cloudwatch/home?region=${AWS::Region}#logsV2:log-groups/log-group/${LogGroup}"