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
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 28 additions & 5 deletions pkg/stack/stack_processor.go
Expand Up @@ -2,17 +2,18 @@ package stack

import (
"fmt"
"path"
"path/filepath"
"sort"
"strings"
"sync"

c "github.com/cloudposse/atmos/pkg/convert"
g "github.com/cloudposse/atmos/pkg/globals"
m "github.com/cloudposse/atmos/pkg/merge"
"github.com/cloudposse/atmos/pkg/utils"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
"path"
"path/filepath"
"sort"
"strings"
"sync"
)

var (
Expand Down Expand Up @@ -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

if finalComponentBackendType == "azurerm" {
if azurerm, ok2 := componentBackendSection["azurerm"].(map[interface{}]interface{}); !ok2 {
if _, ok2 := azurerm["key"].(string); !ok2 {
azureKeyPrefixComponent := component
baseKeyName := ""
if baseComponentName != "" {
azureKeyPrefixComponent = baseComponentName
}
if azurerm, ok2 := globalBackendSection["azurerm"].(map[interface{}]interface{}); ok2 {
baseKeyName = azurerm["key"].(string)
}
componentKeyName := strings.Replace(azureKeyPrefixComponent, "/", "-", -1)
finalComponentBackend["key"] = fmt.Sprintf("%s/%s.terraform.tfstate", baseKeyName, componentKeyName)

}
}

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

}

// Final remote state backend
finalComponentRemoteStateBackendType := finalComponentBackendType
if len(globalRemoteStateBackendType) > 0 {
Expand Down