{"id":13575422,"url":"https://github.com/terramate-io/terramate-example-code-generation","last_synced_at":"2025-04-04T22:31:04.305Z","repository":{"id":37799528,"uuid":"476699610","full_name":"terramate-io/terramate-example-code-generation","owner":"terramate-io","description":"An code generation example for Terramate.","archived":false,"fork":false,"pushed_at":"2024-11-01T15:08:29.000Z","size":172,"stargazers_count":67,"open_issues_count":0,"forks_count":13,"subscribers_count":11,"default_branch":"main","last_synced_at":"2024-11-01T15:17:33.830Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://terramate.io","language":"HCL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/terramate-io.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2022-04-01T11:50:45.000Z","updated_at":"2024-11-01T15:08:31.000Z","dependencies_parsed_at":"2024-06-18T08:52:18.731Z","dependency_job_id":"75064cab-f893-47b0-b018-bef0449f7b27","html_url":"https://github.com/terramate-io/terramate-example-code-generation","commit_stats":null,"previous_names":["terramate-io/terramate-example-code-generation"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/terramate-io%2Fterramate-example-code-generation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/terramate-io%2Fterramate-example-code-generation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/terramate-io%2Fterramate-example-code-generation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/terramate-io%2Fterramate-example-code-generation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/terramate-io","download_url":"https://codeload.github.com/terramate-io/terramate-example-code-generation/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247260447,"owners_count":20910006,"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":"2024-08-01T15:01:00.877Z","updated_at":"2025-04-04T22:31:00.208Z","avatar_url":"https://github.com/terramate-io.png","language":"HCL","readme":"# terramate-example-code-generation\n\n![CI Status](https://github.com/terramate-io/terramate-example-code-generation/actions/workflows/ci.yml/badge.svg)\n[![Join Discord](https://img.shields.io/discord/1088753599951151154?label=Discord\u0026logo=discord\u0026logoColor=white)](https://terramate.io/discord)\n\nThis project shows an example file/dir structure you can use with\n[Terramate](https://github.com/terramate-io/terramate) to keep your Terraform\ncode DRY.\n\nBe sure to read through the [Terramate documentation](https://github.com/terramate-io/terramate)\nto understand the features of Terramate used here.\n\nThe example is organized as two environments, each environment will have:\n\n- Its own [Google Cloud Project](https://cloud.google.com/storage/docs/projects).\n- Service account to be used when deploying Cloud Run services.\n- Two [Cloud Run](https://cloud.google.com/run) applications.\n\nThe [Cloud Run](https://cloud.google.com/run) applications are simple\necho servers that will be reachable through public URLs provided by\n[Cloud Run](https://cloud.google.com/run).\n\nNote: This code is solely for demonstration purposes.\nThis is not production-ready code, so use it at your own risk.\n\n# How to use this project?\n\n## Pre-Requisites\n\n- [Terraform](https://www.terraform.io/) `~\u003e 1.8`\n- [Terramate](https://github.com/terramate-io/terramate) `~\u003e 0.11.1`\n- Configure your Google Cloud credentials using one of the supported [authentication mechanisms](https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/provider_reference#authentication)\n- Google Cloud Provider account\n- At least one [Google Cloud project](https://cloud.google.com/storage/docs/projects)\n- The Google Cloud project has a proper billing account configured\n\n# How is the code organized?\n\nThis is the overall structure of the project:\n\n```\n├── modules\n│   ├── cloud-run\n│   └── service-account\n└── stacks\n    ├── prod\n    │   ├── cloud-runs\n    │   │   ├── app1\n    │   │   ├── app2\n    │   └── service-accounts\n    │       └── cloud-run\n    └── staging\n        ├── cloud-runs\n        │   ├── app1\n        │   ├── app2\n        └── service-accounts\n            └── cloud-run\n```\n\n- `modules/cloud-run`: Local module, useful to showcase change detection and DRY code generation.\n- `modules/service-account`: Local Terramate config that keeps code generation DRY between environments.\n- `stacks/prod`: All stacks belonging to the prod environment.\n- `stacks/staging`: All stacks belonging to the staging environment.\n- `stacks/\u003cenv\u003e/service-accounts/cloud-run`: Stack that creates service accounts used to execute the cloud run services.\n- `stacks/\u003cenv\u003e/cloud-runs/{app1,app2}`: Stacks that create Cloud Run services.\n\nAs you navigate the project you will find multiple Terramate configuration files.\nEach file will have documentation guiding you through its purpose and usage.\n\n## Listing Stacks\n\nTo check if your Terramate installation is working and get an overview of the\navailable stacks just run:\n\n```sh\nterramate list\n```\n\nTo check how each stack is defined in detail you can use `terramate run`:\n\n```sh\nterramate run -- cat stack.tm.hcl\n```\n\nThis will run on each stack directory the command `cat stack.tm.hcl`.\nThe output will be the definition of all stacks.\n\nLater we are going to use the same mechanism to create and destroy all stacks.\n\n## Deploying Stacks\n\nBefore we try to deploy any stacks, beware that this will require you\nto have [Google Cloud credentials](https://cloud.google.com/docs/authentication/getting-started)\nand deploying infrastructure will incur costs (check the\n[pre-requisites](#pre-requisites) section for more details).\n\nOn `stacks/config.tm.hcl` you will find the `terraform_google_provider_project`\nglobal which configures the project where infrastructure will be created.\n\nIt is important to change that to a [Google Cloud project](https://cloud.google.com/storage/docs/projects)\nwhere you have appropriate permissions.\n\nOnce the configuration is changed we need to update the generated code by running:\nAt this point, since our project has uncommitted changes Terramate will prevent us\nfrom running any commands. Create a branch (or use the flag `--disable-check-git-uncommitted`\nto disable the git checks):\n\n```sh\ngit checkout -b \u003cyour branch name\u003e\n```\n\nAnd commit all the changed files.\n\nNow we initialize all our stacks:\n\n```sh\nterramate run -- terraform init\n```\n\nCheck how their plans look like:\n\n```sh\nterramate run -- terraform plan\n```\n\nAnd apply them:\n\n```sh\nterramate run -- terraform apply\n```\n\nFor each Cloud Run service deployed, there will be an output with the URL to\nthe deployed service, like this:\n\n```sh\nurl = \"https://terramate-app1-\u003cenv\u003e-\u003chash\u003e-lz.a.run.app\"\n```\n\nYou can check the outputs with:\n\n```sh\nterramate run -- terraform output\n```\n\nOpen the URL on the browser to check the running service.\n\nTo avoid unnecessary charges to your account let's destroy all stacks:\n\n```sh\nterramate run --reverse -- terraform destroy\n```\n\nThe `--reverse` flag runs all stacks in reversed order, which is desirable\nwhen destroying resources.\n","funding_links":[],"categories":["HCL","Uncategorized"],"sub_categories":["Uncategorized"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fterramate-io%2Fterramate-example-code-generation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fterramate-io%2Fterramate-example-code-generation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fterramate-io%2Fterramate-example-code-generation/lists"}