AWS S3 存储桶的 Terraform 文件不断收到错误:提供程序配置无效
编辑:我现在已经修改了问题以显示整个 main.tf 文件。
我有一个应该创建 AWS S3 存储桶的 Terraform 文件,但每次运行 terraform plan
时不断收到的错误之一是:
│ Error: Invalid provider configuration
│ Provider "registry.terraform.io/hashicorp/aws" requires explicit configuration. Add a provider block to the root module and configure the provider's required arguments as described in the provider documentation.
The main.tf file:
terraform {
backend "s3" {
region = "us-east-1"
bucket = "bucketname"
key = "path/terraform.tfstate"
dynamodb_table = "tf-state-lock"
access_key = "<access_key>"
secret_key = "<secret_key"
}
required_providers {
aws = {
version = " ~> 3.0"
source = "registry.terraform.io/hashicorp/aws"
}
}
}
provider "aws" {
alias = "east"
region = "us-east-1"
access_key = "access_key"
secret_key = "secret_key"
}
resource "aws_s3_bucket" "tf-remote-state" {
bucket = "bucketname"
versioning {
enabled = true
}
lifecycle {
prevent_destroy = true
}
}
resource "aws_dynamodb_table" "dynamodb-tf-state-lock" {
name = "tf-state-lock"
hash_key = "LockID"
read_capacity = 20
write_capacity = 20
attribute {
name = "LockID"
type = "S"
}
tags = {
name = "state lock"
}
}
What am I do bad 我做错了什么?
Edit: I've modified the question to show the entire main.tf file, now.
I have a Terraform file that is supposed to create an AWS S3 bucket, but one of the errors I keep getting every time I run terraform plan
is this:
│ Error: Invalid provider configuration
│ Provider "registry.terraform.io/hashicorp/aws" requires explicit configuration. Add a provider block to the root module and configure the provider's required arguments as described in the provider documentation.
The main.tf file:
terraform {
backend "s3" {
region = "us-east-1"
bucket = "bucketname"
key = "path/terraform.tfstate"
dynamodb_table = "tf-state-lock"
access_key = "<access_key>"
secret_key = "<secret_key"
}
required_providers {
aws = {
version = " ~> 3.0"
source = "registry.terraform.io/hashicorp/aws"
}
}
}
provider "aws" {
alias = "east"
region = "us-east-1"
access_key = "access_key"
secret_key = "secret_key"
}
resource "aws_s3_bucket" "tf-remote-state" {
bucket = "bucketname"
versioning {
enabled = true
}
lifecycle {
prevent_destroy = true
}
}
resource "aws_dynamodb_table" "dynamodb-tf-state-lock" {
name = "tf-state-lock"
hash_key = "LockID"
read_capacity = 20
write_capacity = 20
attribute {
name = "LockID"
type = "S"
}
tags = {
name = "state lock"
}
}
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您在
provider
块中使用alias
[1],当前设置资源的方式将导致 Terraform 期望使用无别名的provider
阻止您要创建的资源存在:换句话说,由于您没有在两个资源块(对于 S3 和 DynamoDB)中指定别名提供程序的名称,Terraform 希望默认使用非别名提供程序配置。由于您没有没有
alias
的提供程序块,因此修复它的最简单方法可能是将以下内容添加到两个资源块 [2]:整个块将如下所示:
但是,如果没有任何特殊原因需要使用别名(即无论如何都会在一个区域中创建资源),则仅设置
region
(就像您已经做的那样)也可以解决问题。如果您不想使用别名,只需从provider
块中删除alias = "east"
即可,然后您无需更改资源块中的任何内容,该代码将按原样工作。[1] https://www.terraform.io/language/提供商/配置#alias-multiple-provider-configurations
[2] https://www.terraform.io/language/providers/configuration#selecting -备用提供商配置
Since you are using
alias
[1] in theprovider
block, the way resources are currently set up will cause Terraform to expect the un-aliasedprovider
block to exist for the resources you want to create:In other words, since you are not specifying the name of the aliased provider in the two resource blocks (for S3 and DynamoDB), Terraform wants to use the un-aliased provider configuration by default. As you don't have the provider block without
alias
, probably the easiest way to fix it is by adding the following to both resource blocks [2]:The whole blocks would then look like this:
However, if there are not any special reasons to use aliases (i.e., resources will be created in one region anyway), setting just the
region
(as you already did) would do the trick as well. If you do not want to use the alias, just drop thealias = "east"
from theprovider
block and then you will not have to change anything in the resource blocks and the code will work as is.[1] https://www.terraform.io/language/providers/configuration#alias-multiple-provider-configurations
[2] https://www.terraform.io/language/providers/configuration#selecting-alternate-provider-configurations
我来自谷歌寻找
这就是它的修复方式:要么将此行添加到您的
main.tf
中:或者定义一个环境变量:
I came from Google looking for
This is how it's fixed: either you add this line to your
main.tf
:or you define an environment variable: