brikis98 / terraform-up-and-running-code

Code samples for the book "Terraform: Up & Running" by Yevgeniy Brikman
http://www.terraformupandrunning.com/
MIT License
2.87k stars 1.92k forks source link

chapter 3 issue #45

Closed davidATsains closed 4 years ago

davidATsains commented 4 years ago

Hello, i am currently using this book to learn Terraform. I am currently Chapter 3, sub section: shared storage for state files. I have entered the terrafrorm codes to create s3 bucket, dynamodb and to initialise remote backend but i keep getting errors. please help me this is my code

provider "aws" {
  region = "eu-west-1"
}
resource "aws_s3_bucket" "terraform_state" {
  bucket = "test-2020-bucket"

  lifecycle {
    prevent_destroy = true
  }

  versioning {
    enabled = true
  }

  server_side_encryption_configuration {
    rule {
      apply_server_side_encryption_by_default {
        sse_algorithm = "AES256"
      }
    }
  }
}

resource "aws_dynamodb_table" "locks-2020" {
  name         = "test-2020"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = "LockID"

  attribute {
    name = "LockID"
    type = "S"
  }

  tags = {
    Name = "dynamodb-table-1"
    Environment = "development"
  }
}

terraform {
  backend "s3" {
    bucket         = "terraform-test-"
    key            = "global/s3/terraform.tfstate"
    region         = "eu-west-1"
    dynamodb_table = "terraform-test"
    encrypt        = true
  }

}

Screenshot 2019-12-27 at 22 17 57

davidATsains commented 4 years ago

i . fixed it

brikis98 commented 4 years ago

For those that hit this later, you need to create the S3 bucket and DynamoDB table first, and only after they have been created, should you add the terraform { backend "s3" { ... } } portion of the code.