Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
32 changes: 26 additions & 6 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 Expand Up @@ -114,13 +129,15 @@ The Django backend deployment is managed by Zappa. This includes the API Gateway

5. **Deploy**:

- *Note*: Make sure to populate all `DJANGO_*` secrets that are set as `to-be-set-in-aws-console`
- **Note**: Make sure to populate all `DJANGO_*` secrets that are set as `to-be-set-in-aws-console`
in the Parameter Store. The deployment might fail with no logs if secrets such as
`DJANGO_SLACK_BOT_TOKEN` are invalid.

```bash
zappa deploy staging
```
- **Note**: If the deployment is successful but returns a `5xx` error, resolve the issues
and use `zappa undeploy staging` & `zappa deploy staging`. The command `zappa update staging` may not work.

Once deployed, use the URL provided by Zappa to test the API.

Expand Down Expand Up @@ -188,6 +205,9 @@ Migrate and load data into the new database.
zappa undeploy staging
```

- Ensure all buckets and ECR repositories are empty.

**Note:** Some resources have `prevent_destroy` set to `true`. Please set it to `false` before destruction.
- To destroy Terraform infrastructure:

```bash
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.

35 changes: 33 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 @@ -58,20 +66,32 @@ resource "aws_dynamodb_table" "state_lock" {
name = "LockID"
type = "S"
}
lifecycle {
prevent_destroy = true
}
point_in_time_recovery {
enabled = true
}
}

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

lifecycle {
prevent_destroy = true
}
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}"
object_lock_enabled = true

lifecycle {
prevent_destroy = true
}
tags = {
Name = "${var.project_name}-terraform-state"
}
Expand Down Expand Up @@ -115,6 +135,17 @@ resource "aws_s3_bucket_logging" "state" {
target_prefix = "s3/"
}

resource "aws_s3_bucket_object_lock_configuration" "state" {
bucket = aws_s3_bucket.state.id

rule {
default_retention {
days = 30
mode = "GOVERNANCE"
}
}
}

resource "aws_s3_bucket_policy" "logs" {
bucket = aws_s3_bucket.logs.id
policy = data.aws_iam_policy_document.logs.json
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/modules/ecs/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ module "load_data_task" {
set -e
pip install --target=/tmp/awscli-packages awscli
export PYTHONPATH="/tmp/awscli-packages:$PYTHONPATH"
python /tmp/awscli-packages/bin/aws s3 cp s3://${var.fixtures_s3_bucket}/nest.json.gz /tmp/nest.json.gz
python /tmp/awscli-packages/bin/aws s3 cp s3://${var.fixtures_bucket_name}/nest.json.gz /tmp/nest.json.gz
python manage.py load_data --fixture-path /tmp/nest.json.gz
EOT
]
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/modules/ecs/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ variable "fixtures_read_only_policy_arn" {
type = string
}

variable "fixtures_s3_bucket" {
variable "fixtures_bucket_name" {
description = "The name of the S3 bucket for fixtures"
type = string
}
Expand Down
16 changes: 11 additions & 5 deletions infrastructure/modules/storage/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 All @@ -15,16 +19,19 @@ data "aws_iam_policy_document" "fixtures_read_only" {
]
effect = "Allow"
resources = [
"arn:aws:s3:::${var.fixtures_s3_bucket}/*"
"arn:aws:s3:::${var.fixtures_bucket_name}-${random_id.suffix.hex}/*"
]
}
}

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

