Mi archivo de terraformación simple es:
provider "aws" { region = "region" access_key = "key" secret_key = "secret_key" } terraform { backend "s3" { # Replace this with your bucket name! bucket = "great-name-terraform-state-2" key = "global/s3/terraform.tfstate" region = "eu-central-1" # Replace this with your DynamoDB table name! dynamodb_table = "great-name-locks-2" encrypt = true } } resource "aws_s3_bucket" "terraform_state" { bucket = "great-name-terraform-state-2" # Enable versioning so we can see the full revision history of our # state files versioning { enabled = true } server_side_encryption_configuration { rule { apply_server_side_encryption_by_default { sse_algorithm = "AES256" } } } } resource "aws_dynamodb_table" "terraform_locks" { name = "great-name-locks-2" billing_mode = "PAY_PER_REQUEST" hash_key = "LockID" attribute { name = "LockID" type = "S" } }Todo lo que estoy tratando de hacer es reemplazar mi backend local para almacenarlo en S3. Estoy haciendo lo siguiente:
terraform init (cuando el bloque terrafrom{} es un comentario)
terrafrom apply : puedo ver en mi AWS que se creó el depósito y la tabla Dynmpo también.
ahora estoy dejando de comentar el bloque terrafrom y nuevamente terraform init y obtengo el siguiente error:
Error loading state: AccessDenied: Access Denied status code: 403, request id: xxx, host id: xxxx Mi IAM tiene acceso de administrador . Estoy usando Terraform v0.12.24 como se puede observar, estoy escribiendo directamente mi clave y secreto de AWS en el archivo.
¿Qué estoy haciendo mal?
Agradezco cualquier ayuda!
Me encontré con esto antes. Los siguientes son los pasos que lo ayudarán a superar ese error:
backend "s3" { bucket = "great-name-terraform-state-2" key = "global/s3/terraform.tfstate" region = "eu-central-1" access_key = "<access-key>" secret_key = "<secret-key>" } }El error debería desaparecer.
También me enfrenté al mismo problema. Luego elimino manualmente el archivo de estado de mi sistema local. Puede encontrar el archivo terraform.tfstate en el directorio .terraform/ y ejecutar init nuevamente. en caso de que tuviera varios perfiles configurados en aws cli. no mencionar el perfil en la configuración del proveedor de aws hará que terraform use el perfil predeterminado.
Para una mejor seguridad, puede usar shared_credentials_file y profile así;
provider "aws" { region = "region" shared_credentials_file = "$HOME/.aws/credentials # default profile = "default" # you may change to desired profile } terraform { backend "s3" { profile = "default" # change to desired profile # Replace this with your bucket name! bucket = "great-name-terraform-state-2" key = "global/s3/terraform.tfstate" region = "eu-central-1" # Replace this with your DynamoDB table name! dynamodb_table = "great-name-locks-2" encrypt = true } }