|
| 1 | +--- |
| 2 | +title: Sharing Outputs with Continuous Deployment |
| 3 | +description: Export Outputs of a Terraform Stack to a Kubernetes CD Service |
| 4 | +--- |
| 5 | + |
| 6 | +It's frequently necessary to take infrastructure created in a stack and use the data in a Service being deployed to K8s. A few usecases where this can be important: |
| 7 | + |
| 8 | +* exporting IAM role ARNs for access to various AWS services like S3 or SQS |
| 9 | +* exporting DB connection strings to configure as k8s secrets for a webserver |
| 10 | +* exporting a dynamically created S3 bucket name to use in your deployed service |
| 11 | + |
| 12 | +This also facilitates end-to-end self-service, as you no longer need a human in-the-loop to apply that last mile configuration, plus you get continuous reconciliation in the event a terraform change implies a recreation of those resources. |
| 13 | + |
| 14 | +## End To End Example |
| 15 | + |
| 16 | +We do this a lot in our service catalog, available here: https://github.com/pluralsh/scaffolds/tree/main/catalogs. A basic example would be something like our Airbyte setup, where the terraform stack has an outputs file like so: |
| 17 | + |
| 18 | +```tf |
| 19 | +output "access_key_id" { |
| 20 | + value = aws_iam_access_key.airbyte.id |
| 21 | +} |
| 22 | +
|
| 23 | +output "secret_access_key" { |
| 24 | + value = aws_iam_access_key.airbyte.secret |
| 25 | + sensitive = true |
| 26 | +} |
| 27 | +
|
| 28 | +output "postgres_host" { |
| 29 | + value = try(module.db.db_instance_address, "") |
| 30 | +} |
| 31 | +
|
| 32 | +output "postgres_password" { |
| 33 | + value = random_password.password.result |
| 34 | + sensitive = true |
| 35 | +} |
| 36 | +
|
| 37 | +output "oidc_cookie_secret" { |
| 38 | + value = random_password.oidc_cookie.result |
| 39 | + sensitive = true |
| 40 | +} |
| 41 | +
|
| 42 | +output "oidc_client_id" { |
| 43 | + value = plural_oidc_provider.airbyte.client_id |
| 44 | + sensitive = true |
| 45 | +} |
| 46 | +
|
| 47 | +output "oidc_client_secret" { |
| 48 | + value = plural_oidc_provider.airbyte.client_secret |
| 49 | + sensitive = true |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +Airbyte needs fixed aws access keys to communicate with S3 and also there's a dynamically generated OIDC client that's used for auth against it's webserver, alongside postgres credentials. |
| 54 | + |
| 55 | +with a InfrastructureStack resource will like this: |
| 56 | + |
| 57 | +```yaml |
| 58 | +apiVersion: deployments.plural.sh/v1alpha1 |
| 59 | +kind: InfrastructureStack |
| 60 | +metadata: |
| 61 | + name: airbyte-data |
| 62 | + namespace: apps |
| 63 | +spec: |
| 64 | + detach: false |
| 65 | + type: TERRAFORM |
| 66 | + approval: true |
| 67 | + manageState: true |
| 68 | + git: |
| 69 | + ref: main |
| 70 | + folder: terraform/apps/airbyte/data |
| 71 | + repositoryRef: |
| 72 | + name: infra |
| 73 | + namespace: infra |
| 74 | + clusterRef: |
| 75 | + name: mgmt |
| 76 | + namespace: infra |
| 77 | +``` |
| 78 | +
|
| 79 | +All of this data needs to be used by the service that is actually deployed to K8s, so it will explicitly "import" that stack with its `imports` declaration, like so: |
| 80 | + |
| 81 | +```yaml |
| 82 | +apiVersion: deployments.plural.sh/v1alpha1 |
| 83 | +kind: ServiceDeployment |
| 84 | +metadata: |
| 85 | + name: airbyte-data |
| 86 | + namespace: apps |
| 87 | +spec: |
| 88 | + namespace: airbyte |
| 89 | + git: |
| 90 | + folder: helm/airbyte/data |
| 91 | + ref: main |
| 92 | + repositoryRef: |
| 93 | + kind: GitRepository |
| 94 | + name: infra |
| 95 | + namespace: infra |
| 96 | + helm: |
| 97 | + version: "1.x.x" |
| 98 | + chart: airbyte |
| 99 | + release: airbyte |
| 100 | + ignoreHooks: false |
| 101 | + url: https://airbytehq.github.io/helm-charts |
| 102 | + valuesFiles: |
| 103 | + - airbyte.yaml.liquid |
| 104 | + imports: |
| 105 | + - stackRef: |
| 106 | + name: airbyte-data # notice this is the same as the metadata.name and metadata.namespace of the `InfrastructureStack` CRD to resolve the ref |
| 107 | + namespace: apps |
| 108 | + configuration: |
| 109 | + cluster: data |
| 110 | + hostname: airbyte.example.com |
| 111 | + bucket: airbyte-bucket |
| 112 | + region: us-east-2 |
| 113 | + clusterRef: |
| 114 | + kind: Cluster |
| 115 | + name: data |
| 116 | + namespace: infra |
| 117 | +``` |
| 118 | +
|
| 119 | +When that is present, it will allow us to template the outputs under the key `imports["airbyte-data"].{output_field_name}` in any .liquid values or yaml file for the service, an example for the airbyte helm chart `airbyte.yaml.liquid` values file: |
| 120 | + |
| 121 | +```yaml |
| 122 | +global: |
| 123 | + deploymentMode: oss |
| 124 | + edition: community |
| 125 | +
|
| 126 | + airbyteUrl: {{ configuration.hostname }} |
| 127 | +
|
| 128 | + storage: |
| 129 | + type: S3 |
| 130 | + storageSecretName: airbyte-airbyte-secrets |
| 131 | + s3: |
| 132 | + region: {{ configuration.region }} |
| 133 | + authenticationType: credentials |
| 134 | + accessKeyId: {{ imports["airbyte-data"].access_key_id }} |
| 135 | + accessKeyIdSecretKey: AWS_ACCESS_KEY_ID |
| 136 | + secretAccessKey: {{ imports["airbyte-data"].secret_access_key }} |
| 137 | + secretAccessKeySecretKey: AWS_SECRET_ACCESS_KEY |
| 138 | + bucket: |
| 139 | + log: {{ configuration.bucket }} |
| 140 | + state: {{ configuration.bucket }} |
| 141 | + workloadOutput: {{ configuration.bucket }} |
| 142 | +
|
| 143 | + database: |
| 144 | + type: external |
| 145 | + database: airbyte |
| 146 | + host: {{ imports["airbyte-data"].postgres_host }} |
| 147 | + port: "5432" |
| 148 | + secretName: airbyte-airbyte-secrets |
| 149 | + user: airbyte |
| 150 | + userSecretKey: DATABASE_USER |
| 151 | + password: {{ imports["airbyte-data"].postgres_password }} |
| 152 | + passwordSecretKey: DATABASE_PASSWORD |
| 153 | +
|
| 154 | +postgresql: |
| 155 | + enabled: false |
| 156 | +
|
| 157 | +externalDatabase: |
| 158 | + database: airbyte |
| 159 | + host: {{ imports["airbyte-data"].postgres_host }} |
| 160 | + user: airbyte |
| 161 | + existingSecret: ~ |
| 162 | + password: {{ imports["airbyte-data"].postgres_password }} |
| 163 | + port: 5432 |
| 164 | +
|
| 165 | +webapp: |
| 166 | + ingress: |
| 167 | + enabled: false |
| 168 | + podAnnotations: |
| 169 | + security.plural.sh/oauth-env-secret: airbyte-proxy-config |
| 170 | + podLabels: |
| 171 | + security.plural.sh/inject-oauth-sidecar: "true" |
| 172 | +``` |
| 173 | + |
| 174 | +You an read more about templating [here](/plural-features/service-templating). In this case it's going to pass these variables through helm and configure the necessary secrets and yaml structures with the provided information. |
| 175 | + |
| 176 | +{% callout severity="info" %} |
| 177 | +The imports structure is a map from stack name to stack outputs, eg a nested map type like { string => { string => any } } |
| 178 | +{% /callout %} |
0 commit comments