module "fixtures_bucket" {
source = "./modules/s3-bucket"

bucket_name = var.fixtures_s3_bucket
force_destroy = var.force_destroy_bucket
bucket_name = "${var.fixtures_bucket_name}-${random_id.suffix.hex}"
tags = merge(var.common_tags, {
Name = "${var.project_name}-${var.environment}-fixtures"
})
Expand All @@ -33,8 +40,7 @@ module "fixtures_bucket" {
module "zappa_bucket" {
source = "./modules/s3-bucket"

bucket_name = var.zappa_s3_bucket
force_destroy = var.force_destroy_bucket
bucket_name = "${var.zappa_bucket_name}-${random_id.suffix.hex}"
tags = merge(var.common_tags, {
Name = "${var.project_name}-${var.environment}-zappa-deployments"
})
Expand Down
11 changes: 7 additions & 4 deletions infrastructure/modules/storage/modules/s3-bucket/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@ data "aws_iam_policy_document" "this" {
}
}

resource "aws_s3_bucket" "this" { #NOSONAR
bucket = var.bucket_name
force_destroy = var.force_destroy
tags = var.tags
resource "aws_s3_bucket" "this" { # NOSONAR
bucket = var.bucket_name
tags = var.tags

lifecycle {
prevent_destroy = true
}
}

resource "aws_s3_bucket_policy" "this" {
Expand Down
10 changes: 2 additions & 8 deletions infrastructure/modules/storage/modules/s3-bucket/variables.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
variable "abort_incomplete_multipart_upload_days" {
description = "Specifies the number of days after which an incomplete multipart upload is aborted."
description = "The number of days after which an incomplete multipart upload is aborted."
type = number
default = 7
}
Expand All @@ -9,14 +9,8 @@ variable "bucket_name" {
type = string
}

variable "force_destroy" {
description = "If true, deletes all objects from the bucket when the bucket is destroyed."
type = bool
default = false
}

variable "noncurrent_version_expiration_days" {
description = "Specifies the number of days an object is noncurrent before it is expired."
description = "The number of days an object is noncurrent before it is expired."
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll remove other "specifies" like description in other PRs and make it all consistent.

type = number
default = 30
}
Expand Down
10 changes: 2 additions & 8 deletions infrastructure/modules/storage/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,17 @@ variable "environment" {
type = string
}

variable "fixtures_s3_bucket" {
variable "fixtures_bucket_name" {
description = "The name of the S3 bucket for fixtures"
type = string
}

variable "force_destroy_bucket" {
description = "If true, deletes all objects from the bucket when the bucket is destroyed."
type = bool
default = false
}

variable "project_name" {
description = "The name of the project"
type = string
}

variable "zappa_s3_bucket" {
variable "zappa_bucket_name" {
description = "The name of the S3 bucket for Zappa deployments"
type = string
}
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"
}
}
7 changes: 3 additions & 4 deletions infrastructure/staging/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ module "ecs" {
ecs_sg_id = module.security.ecs_sg_id
environment = var.environment
fixtures_read_only_policy_arn = module.storage.fixtures_read_only_policy_arn
fixtures_s3_bucket = var.fixtures_s3_bucket
fixtures_bucket_name = var.fixtures_bucket_name
private_subnet_ids = module.networking.private_subnet_ids
project_name = var.project_name
}
Expand Down Expand Up @@ -109,8 +109,7 @@ module "storage" {

common_tags = local.common_tags
environment = var.environment
fixtures_s3_bucket = var.fixtures_s3_bucket
force_destroy_bucket = var.force_destroy_bucket
fixtures_bucket_name = var.fixtures_bucket_name
project_name = var.project_name
zappa_s3_bucket = var.zappa_s3_bucket
zappa_bucket_name = var.zappa_bucket_name
}
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"
1 change: 0 additions & 1 deletion infrastructure/staging/terraform.tfvars.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ db_name = "owasp_nest"
db_user = "owasp_nest_db_user"
db_port = 5432
environment = "staging"
force_destroy_bucket = true
project_name = "owasp-nest"
12 changes: 3 additions & 9 deletions infrastructure/staging/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,10 @@ variable "environment" {
}
}

variable "force_destroy_bucket" {
description = "If true, deletes all objects from the bucket when the bucket is destroyed."
type = bool
default = false
}

variable "fixtures_s3_bucket" {
variable "fixtures_bucket_name" {
description = "The name of the S3 bucket for fixtures"
type = string
default = "nest-fixtures"
default = "owasp-nest-fixtures"
}

variable "private_subnet_cidrs" {
Expand Down Expand Up @@ -141,7 +135,7 @@ variable "vpc_cidr" {
default = "10.0.0.0/16"
}

variable "zappa_s3_bucket" {
variable "zappa_bucket_name" {
description = "The name of the S3 bucket for Zappa deployments"
type = string
default = "owasp-nest-zappa-deployments"
Expand Down
Loading