【发布时间】:2021-05-20 12:50:35
【问题描述】:
概述
在学习Terraform的状态锁定时,有些地方我不明白。
- terraform v0.14.6
# Specify the provider and access details
provider "aws" {
region = "ap-northeast-1"
profile = "default"
}
terraform {
backend "s3" {
key = "terraform.tfstate"
bucket = "terraform-sample-yuta"
region = "ap-northeast-1"
dynamodb_table = "terraform-state-lock-dynamo"
}
}
resource "aws_dynamodb_table" "dynamodb-terraform-state-lock" {
name = "terraform-state-lock-dynamo"
hash_key = "LockID"
read_capacity = 20
write_capacity = 20
attribute {
name = "LockID"
type = "S"
}
tags = {
Name = "DynamoDB State Lock Table"
}
}
resource "aws_instance" "web" {
instance_type = "t3.small"
# Amazon Linux2
ami = "ami-0992fc94ca0f1415a"
count = 1
tags = {
Name = "EC2 instance terraform"
}
}
我执行这个main.tf,terraform plan main.tf,出现如下错误。
$ terraform plan
Error: Error locking state: Error acquiring the state lock: 2 errors occurred:
* ResourceNotFoundException: Requested resource not found
* ResourceNotFoundException: Requested resource not found
Terraform acquires a state lock to protect the state from being written
by multiple users at the same time. Please resolve the issue above and try
again. For most commands, you can disable locking with the "-lock=false"
flag, but this is not recommended.
出现类似错误,Terraform Error: Error locking state: Error acquiring the state lock: 2 errors occurred:
所以,我提前创建了 DynamoDB。 但是,发生了另一个错误。
Acquiring state lock. This may take a few moments...
aws_instance.web[0]: Refreshing state... [id=i-084998a0833bc68cb]
aws_dynamodb_table.dynamodb-terraform-state-lock: Creating...
Error: error creating DynamoDB Table: ResourceInUseException: Table already exists: terraform-state-lock-dynamo
我可以使用-lock=false 标志解决它,但不建议这样做。
请在不使用-lock=false 标志的情况下给我一些建议。
【问题讨论】:
标签: amazon-web-services amazon-dynamodb terraform