Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions infrastructure/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ Follow these steps to set up the infrastructure:
```bash
cd infrastructure/backend/
```
*Note:* Optionally change the region: set `aws_region` in a `.tfvars` file.

**Note:** Optionally change the region: set `aws_region` in a `.tfvars` file.

- Initialize Terraform if needed:
```bash
Expand All @@ -33,6 +34,10 @@ Follow these steps to set up the infrastructure:
terraform apply
```

**Note:** Copy the state bucket name from the output.

**Note:** It is recommended to not destroy the backend resources unless absolutely necessary.

2. **Setup Main Infrastructure (staging)**:

- Navigate to the main infrastructure directory. If you are in `infrastructure/backend`, you can use:
Expand All @@ -50,13 +55,23 @@ Follow these steps to set up the infrastructure:
cat terraform.tfvars.example > terraform.tfvars
```

- *Note:* Optionally change the region:
- set `aws_region` in a `.tfvars` file.
- set `region` in a `.tfbackend` file and provide it using `terraform init -backend-config=<file>`.
- Create a local backend configuration file:
```bash
touch terraform.tfbackend
```

- Copy the contents from the example file:
```bash
cat terraform.tfbackend.example > terraform.tfbackend
```

*Note:* Update the state bucket name in `terraform.tfbackend` with the name of the state bucket created in the previous step.

*Note:* Update defaults (e.g. `region`) as needed.

- Initialize Terraform with the backend configuration:
```bash
terraform init
terraform init -backend-config=terraform.tfbackend
```

- Apply the changes to create the main infrastructure using the command:
Expand Down
20 changes: 20 additions & 0 deletions infrastructure/backend/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions infrastructure/backend/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ terraform {
source = "hashicorp/aws"
version = "6.22.0"
}
random = {
source = "hashicorp/random"
version = "3.7.2"
}
}
}

Expand Down Expand Up @@ -46,6 +50,10 @@ data "aws_iam_policy_document" "state_https_only" {
}
}

resource "random_id" "suffix" {
byte_length = 4
}

resource "aws_dynamodb_table" "state_lock" {
name = "${var.project_name}-terraform-state-lock"
billing_mode = "PAY_PER_REQUEST"
Expand All @@ -64,14 +72,14 @@ resource "aws_dynamodb_table" "state_lock" {
}

resource "aws_s3_bucket" "logs" { # NOSONAR
bucket = "${var.project_name}-terraform-state-logs"
bucket = "${var.project_name}-terraform-state-logs-${random_id.suffix.hex}"
tags = {
Name = "${var.project_name}-terraform-state-logs"
}
}

resource "aws_s3_bucket" "state" { # NOSONAR
bucket = "${var.project_name}-terraform-state"
bucket = "${var.project_name}-terraform-state-${random_id.suffix.hex}"
tags = {
Name = "${var.project_name}-terraform-state"
}
Expand Down
7 changes: 2 additions & 5 deletions infrastructure/staging/backend.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
terraform {
backend "s3" {
bucket = "owasp-nest-terraform-state"
dynamodb_table = "owasp-nest-terraform-state-lock"
encrypt = true
key = "staging/terraform.tfstate"
region = "us-east-2"
encrypt = true
key = "staging/terraform.tfstate"
}
}
3 changes: 3 additions & 0 deletions infrastructure/staging/terraform.tfbackend.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bucket = "${STATE_BUCKET_NAME}"
dynamodb_table = "owasp-nest-terraform-state-lock"
region = "us-east-2"
2 changes: 1 addition & 1 deletion infrastructure/staging/terraform.tfvars.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ db_name = "owasp_nest"
db_user = "owasp_nest_db_user"
db_port = 5432
environment = "staging"
force_destroy_bucket = true
force_destroy_bucket = false
project_name = "owasp-nest"
Loading