配置 S3 后端时 Terraform 出现错误

Eya*_*mon 6 amazon-s3 amazon-web-services terraform terraform-provider-aws

尝试将我的状态文件存储在 s3 存储桶中,但在尝试“Terraform init”时出现此错误:

  • 确保我的 aws 凭证没有“/# $ ..”
error configuring S3 Backend: error validating provider credentials: 
error calling sts:GetCallerIdentity: 
InvalidClientTokenId: The security token included in the request is invalid.
Run Code Online (Sandbox Code Playgroud)

主要.tf:

provider "aws" {
  region     = var.region
  access_key = var.acc_key
  secret_key = var.sec_key
}

terraform {
  backend "s3" {
    bucket         = "mybucket-terra-prac"
    key            = "terraform.tfstate"
    region         = "eu-central-1"
  }
}
resource "aws_instance" "web" {
  ami           = var.ami
  instance_type = "t2.large"
  associate_public_ip_address=true
  key_name = var.public_key
  tags = {
    Name = var.ec2_name
  }
}
Run Code Online (Sandbox Code Playgroud)

我在 Variables.tf 文件中拥有的变量(具有类型和默认值):

variable "acc_key" {}
variable "sec_key" {}
variable "public_key" {}
variable "ami" {}
Run Code Online (Sandbox Code Playgroud)

小智 1

region我不完全确定,但我认为在节中指定时不能使用变量aws provider。我认为你需要将其硬编码到你的区域。同样也不完全确定,但在secretaccess键中使用变量应该进行硬编码,而不是将其指向变量(这些参数旨在直接在 terraform 文件内指定值时使用)。

并且该terraform部分应放置在文件开头的aws provider部分之前。