Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto generate backend key based on component name for azurerm #95

Merged
merged 2 commits into from Dec 22, 2021

Conversation

lukeorellana
Copy link
Contributor

what

When using the AzureRM backend type, automatically generate backend key based on component name if the backend key is not specified at the component level:

  • Key is created in {Global Backend Key value}/{component name}.terraform.tfstate format. Places all component states in a storage account folder with the name of the global backend key value.
  • If the key is specified at the component level, the backend key is not automatically generated.

Before:

terraform:
  backend_type: azurerm
  backend:
    azurerm:
      subscription_id: 88888-8888-8888-8888-8888888888
      resource_group_name: rg-terraform-state
      storage_account_name: staterraformstate
      container_name: dev-tfstate

components:
  terraform:
    "monitoring":
      backend:
          azurerm:
            key: dev.atmos/monitoring.terraform.tfstate
        
    "networking":
      backend:
          azurerm:
            key: dev.atmos/networking.terraform.tfstate

After:

terraform:
  backend_type: azurerm
  backend:
    azurerm:
      subscription_id: 88888-8888-8888-8888-8888888888
      resource_group_name: rg-terraform-state
      storage_account_name: staterraformstate
      container_name: dev-tfstate
      key: dev.atmos

components:
  terraform:
    "rg": {}

    "monitoring": {}

    "vault": {}

backend.tf:

{
  "terraform": {
    "backend": {
      "azurerm": {
        "subscription_id": "88888-8888-8888-8888-8888888888",
        "container_name": "dev-tfstate",
        "key": "dev.atmos/rg.terraform.tfstate",
        "resource_group_name": "rg-terraform-state",
        "storage_account_name": "staterraformstate"
      }
    }
  }
}

why

The (azure backend) does not currently have a prefix option for workspaces. When using Atmos with Azure, users are forced to specify each backend key for every component.

references

hashicorp/terraform#28985

@aknysh
Copy link
Member

aknysh commented Dec 21, 2021

@lukeorellana thanks for the PR, and thanks for trying out atmos.
The PR looks, good, would yuo please add an example of the backend here https://github.com/cloudposse/atmos/blob/master/examples/complete/stacks/globals/globals.yaml#L7

backend_type: s3 # s3, remote, vault, static, azurerm, etc.
azurerm:
      subscription_id: 88888-8888-8888-8888-8888888888
      resource_group_name: rg-terraform-state
      storage_account_name: staterraformstate
      container_name: dev-tfstate
      key: dev.atmos

so other people would see the example on how to use it.

thanks!

Copy link
Member

@aknysh aknysh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lukeorellana thanks you, please see comments

@lukeorellana
Copy link
Contributor Author

@aknysh Thanks! Pushed commit with backend example . Let me know if you would like me to add anything else.

@aknysh aknysh added the patch A minor, backward compatible change label Dec 22, 2021

}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

@@ -478,6 +479,28 @@ func ProcessConfig(
}
}

// Check if component `backend` section has `key` for `azurerm` backend type
// If it does not, use the component name instead and format it with the global backend key name to auto generate a unique tf state key
// The backend state file will be formated like so: {global key name}/{component name}.terraform.tfstate
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// The backend state file will be formated like so: {global key name}/{component name}.terraform.tfstate
// The backend state file will be formatted like so: {global key name}/{component name}.terraform.tfstate

@aknysh
Copy link
Member

aknysh commented Dec 22, 2021

thanks @lukeorellana

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
patch A minor, backward compatible change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants