{"id":24977943,"url":"https://github.com/mondata-dev/terraform-stages","last_synced_at":"2025-03-29T08:44:40.335Z","repository":{"id":141586364,"uuid":"241744778","full_name":"mondata-dev/terraform-stages","owner":"mondata-dev","description":"A wrapper around terraform enabling staged apply/destroy","archived":false,"fork":false,"pushed_at":"2020-05-27T09:24:22.000Z","size":22,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-03T23:44:05.791Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mondata-dev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-19T22:55:54.000Z","updated_at":"2022-07-14T12:29:42.000Z","dependencies_parsed_at":null,"dependency_job_id":"53020cc9-795d-4fa5-bd47-09bddf298a95","html_url":"https://github.com/mondata-dev/terraform-stages","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mondata-dev%2Fterraform-stages","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mondata-dev%2Fterraform-stages/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mondata-dev%2Fterraform-stages/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mondata-dev%2Fterraform-stages/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mondata-dev","download_url":"https://codeload.github.com/mondata-dev/terraform-stages/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246162115,"owners_count":20733355,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-03T23:42:26.058Z","updated_at":"2025-03-29T08:44:40.328Z","avatar_url":"https://github.com/mondata-dev.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# terraform-stages\n\nA simple wrapper around terraform that supports sequential applications of terraform.\nBasically, with terraform-stages one can specify multiple terraform projects to be applied sequentially.\n\n## But why?\n\nIn certain scenarios it is currently not possible to specify the whole infrastructure in one terraform deployment.\nThis is mostly because providers cannot depend on one another.\nSee this terraform issue for more information: https://github.com/hashicorp/terraform/issues/2430\n\nLet's look at a simple example:\nConsider a simple deployment with a dockerized postgres DBMS that can be set up using the docker provider.\nNow, one wants to configure a database within this server.\nNormally, this is perfectly possible using the terraform postgres provider.\nFor configuring the postgres provider in terraform, the database needs to be up and running already in the planning phase.\nThis is however not yet the case, as the server needs to be spun up first.\n\nFor these cases terraform-stages can provide a solution:\nBasically, you split up the configuration into two separate terraform deployments called stages.\nIn the first, you specify the setup for the postgres server, in the second stage you configure the server.\nWith terraform-stages it is possible to pass information from one stage to another using terraform outputs and variables.\nAdditionally, terraform-stages can be configured to wait for an URL to be available (e.g. the postgres url) before continuing with the next stage.\nFor a complete example see the `example` folder.\n\n## Basic usage\n\nIn your base directory, create a folder for each stage you want to define.\nIn our example, we have two stages: `config` and `infrastructure`.\nWithin the folder create the terraform deployment as usual.\n\nCreate a `terraform-stages.yaml` file in the base directory.\nA basic configuration might look like this:\n```yaml\ninfrastructure: {}\n\nconfig:\n  depends_on:\n  - stage: infrastructure\n```\n\nHere, the config stage needs to run *after* the infrastructure stage.\nTo apply this configuration, `cd` to your base directory (e.g. `example` in this repository) and call\n```bash\nterraform-stages.rb apply\n```\n\nTo destroy it call\n```bash\nterraform-stages.rb destroy\n```\n\n## Variables\n\nYou can pass in variables by creating `.tfvars` files in the folder structure.\nSee the section on build directories for more details.\n\nAdditionally, you can specify that output variables of a stage should be used as input variables of another stage.\nFor example, to pass the `postgres_port` from the server setup to the configuration stage define it as output in the first stage:\n```hcl\noutput \"postgres_port\" {\n  value = docker_container.db.ports[0].external\n}\n```\nThen, define it as input variable in the config stage:\n```hcl\nvariable \"postgres_port\" {\n  type = number\n}\n```\nFinally, let terraform-stages know about your plan by specifying it in the `terraform-stages.yaml`:\n\n```yaml\ninfrastructure: {}\n\nconfig:\n  depends_on:\n  - stage: infrastructure\n    variables:\n    - postgres_port\n```\n\nNote, that the example in this repository already implements this.\n\n## URL Dependencies\n\nterraform-stages has the ability to delay application of a stage until a url is reachable.\nIn our example, we want to wait for the postgres server to be up and running before configuring it.\nTo configure this, add a url dependency in the `terraform-stages.yaml`:\n\n```yaml\ninfrastructure: {}\n\nconfig:\n  depends_on:\n  - stage: infrastructure\n    variables:\n    - postgres_port\n  - url: http://localhost:9000\n    timeout: 30\n```\n\nThe optional timeout flag allows to set a timeout in seconds.\nDefault is 120 seconds.\n\nFinally, it is also possible to use the variables defined in the stage dependencies (here only `postgres_port`) in the url:\n\n```yaml\n  - url: http://localhost:${postgres_port}\n```\n\n## Build directories / variants\n\nterraform-stages has rudimentary support for build variants via a feature called build directories.\nBasically build directories are folders with a similar structure as your base directory.\nAny `.tfvars` files you put into this structure will be used when applying or destroying your deployment:\n`.tfvars` files in the base directory will apply to all stages, `.tfvars` files in the subfolders will apply only the the stage with that name.\nFor an example have a look at the `example/variants/dev` and `example/variants/live` build directories.\n\nTo use a build directory, specify it when calling terraform-stages:\n```bash\nterraform-stages.rb apply/destroy -build-dir=path/to/build/dir\n```\nFor example, to create the dev build of the example go to the `example` folder of this repository and call\n```bash\nterraform-stages.rb apply -build-dir=variants/dev\n```\n\nTo make the variants feature complete, the terraform state will also be stored within the build directories.\nCurrently, terraform-stages does not support remote state.\n\nThe default build directory is always the base directory.\nThis means if no build directory is specified, any `.tfvars` files in the base directory or the stage directories will be included and the state will be stored within each stage directory.\n\n## A note on the maturity of this project\n\nThis project is still in early alpha phase and many things might be subject to change in the future.\nIn the long term, we do hope that a terraform update might make this project obsolete - but as this issue https://github.com/hashicorp/terraform/issues/2430 is known since 2015 this might still take some time.\n\nUntil then, we already use terraform-stages successfully for our own deployments.\nIf you are interested, we would be glad to know your thoughts on the project.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmondata-dev%2Fterraform-stages","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmondata-dev%2Fterraform-stages","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmondata-dev%2Fterraform-stages/lists"}