Home > other >  Terraform file for AWS S3 bucket keeps getting Error: Invalid provider configuration
Terraform file for AWS S3 bucket keeps getting Error: Invalid provider configuration

Time:03-05

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?

CodePudding user response:

As commenter Marko E already said: You get this error because of the provider configuration.

Replace this in your main.tf:

required_providers {
    aws = {
        version = " ~> 3.0"
        source = "registry.terraform.io/hashicorp/aws"
    }
}

With this:

required_providers {
    aws = {
        version = "~> 3.0"
        source = "hashicorp/aws"
    }
}

CodePudding user response:

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

  • Related