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

Disable automatic support for Terraform Cloud remote state backend #56

Merged
merged 3 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ Available targets:

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.0 |
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.14.0 |
| <a name="requirement_external"></a> [external](#requirement\_external) | >= 2.0 |
| <a name="requirement_local"></a> [local](#requirement\_local) | >= 1.3 |
| <a name="requirement_utils"></a> [utils](#requirement\_utils) | >= 1.5.0 |
Expand Down
2 changes: 1 addition & 1 deletion docs/terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.0 |
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.14.0 |
| <a name="requirement_external"></a> [external](#requirement\_external) | >= 2.0 |
| <a name="requirement_local"></a> [local](#requirement\_local) | >= 1.3 |
| <a name="requirement_utils"></a> [utils](#requirement\_utils) | >= 1.5.0 |
Expand Down
2 changes: 1 addition & 1 deletion examples/backend/versions.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = ">= 0.13.0"
required_version = ">= 0.14.0"

required_providers {
local = {
Expand Down
2 changes: 1 addition & 1 deletion examples/complete/versions.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = ">= 0.13.0"
required_version = ">= 1.1.0"

required_providers {
local = {
Expand Down
2 changes: 1 addition & 1 deletion examples/remote-state/versions.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = ">= 0.13.0"
required_version = ">= 1.1.0"

required_providers {
local = {
Expand Down
2 changes: 1 addition & 1 deletion examples/spacelift/versions.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = ">= 0.13.0"
required_version = ">= 0.14.0"

required_providers {
local = {
Expand Down
2 changes: 1 addition & 1 deletion examples/stack/versions.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = ">= 0.13.0"
required_version = ">= 0.14.0"

required_providers {
local = {
Expand Down
2 changes: 1 addition & 1 deletion examples/stacks/versions.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = ">= 0.13.0"
required_version = ">= 0.14.0"

required_providers {
local = {
Expand Down
2 changes: 1 addition & 1 deletion modules/backend/versions.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = ">= 0.13.0"
required_version = ">= 0.14.0"

required_providers {
local = {
Expand Down
2 changes: 1 addition & 1 deletion modules/env/versions.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = ">= 0.13.0"
required_version = ">= 0.14.0"

required_providers {
local = {
Expand Down
1 change: 1 addition & 0 deletions modules/remote-state/dummy-remote-state.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version": 1}
2 changes: 1 addition & 1 deletion modules/remote-state/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ locals {
workspace = lookup(local.config, "workspace", "")
workspace_key_prefix = lookup(local.backend, "workspace_key_prefix", null)

remote_state_enabled = ! var.bypass
remote_state_enabled = !var.bypass

remote_states = {
s3 = data.terraform_remote_state.s3
Expand Down
4 changes: 3 additions & 1 deletion modules/remote-state/remote.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ locals {
}

data "terraform_remote_state" "remote" {
count = local.remote_state_enabled && local.backend_type == "remote" ? 1 : 0
# workaround for https://github.com/hashicorp/terraform/issues/32023
count = local.remote_state_enabled && (var.backend_type == "remote" ? true : var.backend_type != "auto" ? false : local.backend_type == "remote") ? 1 : 0


backend = "remote"

Expand Down
28 changes: 22 additions & 6 deletions modules/remote-state/s3.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,27 @@ locals {
}

data "terraform_remote_state" "s3" {
count = local.remote_state_enabled && local.backend_type == "s3" ? 1 : 0
# workaround for https://github.com/hashicorp/terraform/issues/32023
count = local.remote_state_enabled && (var.backend_type == "s3" ? true : var.backend_type != "auto" ? false : local.backend_type == "s3") ? 1 : 0

backend = "s3"
# Mitigation for https://github.com/hashicorp/terraform/issues/32023
#
# With this bug, `local.config` is unknown and everything that flows from it
# is unknown, and cannot be used in count or for_each. This includes
# `local.backend_type`. The workaround is to force the S3 terraform remote
# state data source to be created, and then use `local.backend_type` to
# determine if we really meant to reference the S3 remote state, because by
# the time we get there, `local.config` is known. Except now that it is
# known, it might not really be S3, so we have to supply a dummy value if it
# is not S3. The rest of our code will ignore the dummy value, because it
# will not be looking to this resource for the data it needs, it will be
# looking to the correct backend type.

workspace = local.s3_workspace
backend = local.backend_type == "s3" ? "s3" : "local"

config = {
workspace = local.backend_type == "s3" ? local.s3_workspace : null

config = local.backend_type == "s3" ? {
encrypt = local.backend.encrypt
bucket = local.backend.bucket
key = local.backend.key
Expand Down Expand Up @@ -56,12 +70,14 @@ data "terraform_remote_state" "s3" {
# component, we don't touch the `globals.yaml` file at all, and we don't update the component's `role_arn` and `profile` settings).

# Use the role to access the remote state if the component is not privileged and `role_arn` is specified
role_arn = ! coalesce(try(local.backend.privileged, null), var.privileged) && contains(keys(local.backend), "role_arn") ? local.backend.role_arn : null
role_arn = !coalesce(try(local.backend.privileged, null), var.privileged) && contains(keys(local.backend), "role_arn") ? local.backend.role_arn : null

# Use the profile to access the remote state if the component is not privileged and `profile` is specified
profile = ! coalesce(try(local.backend.privileged, null), var.privileged) && contains(keys(local.backend), "profile") ? local.backend.profile : null
profile = !coalesce(try(local.backend.privileged, null), var.privileged) && contains(keys(local.backend), "profile") ? local.backend.profile : null

workspace_key_prefix = local.workspace_key_prefix
} : {
path = "${path.module}/dummy-remote-state.json"
}

defaults = var.defaults
Expand Down
14 changes: 14 additions & 0 deletions modules/remote-state/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,17 @@ variable "atmos_base_path" {
description = "atmos base path to components and stacks"
default = null
}

variable "backend_type" {
type = string
# Due to Terraform [issue #32023](https://github.com/hashicorp/terraform/issues/32023),
# we cannot reliably get the backend type from the stack configuration, even when
# the stack has it. So we need to pass it in as a variable.
description = <<-EOF
Set to "auto" to get the backend type from the stack configuration.
Unfortunately, the "auto" setting causes Terraform [issue #32023](https://github.com/hashicorp/terraform/issues/32023).
However, please continue to configure the backend type in the stack configuration,
because when the Terraform issue is fixed, the default will be quietly changed to "auto".
EOF
default = "s3"
}
15 changes: 12 additions & 3 deletions modules/remote-state/versions.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = ">= 0.13.0"
required_version = ">= 1.1.0"

required_providers {
local = {
Expand All @@ -11,8 +11,17 @@ terraform {
version = ">= 2.0"
}
utils = {
source = "cloudposse/utils"
version = "1.5.0"
source = "cloudposse/utils"
# Do not allow automatic updates to this provider
# until we have tested the new version thoroughly.
# Move the <= version constraint to the latest version
# after testing is complete. Move the >= version constraint
# when a new version adds a required feature or fixes a bug.
# If a version in between is found to have a bug,
# add a != constraint for that version.
# Leave a redundant != constraint for the last known bad version
# as an example of how to add a constraint for a bad version.
version = ">= 1.5.0, != 1.4.0, <= 1.5.0"
}
}
}
2 changes: 1 addition & 1 deletion modules/settings/versions.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = ">= 0.13.0"
required_version = ">= 0.14.0"

required_providers {
local = {
Expand Down
2 changes: 1 addition & 1 deletion modules/spacelift/versions.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = ">= 0.13.0"
required_version = ">= 0.14.0"

required_providers {
local = {
Expand Down
2 changes: 1 addition & 1 deletion modules/stack/versions.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = ">= 0.13.0"
required_version = ">= 0.14.0"

required_providers {
local = {
Expand Down
2 changes: 1 addition & 1 deletion modules/vars/versions.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = ">= 0.13.0"
required_version = ">= 0.14.0"

required_providers {
local = {
Expand Down
2 changes: 1 addition & 1 deletion versions.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = ">= 0.13.0"
required_version = ">= 0.14.0"

required_providers {
local = {
Expand Down