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 (workspaces issue) #46

Closed davidATsains closed 4 years ago

davidATsains commented 4 years ago

Chapter 3 Sub section: Isolation via workspace I have followed this section. I created the new worksapce called example1. i run terraform plan and then terraform apply but it did not complete. Instead I got an error Screenshot 2019-12-31 at 12 09 43

from the book, it is suppose to only apply the EC2 instance but instead it want to create the S3 bucket, DynamoDb table and EC2 instances.

Please help me understand where this is happening

here is my code

provider "aws" {
  region     = "eu-west-1"
  version    = "~>2.0"
}

resource "aws_instance" "example" {
  ami           = "ami-02df9ea15c1778c9c"
  instance_type = "t2.micro"
}

resource "aws_s3_bucket" "terraform_state" {
  bucket = "bucket name"

  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         = "name here"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = "LockID"

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

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

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

output "s3_bucket_arn" {
  value       = aws_s3_bucket.terraform_state.arn
  description = "The ARN of the S3 bucket"
}

output "dynamodb_table_name" {
  value       = aws_dynamodb_table.name_here.name
  description = "The name of the DynamoDB"
}
brikis98 commented 4 years ago

The examples with workspaces should only be creating an aws_instance resource. Why do you have aws_s3_bucket and aws_dynamodb_table there too? Those likely exist from earlier and don't need to be created again!

davidATsains commented 4 years ago

The examples with workspaces should only be creating an aws_instance resource. Why do you have aws_s3_bucket and aws_dynamodb_table there too? Those likely exist from earlier and don't need to be created again!

Thank you . Normally you instruct the reader to remove it as it is ready created. I like the clarity when you instruct the reader to clean up. I believe that this is important for beginners like me. Please in the future editions . - it would be good to have more these kind of pointers in the book.

brikis98 commented 4 years ago

Understood, thx for the feedback!