Skip to content

Commit

Permalink
add remote backend
Browse files Browse the repository at this point in the history
  • Loading branch information
hazzardr committed Feb 11, 2024
1 parent 84845c7 commit a620b2f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
4 changes: 4 additions & 0 deletions chapter-3/backend.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bucket = "hazzardr-terraform-up-and-running-state"
region = "us-east-2"
dynamodb_table = "terraform-up-and-running-locks"
encrypt = true
37 changes: 35 additions & 2 deletions chapter-3/main.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,42 @@
variable "state_bucket_name" {
description = "the name of the s3 bucket which hosts the backend state"
type = string
default = "hazzardr-terraform-up-and-running-state"
}
variable "state_lock_table_name" {
description = "the name of the dynamodb table that tracks locks on backen state"
type = string
default = "terraform-up-and-running-locks"
}

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

output "dynamodb_table_name" {
value = aws_dynamodb_table.terraform_locks.name
description = "the name of the table managing the locks on the backend state"

}

# We use partial configuration which will be passed in via `backend.hcl` to `tf init`
# e.g.:
# $ terraform init -backend-config=backend.hcl
#
# However, Key has to be unique so we don't parameterize it
terraform {
backend "s3" {
key = "global/s3/terraform.tfstate"
}
}

provider "aws" {
region = "us-east-2"
}

resource "aws_s3_bucket" "terraform_state" {
bucket = "hazzardr-terraform-up-and-running-state"
bucket = var.state_bucket_name

# so we don't accidentally delete remote state with a destroy command
lifecycle {
Expand Down Expand Up @@ -39,7 +72,7 @@ resource "aws_s3_bucket_public_access_block" "public_access" {
}

resource "aws_dynamodb_table" "terraform_locks" {
name = "terraform-up-and-running-locks"
name = var.state_lock_table_name
billing_mode = "PAY_PER_REQUEST"
hash_key = "LockID"
attribute {
Expand Down

0 comments on commit a620b2f

Please sign in to comment.