|
| 1 | +--- |
| 2 | +page_title: "Creating a Standardised Space and Project" |
| 3 | +subcategory: "Examples" |
| 4 | +--- |
| 5 | + |
| 6 | +# Creating a Standardised Space and Project |
| 7 | + |
| 8 | +This example show how to create a standardised space and project that can be used to quickly setup applications used as |
| 9 | +microservices. |
| 10 | + |
| 11 | +## Create Space, Project with Deployment Process |
| 12 | +Consuming terraform module with standard configuration for Space and Project |
| 13 | + |
| 14 | +```terraform |
| 15 | +module "standard" { |
| 16 | + source = "../modules/standardised-space-and-project" |
| 17 | +
|
| 18 | + space = { |
| 19 | + name = "Testing" |
| 20 | + description = "Testing infrastructure" |
| 21 | + } |
| 22 | + |
| 23 | + project = { |
| 24 | + name = "Test Project" |
| 25 | + description = "Testing resources" |
| 26 | + } |
| 27 | +} |
| 28 | +
|
| 29 | +resource "octopusdeploy_process" "test" { |
| 30 | + space_id = module.standard.space |
| 31 | + project_id = module.standard.project |
| 32 | +} |
| 33 | +
|
| 34 | +# Manual Intervention Step (Production only) |
| 35 | +resource "octopusdeploy_process_step" "approve" { |
| 36 | + space_id = module.standard.space |
| 37 | + process_id = octopusdeploy_process.test.id |
| 38 | + name = "Approve" |
| 39 | + type = "Octopus.Manual" |
| 40 | + is_required = true |
| 41 | + environments = [ |
| 42 | + module.standard.environments.production |
| 43 | + ] |
| 44 | + execution_properties = { |
| 45 | + "Octopus.Action.RunOnServer" = "True" |
| 46 | + "Octopus.Action.Manual.Instructions" = "Please review and approve the deployment to production." |
| 47 | + "Octopus.Action.Manual.ResponsibleTeamIds" = octopusdeploy_team.approvers.id |
| 48 | + } |
| 49 | +} |
| 50 | +
|
| 51 | +# Run Azure Script Step |
| 52 | +resource "octopusdeploy_process_step" "azure" { |
| 53 | + space_id = module.standard.space |
| 54 | + process_id = octopusdeploy_process.test.id |
| 55 | + name = "Run Azure Script" |
| 56 | + type = "Octopus.AzurePowerShell" |
| 57 | + is_required = true |
| 58 | + environments = [ |
| 59 | + module.standard.environments.development, |
| 60 | + module.standard.environments.staging, |
| 61 | + module.standard.environments.production |
| 62 | + ] |
| 63 | + execution_properties = { |
| 64 | + "Octopus.Action.RunOnServer" = "True" |
| 65 | + "Octopus.Action.Azure.AccountId" = octopusdeploy_azure_service_principal.azure_account.id |
| 66 | + "Octopus.Action.Script.ScriptSource" = "Inline" |
| 67 | + "Octopus.Action.Script.Syntax" = "PowerShell" |
| 68 | + "Octopus.Action.Script.ScriptBody" = <<-EOT |
| 69 | + # Your Azure PowerShell script here |
| 70 | + Write-Output "Running Azure Script" |
| 71 | + EOT |
| 72 | + "OctopusUseBundledTooling" = "False" |
| 73 | + } |
| 74 | + container = { |
| 75 | + feed_id = module.standard.docker_registry |
| 76 | + image = "your-private-registry/your-execution-container:latest" |
| 77 | + } |
| 78 | +} |
| 79 | +
|
| 80 | +# Deploy Kubernetes YAML Step |
| 81 | +resource "octopusdeploy_process_step" "kubernetes" { |
| 82 | + space_id = module.standard.space |
| 83 | + process_id = octopusdeploy_process.test.id |
| 84 | + name = "Deploy Kubernetes YAML" |
| 85 | + properties = { |
| 86 | + "Octopus.Action.TargetRoles" = "k8s_cluster" |
| 87 | + } |
| 88 | + type = "Octopus.KubernetesDeployRawYaml" |
| 89 | + is_required = true |
| 90 | + environments = [ |
| 91 | + module.standard.environments.development, |
| 92 | + module.standard.environments.staging, |
| 93 | + module.standard.environments.production |
| 94 | + ] |
| 95 | + worker_pool_id = octopusdeploy_static_worker_pool.default.id |
| 96 | + execution_properties = { |
| 97 | + "Octopus.Action.RunOnServer" = "true" |
| 98 | + "Octopus.Action.KubernetesContainers.CustomResourceYamlFileName" = "deployment.yaml" |
| 99 | + "Octopus.Action.KubernetesContainers.Namespace" = "#{Octopus.Environment.Name | ToLower}" |
| 100 | + "Octopus.Action.Script.ScriptSource" = "GitRepository" |
| 101 | + "Octopus.Action.Git.Url" = "https://github.com/your-org/your-repo.git" |
| 102 | + "Octopus.Action.Git.Branch" = "main" |
| 103 | + "OctopusUseBundledTooling" = "False" |
| 104 | + } |
| 105 | + container = { |
| 106 | + feed_id = module.standard.docker_registry |
| 107 | + image = "octopusdeploy/worker-tools:latest" |
| 108 | + } |
| 109 | + git_dependencies = { |
| 110 | + "": { |
| 111 | + git_credential_id = octopusdeploy_git_credential.git_credential.id |
| 112 | + git_credential_type = "Library" |
| 113 | + repository_uri = "https://github.com/your-org/your-repo.git" |
| 114 | + default_branch = "main" |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | +
|
| 119 | +resource "octopusdeploy_process_steps_order" "test" { |
| 120 | + space_id = module.standard.space |
| 121 | + process_id = octopusdeploy_process.test.id |
| 122 | + steps = [ |
| 123 | + octopusdeploy_process_step.approve.id, |
| 124 | + octopusdeploy_process_step.azure.id, |
| 125 | + octopusdeploy_process_step.kubernetes.id, |
| 126 | + ] |
| 127 | +} |
| 128 | +
|
| 129 | +resource "octopusdeploy_team" "approvers" { |
| 130 | + space_id = module.standard.space |
| 131 | + name = "Deployment Approvers" |
| 132 | + description = "Team responsible for approving production deployments" |
| 133 | +} |
| 134 | +
|
| 135 | +resource "octopusdeploy_azure_service_principal" "azure_account" { |
| 136 | + space_id = module.standard.space |
| 137 | + name = "Example Azure Service Principal" |
| 138 | + description = "Azure service principal account for deployments" |
| 139 | + subscription_id = "00000000-0000-0000-0000-000000000001" |
| 140 | + tenant_id = "00000000-0000-0000-0000-000000000002" |
| 141 | + application_id = "00000000-0000-0000-0000-000000000003" |
| 142 | + password = "TopSecretPassword01!" |
| 143 | + environments = [ |
| 144 | + module.standard.environments.development, |
| 145 | + module.standard.environments.staging, |
| 146 | + module.standard.environments.production, |
| 147 | + ] |
| 148 | +} |
| 149 | +
|
| 150 | +resource "octopusdeploy_static_worker_pool" "default" { |
| 151 | + space_id = module.standard.space |
| 152 | + name = "Worker Pool 1" |
| 153 | + is_default = true |
| 154 | +} |
| 155 | +
|
| 156 | +resource "octopusdeploy_git_credential" "git_credential" { |
| 157 | + space_id = module.standard.space |
| 158 | + name = "TerraformGitCred" |
| 159 | + username = "TFP" |
| 160 | + password = "password01!" |
| 161 | +} |
| 162 | +
|
| 163 | +resource "octopusdeploy_kubernetes_cluster_deployment_target" "k8s_cluster_1" { |
| 164 | + space_id = module.standard.space |
| 165 | + name = "Example Kubernetes Cluster" |
| 166 | + cluster_url = "https://your-cluster-url.com" |
| 167 | + skip_tls_verification = false |
| 168 | + default_worker_pool_id = octopusdeploy_static_worker_pool.default.id |
| 169 | + namespace = "default" |
| 170 | + environments = [ |
| 171 | + module.standard.environments.development, |
| 172 | + module.standard.environments.staging, |
| 173 | + module.standard.environments.production |
| 174 | + ] |
| 175 | + roles = ["k8s-cluster"] |
| 176 | + azure_service_principal_authentication { |
| 177 | + account_id = octopusdeploy_azure_service_principal.azure_account.id |
| 178 | + cluster_name = "Cluster" |
| 179 | + cluster_resource_group = "cluster-rg" |
| 180 | + } |
| 181 | +} |
| 182 | +``` |
| 183 | + |
| 184 | +## Space and Project configuration module |
| 185 | +Module with standard configuration to be reused for multiple applications |
| 186 | + |
| 187 | +### variables.tf |
| 188 | +```terraform |
| 189 | +variable "space" { |
| 190 | + description = "Space details" |
| 191 | + type = object({ |
| 192 | + name = string |
| 193 | + description = optional(string, "") |
| 194 | + }) |
| 195 | +} |
| 196 | +
|
| 197 | +variable "project" { |
| 198 | + description = "Application details" |
| 199 | + type = object({ |
| 200 | + name = string |
| 201 | + description = optional(string, "") |
| 202 | + }) |
| 203 | +} |
| 204 | +``` |
| 205 | + |
| 206 | +### main.tf |
| 207 | +```terraform |
| 208 | +data "octopusdeploy_teams" "everyone" { |
| 209 | + partial_name = "Everyone" |
| 210 | + skip = 0 |
| 211 | + take = 1 |
| 212 | +} |
| 213 | +
|
| 214 | +# Create a Space |
| 215 | +resource "octopusdeploy_space" "example" { |
| 216 | + name = var.space.name |
| 217 | + description = var.space.description |
| 218 | + space_managers_teams = [ data.octopusdeploy_teams.everyone.teams[0].id ] |
| 219 | +} |
| 220 | +
|
| 221 | +# Create 3 Environments |
| 222 | +resource "octopusdeploy_environment" "development" { |
| 223 | + name = "Development" |
| 224 | + description = "Development environment" |
| 225 | + space_id = octopusdeploy_space.example.id |
| 226 | +} |
| 227 | +
|
| 228 | +resource "octopusdeploy_environment" "staging" { |
| 229 | + name = "Staging" |
| 230 | + description = "Staging environment" |
| 231 | + space_id = octopusdeploy_space.example.id |
| 232 | +} |
| 233 | +
|
| 234 | +resource "octopusdeploy_environment" "production" { |
| 235 | + name = "Production" |
| 236 | + description = "Production environment" |
| 237 | + space_id = octopusdeploy_space.example.id |
| 238 | +} |
| 239 | +
|
| 240 | +# Create a Container Registry |
| 241 | +resource "octopusdeploy_docker_container_registry" "example" { |
| 242 | + name = "DockerHub" |
| 243 | + space_id = octopusdeploy_space.example.id |
| 244 | + feed_uri = "https://registry.hub.docker.com" |
| 245 | +} |
| 246 | +
|
| 247 | +# Additional required resources |
| 248 | +resource "octopusdeploy_lifecycle" "example" { |
| 249 | + name = "Example Lifecycle" |
| 250 | + space_id = octopusdeploy_space.example.id |
| 251 | +} |
| 252 | +
|
| 253 | +resource "octopusdeploy_project_group" "example" { |
| 254 | + name = "Example Project Group" |
| 255 | + space_id = octopusdeploy_space.example.id |
| 256 | +} |
| 257 | +
|
| 258 | +# Create a Project |
| 259 | +resource "octopusdeploy_project" "example" { |
| 260 | + name = var.project.name |
| 261 | + description = var.project.description |
| 262 | + space_id = octopusdeploy_space.example.id |
| 263 | + lifecycle_id = octopusdeploy_lifecycle.example.id |
| 264 | + project_group_id = octopusdeploy_project_group.example.id |
| 265 | +} |
| 266 | +``` |
| 267 | + |
| 268 | +### output.tf |
| 269 | +```terraform |
| 270 | +output "space" { |
| 271 | + value = octopusdeploy_space.example.id |
| 272 | +} |
| 273 | +
|
| 274 | +output "project" { |
| 275 | + value = octopusdeploy_project.example.id |
| 276 | +} |
| 277 | +
|
| 278 | +output "environments" { |
| 279 | + value = { |
| 280 | + development = octopusdeploy_environment.development.id |
| 281 | + staging = octopusdeploy_environment.staging.id |
| 282 | + production = octopusdeploy_environment.production.id |
| 283 | + } |
| 284 | +} |
| 285 | +
|
| 286 | +output "docker_registry" { |
| 287 | + value = octopusdeploy_docker_container_registry.example.id |
| 288 | +} |
| 289 | +``` |
0 commit comments