Home > front end >  Terraform init Error: Failed to get existing workspaces: S3 bucket does not exist
Terraform init Error: Failed to get existing workspaces: S3 bucket does not exist

Time:02-12

Hi i have an issue with terraform not being able to see the s3 bucket when i specify it as a backend

aws --profile terraform s3api create-bucket --bucket "some_name_here" --region "eu-west-2" \
          --create-bucket-configuration LocationConstraint="eu-west-2"

terraform init

Initializing modules...

Initializing the backend...

Error: Failed to get existing workspaces: S3 bucket does not exist.

The referenced S3 bucket must have been previously created. If the S3 bucket
was created within the last minute, please wait for a minute or two and try
again.

Error: NoSuchBucket: The specified bucket does not exist
    status code: 404, request id: QYJT8KP0W4TM986A, host id: a7R1EOOnIhP6YzDcKd66zdyCJ8wk6lVom/tohsc0ipUe5yEJK1/V4bLGX9khi4q4/J7d4BgYXCc=

backend.tf

terraform {
  backend "s3" {
    bucket = "some_name_here"
    key    = "networking/terraform.tfstate"
    region = "eu-west-2"
  }
}

provider.tf

provider "aws" {
  region                  = "eu-west-2"
  shared_credentials_file = "$HOME/.aws/credentials"
  profile                 = "terraform"
}

I can see the bucket in the dashboard

CodePudding user response:

It looks like you are using a profile in the command to create the bucket. Therefore, you probably need to export a variable in the environment running terraform to use this same profile. I imagine terraform without this profile or another with sufficient permissions is unable to read from the bucket.

export AWS_PROFILE=terraform
terraform init

Alternatively, you can pass the profile into the backend configuration, like:

terraform {
  backend "s3" {
    bucket  = "some_name_here"
    key     = "networking/terraform.tfstate"
    profile = "terraform"
    region  = "eu-west-2"
  }
}

To summarize, the most simple configuration is:

terraform {
  backend "s3" {
    bucket  = "some_name_here"
    key     = "networking/terraform.tfstate"
    region  = "eu-west-2"
  }
}

provider "aws" {
  region = "eu-west-2"
}

then:

export AWS_PROFILE=terraform
aws s3api create-bucket --bucket "some_name_here" --region "eu-west-2" --create-bucket-configuration LocationConstraint="eu-west-2"
terraform init
  • Related