skip to Main Content

Could you advise me what is the best way to creation a new terraform project from scratch?

My idea was to create a project structure as below. I am not sure how to handle a terraform state file. I know that I should create e.g. storage account in Azure and then define a backend for each environment. But how to do that? Should I create it manually or create a completely separate module that will handle the creation of a storage account? Or is there any option to create it from the main project, then automatically create a backend.tf file?

├── environment
│   ├── dev
│   │   ├── main.tf
│   │   ├── variables.tf
│   │   └── outputs.tf
│   │   └── backend.tf
│   ├── test
│   │   ├── main.tf
│   │   ├── variables.tf
│   │   └── outputs.tf
│   │   └── backend.tf
│   └── prod
│       ├── main.tf
│       ├── variables.tf
│       └── outputs.tf
│   │   └── backend.tf
├── modules
│   └── aks
│       ├── main.tf
│       ├── variables.tf
│       └── outputs.tf
│   └── terraform_state_storage
│       ├── main.tf
│       ├── variables.tf
│       └── outputs.tf

2

Answers


  1. Following your request, you should consider Workspaces.

    Awesome guide here by Yevgeniy Brikman

    Login or Signup to reply.
  2. First, please use Terraform Workspaces to manage the deployment of duplicate infrastructure in different dev/test/prod environments.


    As for the creation of backend storage, you either need to split that out into a separate Terraform template, or simply create that outside of Terraform. The problems with creating the backend storage in Terraform are:

    • The backend has to exist before you run terraform init to initialize your Terraform project.
    • If you created the backend storage in another Terraform project, then THAT Terraform project will also need backend storage, and where will that be created?

    Given the above, I always create a backend AWS S3 Bucket, or Azure Blob Storage, manually. This is usually the only infrastructure I create in the AWS or Azure account outside of Terraform.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search