Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform getting error when configuring S3 Backend

trying to store my state file in an s3 bucket , but getting this error when trying 'Terraform init' :

  • made sure my aws credentials doesnt have " / # $ .. "
error configuring S3 Backend: error validating provider credentials: 
error calling sts:GetCallerIdentity: 
InvalidClientTokenId: The security token included in the request is invalid.

main.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
  }
}

variables i have in variables.tf file ( with type and default ) :

variable "acc_key" {}
variable "sec_key" {}
variable "public_key" {}
variable "ami" {}
like image 436
Eyal Solomon Avatar asked May 25 '26 19:05

Eyal Solomon


2 Answers

Try execute aws sts get-caller-identity command and see you are using correct credentials.

like image 160
Vidura Dantanarayana Avatar answered May 27 '26 09:05

Vidura Dantanarayana


In my case, I was able to resolve the issue by deleting the .terraform/ folder then running the terraform init again.

like image 27
Edward Mercado Avatar answered May 27 '26 08:05

Edward Mercado