|
| 1 | +# How to Provisioning Cloud Foundry using Terrform |
| 2 | + |
| 3 | +## Objective |
| 4 | + |
| 5 | +This tutorial demonstrates how to provision Cloud Foundry resources by |
| 6 | +integrating the STACKIT Terraform provider with the Cloud Foundry Terraform |
| 7 | +provider. The STACKIT Terraform provider will create a managed Cloud Foundry |
| 8 | +organization and set up a technical "org manager" user with |
| 9 | +`organization_manager` permissions. These credentials, along with the Cloud |
| 10 | +Foundry API URL (retrieved dynamically from a platform data resource), are |
| 11 | +passed to the Cloud Foundry Terraform provider to manage resources within the |
| 12 | +new organization. |
| 13 | + |
| 14 | +### Output |
| 15 | + |
| 16 | +This configuration creates a Cloud Foundry organization, mirroring the structure |
| 17 | +created via the portal. It sets up three distinct spaces: `dev`, `qa`, and |
| 18 | +`prod`. The configuration assigns, a specified user the `organization_manager` |
| 19 | +and `organization_user` roles at the organization level, and the |
| 20 | +`space_developer` role in each space. |
| 21 | + |
| 22 | +### Scope |
| 23 | + |
| 24 | +This tutorial covers the interaction between the STACKIT Terraform provider and |
| 25 | +the Cloud Foundry Terraform provider. It assumes you are familiar with: |
| 26 | + |
| 27 | +- Setting up a STACKIT project and configuring the STACKIT Terraform provider |
| 28 | + with a service account (see the general STACKIT documentation for details). |
| 29 | +- Basic Terraform concepts, such as variables and locals. |
| 30 | + |
| 31 | +This document does not cover foundational topics or every feature of the Cloud |
| 32 | +Foundry Terraform provider. |
| 33 | + |
| 34 | +### Example configuration |
| 35 | + |
| 36 | +The following Terraform configuration provisions a Cloud Foundry organization |
| 37 | +and related resources using the STACKIT Terraform provider and the Cloud Foundry |
| 38 | +Terraform provider: |
| 39 | + |
| 40 | +``` |
| 41 | +terraform { |
| 42 | + required_providers { |
| 43 | + stackit = { |
| 44 | + source = "stackitcloud/stackit" |
| 45 | + } |
| 46 | + cloudfoundry = { |
| 47 | + source = "cloudfoundry/cloudfoundry" |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | +
|
| 52 | +variable "project_id" { |
| 53 | + type = string |
| 54 | + description = "Id of the Project" |
| 55 | +} |
| 56 | +
|
| 57 | +variable "org_name" { |
| 58 | + type = string |
| 59 | + description = "Name of the Organization" |
| 60 | +} |
| 61 | +
|
| 62 | +variable "admin_email" { |
| 63 | + type = string |
| 64 | + description = "Users who are granted permissions" |
| 65 | +} |
| 66 | +
|
| 67 | +provider "stackit" { |
| 68 | + default_region = "eu01" |
| 69 | +} |
| 70 | +
|
| 71 | +resource "stackit_scf_organization" "scf_org" { |
| 72 | + name = var.org_name |
| 73 | + project_id = var.project_id |
| 74 | +} |
| 75 | +
|
| 76 | +data "stackit_scf_platform" "scf_platform" { |
| 77 | + project_id = var.project_id |
| 78 | + platform_id = stackit_scf_organization.scf_org.platform_id |
| 79 | +} |
| 80 | +
|
| 81 | +resource "stackit_scf_organization_manager" "scf_manager" { |
| 82 | + project_id = var.project_id |
| 83 | + org_id = stackit_scf_organization.scf_org.org_id |
| 84 | +} |
| 85 | +
|
| 86 | +provider "cloudfoundry" { |
| 87 | + api_url = data.stackit_scf_platform.scf_platform.api_url |
| 88 | + user = stackit_scf_organization_manager.scf_manager.username |
| 89 | + password = stackit_scf_organization_manager.scf_manager.password |
| 90 | +} |
| 91 | +
|
| 92 | +locals { |
| 93 | + spaces = ["dev", "qa", "prod"] |
| 94 | +} |
| 95 | +
|
| 96 | +resource "cloudfoundry_org_role" "org_user" { |
| 97 | + username = var.admin_email |
| 98 | + type = "organization_user" |
| 99 | + org = stackit_scf_organization.scf_org.org_id |
| 100 | +} |
| 101 | +
|
| 102 | +resource "cloudfoundry_org_role" "org_manager" { |
| 103 | + username = var.admin_email |
| 104 | + type = "organization_manager" |
| 105 | + org = stackit_scf_organization.scf_org.org_id |
| 106 | +} |
| 107 | +
|
| 108 | +resource "cloudfoundry_space" "spaces" { |
| 109 | + for_each = toset(local.spaces) |
| 110 | + name = each.key |
| 111 | + org = stackit_scf_organization.scf_org.org_id |
| 112 | +} |
| 113 | +
|
| 114 | +resource "cloudfoundry_space_role" "space_developer" { |
| 115 | + for_each = toset(local.spaces) |
| 116 | + username = var.admin_email |
| 117 | + type = "space_developer" |
| 118 | + depends_on = [ cloudfoundry_org_role.org_user ] |
| 119 | + space = cloudfoundry_space.spaces[each.key].id |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +## Explanation of configuration |
| 124 | + |
| 125 | +### STACKIT provider configuration |
| 126 | + |
| 127 | +``` |
| 128 | +provider "stackit" { |
| 129 | + default_region = "eu01" |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +The STACKIT Cloud Foundry Application Programming Interface (SCF API) is |
| 134 | +regionalized. Each region operates independently. Set `default_region` in the |
| 135 | +provider configuration, to specify the region for all resources, unless you |
| 136 | +override it for individual resources. You must also provide access data for the |
| 137 | +relevant STACKIT project for the provider to function. |
| 138 | + |
| 139 | +For more details, see |
| 140 | +the:[STACKIT Terraform Provider documentation.](https://registry.terraform.io/providers/stackitcloud/stackit/latest/docs) |
| 141 | + |
| 142 | +### stackit_scf_organization.scf_org resource |
| 143 | + |
| 144 | +``` |
| 145 | +resource "stackit_scf_organization" "scf_org" { |
| 146 | + name = var.org_name |
| 147 | + project_id = var.project_id |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +This resource provisions a Cloud Foundry organization, which acts as the |
| 152 | +foundational container in the Cloud Foundry environment. Each Cloud Foundry |
| 153 | +provider configuration is scoped to a specific organization. The organization’s |
| 154 | +name, defined by a variable, must be unique across the platform. The |
| 155 | +organization is created within a designated STACKIT project, which requires the |
| 156 | +STACKIT provider to be configured with the necessary permissions for that |
| 157 | +project. |
| 158 | + |
| 159 | +### stackit_scf_organization_manager.scf_manager resource |
| 160 | + |
| 161 | +``` |
| 162 | +resource "stackit_scf_organization_manager" "scf_manager" { |
| 163 | + project_id = var.project_id |
| 164 | + org_id = stackit_scf_organization.scf_org.org_id |
| 165 | +} |
| 166 | +``` |
| 167 | + |
| 168 | +This resource creates a technical user in the Cloud Foundry organization with |
| 169 | +the organization_manager permission. The user is linked to the organization and |
| 170 | +is automatically deleted when the organization is removed. |
| 171 | + |
| 172 | +### stackit_scf_platform.scf_platform data source |
| 173 | + |
| 174 | +``` |
| 175 | +data "stackit_scf_platform" "scf_platform" { |
| 176 | + project_id = var.project_id |
| 177 | + platform_id = stackit_scf_organization.scf_org.platform_id |
| 178 | +} |
| 179 | +``` |
| 180 | + |
| 181 | +This data source retrieves properties of the Cloud Foundry platform where the |
| 182 | +organization is provisioned. It does not create resources, but provides |
| 183 | +information about the existing platform. |
| 184 | + |
| 185 | +### Cloud Foundry provider configuration |
| 186 | + |
| 187 | +``` |
| 188 | +provider "cloudfoundry" { |
| 189 | + api_url = data.stackit_scf_platform.scf_platform.api_url |
| 190 | + user = stackit_scf_organization_manager.scf_manager.username |
| 191 | + password = stackit_scf_organization_manager.scf_manager.password |
| 192 | +} |
| 193 | +``` |
| 194 | + |
| 195 | +The Cloud Foundry provider is configured to manage resources in the new |
| 196 | +organization. The provider uses the API URL from the `stackit_scf_platform` data |
| 197 | +source and authenticates using the credentials of the technical user created by |
| 198 | +the `stackit_scf_organization_manager` resource. |
| 199 | + |
| 200 | +For more information, see the: |
| 201 | +[Cloud Foundry Terraform Provider documentation.](https://registry.terraform.io/providers/cloudfoundry/cloudfoundry/latest/docs) |
| 202 | + |
| 203 | +## Deploy resources |
| 204 | + |
| 205 | +Follow these steps to initialize your environment and provision Cloud Foundry |
| 206 | +resources using Terraform. |
| 207 | + |
| 208 | +### Initialize Terraform |
| 209 | + |
| 210 | +Run the following command to initialize the working directory and download the |
| 211 | +required provider plugins: |
| 212 | + |
| 213 | +``` |
| 214 | +terraform init |
| 215 | +``` |
| 216 | + |
| 217 | +### Create the organization manager user |
| 218 | + |
| 219 | +Run this command to provision the organization and technical user needed to |
| 220 | +initialize the Cloud Foundry Terraform provider. This step is required only |
| 221 | +during the initial setup. For later changes, you do not need the -target flag. |
| 222 | + |
| 223 | +``` |
| 224 | +terraform apply -target stackit_scf_organization_manager.scf_manager |
| 225 | +``` |
| 226 | + |
| 227 | +### Apply the full configuration |
| 228 | + |
| 229 | +Run this command to provision all resources defined in your Terraform |
| 230 | +configuration within the Cloud Foundry organization: |
| 231 | + |
| 232 | +``` |
| 233 | +terraform apply |
| 234 | +``` |
| 235 | + |
| 236 | +## Verify the deployment |
| 237 | + |
| 238 | +Verify that your Cloud Foundry resources are provisioned correctly. Use the |
| 239 | +following Cloud Foundry CLI commands to check applications, services, and |
| 240 | +routes: |
| 241 | + |
| 242 | +- `cf apps` |
| 243 | +- `cf services` |
| 244 | +- `cf routes` |
| 245 | + |
| 246 | +For more information, see the |
| 247 | +[Cloud Foundry documentation](https://docs.cloudfoundry.org/) and the |
| 248 | +[Cloud Foundry CLI Reference Guide](https://cli.cloudfoundry.org/). |
0 commit comments