切换到新工作空间时,Terraform尝试再次创建S3后端

时间:2019-12-11 02:59:51

标签: terraform

我正在遵循this关于地形的出色指南。我目前在探索该州的第三篇文章中。特别是在point,这里演示了terraform工作区。 因此,我有以下main.tf

provider "aws" {
  region = "us-east-2"
}

resource "aws_s3_bucket" "terraform_state" {
  bucket = "mark-kharitonov-terraform-up-and-running-state"

  # Enable versioning so we can see the full revision history of our
  # state files
  versioning {
    enabled = true
  }

  # Enable server-side encryption by default
  server_side_encryption_configuration {
    rule {
      apply_server_side_encryption_by_default {
        sse_algorithm = "AES256"
      }
    }
  }
}

resource "aws_dynamodb_table" "terraform_locks" {
  name         = "terraform-up-and-running-locks"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = "LockID"

  attribute {
    name = "LockID"
    type = "S"
  }
}

terraform {
  backend "s3" {
    # Replace this with your bucket name!
    bucket = "mark-kharitonov-terraform-up-and-running-state"
    key    = "workspaces-example/terraform.tfstate"
    region = "us-east-2"
    # Replace this with your DynamoDB table name!
    dynamodb_table = "terraform-up-and-running-locks"
    encrypt        = true
  }
}

output "s3_bucket_arn" {
  value       = aws_s3_bucket.terraform_state.arn
  description = "The ARN of the S3 bucket"
}

output "dynamodb_table_name" {
  value       = aws_dynamodb_table.terraform_locks.name
  description = "The name of the DynamoDB table"
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}

一切都很棒:

C:\work\terraform [master ≡]> terraform workspace show
default
C:\work\terraform [master ≡]> terraform apply
Acquiring state lock. This may take a few moments...
aws_dynamodb_table.terraform_locks: Refreshing state... [id=terraform-up-and-running-locks]
aws_instance.example: Refreshing state... [id=i-01120238707b3ba8e]
aws_s3_bucket.terraform_state: Refreshing state... [id=mark-kharitonov-terraform-up-and-running-state]

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Releasing state lock. This may take a few moments...

Outputs:

dynamodb_table_name = terraform-up-and-running-locks
s3_bucket_arn = arn:aws:s3:::mark-kharitonov-terraform-up-and-running-state
C:\work\terraform [master ≡]>

现在,我正在尝试遵循指南-创建一个新的工作区并在其中应用代码:

C:\work\terraform [master ≡]> terraform workspace new example1
Created and switched to workspace "example1"!

You're now on a new, empty workspace. Workspaces isolate their state,
so if you run "terraform plan" Terraform will not see any existing state
for this configuration.
C:\work\terraform [master ≡]> terraform plan
Acquiring state lock. This may take a few moments...
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.


------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_dynamodb_table.terraform_locks will be created
  + resource "aws_dynamodb_table" "terraform_locks" {
...
      + name             = "terraform-up-and-running-locks"
...
    }

  # aws_instance.example will be created
  + resource "aws_instance" "example" {
      + ami                          = "ami-0c55b159cbfafe1f0"
...
    }

  # aws_s3_bucket.terraform_state will be created
  + resource "aws_s3_bucket" "terraform_state" {
...
      + bucket                      = "mark-kharitonov-terraform-up-and-running-state"
...
    }

Plan: 3 to add, 0 to change, 0 to destroy.

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

Releasing state lock. This may take a few moments...
C:\work\terraform [master ≡]>

问题就从这里开始。在该指南中,terraform plan命令报告仅将创建一个资源-EC2实例。这意味着terraform将为后端重用相同的S3存储桶,为锁重用相同的DynamoDB表。但就我而言,terraform告诉我它想创建所有3个资源,包括S3存储桶。肯定会失败(已经尝试过)。

那么,我在做什么错?缺少什么?

1 个答案:

答案 0 :(得分:1)

创建新的workspace实际上是从头开始的。指导步骤在这方面有些混乱,但是他们正在制定两个计划以达到最终结果。第一个计划创建状态S3存储桶和锁定的DynamoDB表,第二个计划仅包含它们正在创建的实例,但使用terraform代码块来告诉该计划将其状态存储在何处。

在您的示例中,您既要设置状态位置,又要在同一计划中创建它。这意味着当您创建一个新的工作区时,它将再次尝试创建该状态位置,因为该工作区不知道其他工作区的状态。

最后,重要的是要知道使用工作区可以通过appending the workspace name to the remote state path为每个工作区创建唯一的状态文件。例如,如果您的州位置为mark-kharitonov-terraform-up-and-running-state,路径为workspaces-example,那么您可能会看到以下内容:

  • 默认状态:mark-kharitonov-terraform-up-and-running-state/workspaces-example/default/terraform.tfstate
  • 其他状态:mark-kharitonov-terraform-up-and-running-state/workspaces-example/other/terraform.tfstate

编辑:

要清楚如何获得指导结果。您需要在单独的文件夹中创建两个单独的计划(工作目录中的所有计划都将同时运行)。因此,创建一个类似以下的层次结构:

  • 计划>
    • 状态>
      • main.tf
    • 实例>
      • main.tf

在您的plans/state/main.tf文件中,放入您的状态位置内容:

provider "aws" {
  region = "us-east-2"
}

resource "aws_s3_bucket" "terraform_state" {
  bucket = "mark-kharitonov-terraform-up-and-running-state"

  # Enable versioning so we can see the full revision history of our
  # state files
  versioning {
    enabled = true
  }

  # Enable server-side encryption by default
  server_side_encryption_configuration {
    rule {
      apply_server_side_encryption_by_default {
        sse_algorithm = "AES256"
      }
    }
  }
}

resource "aws_dynamodb_table" "terraform_locks" {
  name         = "terraform-up-and-running-locks"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = "LockID"

  attribute {
    name = "LockID"
    type = "S"
  }
}

output "s3_bucket_arn" {
  value       = aws_s3_bucket.terraform_state.arn
  description = "The ARN of the S3 bucket"
}

然后在您的plans/instance/main.tf文件中,您可以使用terraform块引用创建的状态位置,并且只需要以下内容:

terraform {
  backend "s3" {
    # Replace this with your bucket name!
    bucket = "mark-kharitonov-terraform-up-and-running-state"
    key    = "workspaces-example/terraform.tfstate"
    region = "us-east-2"
    # Replace this with your DynamoDB table name!
    dynamodb_table = "terraform-up-and-running-locks"
    encrypt        = true
  }
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}