GuidesFlightcontrolConfig with CodeSimplifying Large Config

Simplifying Large Flightcontrol Configs with CUE

CUE is a powerful configuration language that provides type safety, validation, and the ability to share and reuse configuration patterns. This guide will show you how to use CUE to configure your Flightcontrol project.

Getting Started

To use CUE with Flightcontrol:

  1. Create a flightcontrol.cue file instead of flightcontrol.json. The configuration structure remains the same, but you get all the benefits of CUE’s features.
  2. In the Flightcontrol dashboard, under project settings, update the config file path to the new flightcontrol.cue file.
CUE is a superset of JSON, so your existing JSON file is already valid CUE.

For more information about CUE, check out the official CUE documentation.

Sharing Common Configurations

One of the main benefits of using CUE is the ability to share and reuse configurations. Here’s how you can define common configurations and use them across your services:

#Flightcontrol: {
  @jsonschema(schema="https://app.flightcontrol.dev/schema.json")
}
 
webServerCommon: {
  "name": "Web server",
  "type": "web"
  "target": {
    "type": "fargate"
  },
  "buildType": "docker",
  "ci": {
    "type": "ec2",
  },
  "dockerfilePath": "Dockerfile",
  "dockerContext": ".",
  "port": 3000,
  "healthCheckPath": "/",
  "injectEnvVariablesInDockerfile": true,
  "includeEnvVariablesInBuild": true,
  "cloudfrontAutoCacheInvalidation": true,
  "healthCheckTimeoutSecs": 4,
  "healthCheckIntervalSecs": 5,
}
 
#Flightcontrol: {
  "environments": [
    {
      "id": "production",
      "name": "Production",
      "region": "us-west-2",
      "source": {
        "branch": "main",
        "pr": false,
        "trigger": "push"
      },
      "services": [
        webServerCommon & {
          "id": "web-server-prod",
          "cpu": 2,
          "memory": 4,
          "minInstances": 2,
          "maxInstances": 10,
          "autoscaling": {
            "cpuThreshold": 70,
            "memoryThreshold": 70,
            "cooldownTimerSecs": 300,
            "requestsPerTarget": 500
          },
        }
      ]
    },
    {
      "id": "staging",
      "name": "Staging",
      "region": "us-east-2",
      "source": {
        "branch": "main",
        "pr": false,
        "trigger": "push"
      },
      "services": [
        webServerCommon & {
          "id": "web-server-staging",
          "cpu": 1,
          "memory": 2,
          "minInstances": 1,
          "maxInstances": 2,
        }
      ]
    }
  ]
}

Simplified CUE Syntax

You can also strip out the JSON verbosity like this:

#Flightcontrol: {
  @jsonschema(schema="https://app.flightcontrol.dev/schema.json")
}
 
webServerCommon: {
  name:      "Web server"
  type:      "web"
  target:    {type: "fargate"}
  buildType: "docker"
  ci:        {type: "ec2"}
  dockerfilePath:                  "Dockerfile"
  dockerContext:                   "."
  port:                            3000
  healthCheckPath:                 "/"
  injectEnvVariablesInDockerfile:  true
  includeEnvVariablesInBuild:      true
  cloudfrontAutoCacheInvalidation: true
  healthCheckTimeoutSecs:          4
  healthCheckIntervalSecs:         5
}
 
#Flightcontrol: environments: [
  {
    id:     "production"
    name:   "Production"
    region: "us-west-2"
    source: {
      branch:  "main"
      pr:      false
      trigger: "push"
    }
    services: [
      webServerCommon & {
        id:           "web-server-prod"
        cpu:          2
        memory:       4
        minInstances: 2
        maxInstances: 10
        autoscaling: {
          cpuThreshold:      70
          memoryThreshold:   70
          cooldownTimerSecs: 300
          requestsPerTarget: 500
        }
      },
    ]
  },
  {
    id:     "staging"
    name:   "Staging"
    region: "us-east-2"
    source: {
      branch:  "main"
      pr:      false
      trigger: "push"
    }
    services: [
      webServerCommon & {
        id:           "web-server-staging"
        cpu:          1
        memory:       2
        minInstances: 1
        maxInstances: 2
      },
    ]
  },
]