AWS S3 存储桶的 Terraform 文件不断收到错误:提供程序配置无效

发布于 2025-01-11 13:29:57 字数 1436 浏览 0 评论 0原文

编辑:我现在已经修改了问题以显示整个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野稚 2025-01-18 13:29:57

由于您在 provider 块中使用 alias [1],当前设置资源的方式将导致 Terraform 期望使用无别名的 provider 阻止您要创建的资源存在:

默认情况下,资源使用从资源类型名称的第一个单词推断出的默认提供程序配置(没有别名参数的配置)。

换句话说,由于您没有在两个资源块(对于 S3 和 DynamoDB)中指定别名提供程序的名称,Terraform 希望默认使用非别名提供程序配置。由于您没有没有 alias 的提供程序块,因此修复它的最简单方法可能是将以下内容添加到两个资源块 [2]:

provider = aws.east

整个块将如下所示:

resource "aws_s3_bucket" "tf-remote-state" {
  provider = aws.east
  bucket   = "bucketname" 

  versioning {
    enabled = true
  }

  lifecycle {
    prevent_destroy = true
  }
}

resource "aws_dynamodb_table" "dynamodb-tf-state-lock" {
  provider        = aws.east
  name            = "tf-state-lock" 
  hash_key        = "LockID"
  read_capacity   = 20
  write_capacity = 20

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

  tags = {
    name = "state lock"
  }
}

但是,如果没有任何特殊原因需要使用别名(即无论如何都会在一个区域中创建资源),则仅设置 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 the provider block, the way resources are currently set up will cause Terraform to expect the un-aliased provider block to exist for the resources you want to create:

By default, resources use a default provider configuration (one without an alias argument) inferred from the first word of the resource type name.

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]:

provider = aws.east

The whole blocks would then look like this:

resource "aws_s3_bucket" "tf-remote-state" {
  provider = aws.east
  bucket   = "bucketname" 

  versioning {
    enabled = true
  }

  lifecycle {
    prevent_destroy = true
  }
}

resource "aws_dynamodb_table" "dynamodb-tf-state-lock" {
  provider        = aws.east
  name            = "tf-state-lock" 
  hash_key        = "LockID"
  read_capacity   = 20
  write_capacity = 20

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

  tags = {
    name = "state lock"
  }
}

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 the alias = "east" from the provider 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

年少掌心 2025-01-18 13:29:57

我来自谷歌寻找

提供商“registry.terraform.io/hashicorp/aws”需要显式配置。

这就是它的修复方式:要么将此行添加到您的 main.tf 中:

provider "aws" {
  region = "eu-central-1"
}

或者定义一个环境变量:

AWS_REGION=eu-central-1

I came from Google looking for

Provider "registry.terraform.io/hashicorp/aws" requires explicit configuration.

This is how it's fixed: either you add this line to your main.tf:

provider "aws" {
  region = "eu-central-1"
}

or you define an environment variable:

AWS_REGION=eu-central-1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文