Skip to content

Commit d59068f

Browse files
make bucket names configurable and cleanup
1 parent 686e0fb commit d59068f

File tree

9 files changed

+50
-16
lines changed

9 files changed

+50
-16
lines changed

infrastructure/backend/main.tf

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,23 @@ resource "aws_dynamodb_table" "state_lock" {
7373

7474
resource "aws_s3_bucket" "logs" { # NOSONAR
7575
bucket = "${var.project_name}-terraform-state-logs-${random_id.suffix.hex}"
76+
77+
lifecycle {
78+
prevent_destroy = true
79+
}
7680
tags = {
7781
Name = "${var.project_name}-terraform-state-logs"
7882
}
7983
}
8084

8185
resource "aws_s3_bucket" "state" { # NOSONAR
82-
bucket = "${var.project_name}-terraform-state-${random_id.suffix.hex}"
86+
bucket = "${var.project_name}-terraform-state-${random_id.suffix.hex}"
87+
object_lock_enabled = true
88+
89+
lifecycle {
90+
prevent_destroy = true
91+
}
92+
8393
tags = {
8494
Name = "${var.project_name}-terraform-state"
8595
}
@@ -123,6 +133,17 @@ resource "aws_s3_bucket_logging" "state" {
123133
target_prefix = "s3/"
124134
}
125135

136+
resource "aws_s3_bucket_object_lock_configuration" "state" {
137+
bucket = aws_s3_bucket.state.id
138+
139+
rule {
140+
default_retention {
141+
mode = "GOVERNANCE"
142+
days = 30
143+
}
144+
}
145+
}
146+
126147
resource "aws_s3_bucket_policy" "logs" {
127148
bucket = aws_s3_bucket.logs.id
128149
policy = data.aws_iam_policy_document.logs.json
@@ -169,6 +190,7 @@ resource "aws_s3_bucket_server_side_encryption_configuration" "state" {
169190

170191
resource "aws_s3_bucket_versioning" "state" {
171192
bucket = aws_s3_bucket.state.id
193+
172194
versioning_configuration {
173195
status = "Enabled"
174196
}

infrastructure/modules/ecs/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ module "load_data_task" {
217217
set -e
218218
pip install --target=/tmp/awscli-packages awscli
219219
export PYTHONPATH="/tmp/awscli-packages:$PYTHONPATH"
220-
python /tmp/awscli-packages/bin/aws s3 cp s3://${var.fixtures_s3_bucket}/nest.json.gz /tmp/nest.json.gz
220+
python /tmp/awscli-packages/bin/aws s3 cp s3://${var.fixtures_bucket_name}/nest.json.gz /tmp/nest.json.gz
221221
python manage.py load_data --fixture-path /tmp/nest.json.gz
222222
EOT
223223
]

infrastructure/modules/ecs/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ variable "fixtures_read_only_policy_arn" {
3030
type = string
3131
}
3232

33-
variable "fixtures_s3_bucket" {
33+
variable "fixtures_bucket_name" {
3434
description = "The name of the S3 bucket for fixtures"
3535
type = string
3636
}

infrastructure/modules/storage/main.tf

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ terraform {
55
source = "hashicorp/aws"
66
version = "6.22.0"
77
}
8+
random = {
9+
source = "hashicorp/random"
10+
version = "3.7.2"
11+
}
812
}
913
}
1014

@@ -15,15 +19,19 @@ data "aws_iam_policy_document" "fixtures_read_only" {
1519
]
1620
effect = "Allow"
1721
resources = [
18-
"arn:aws:s3:::${var.fixtures_s3_bucket}/*"
22+
"arn:aws:s3:::${var.fixtures_bucket_name}-${random_id.suffix.hex}/*"
1923
]
2024
}
2125
}
2226

27+
resource "random_id" "suffix" {
28+
byte_length = 4
29+
}
30+
2331
module "fixtures_bucket" {
2432
source = "./modules/s3-bucket"
2533

26-
bucket_name = var.fixtures_s3_bucket
34+
bucket_name = "${var.fixtures_bucket_name}-${random_id.suffix.hex}"
2735
force_destroy = var.force_destroy_bucket
2836
tags = merge(var.common_tags, {
2937
Name = "${var.project_name}-${var.environment}-fixtures"
@@ -33,7 +41,7 @@ module "fixtures_bucket" {
3341
module "zappa_bucket" {
3442
source = "./modules/s3-bucket"
3543

36-
bucket_name = var.zappa_s3_bucket
44+
bucket_name = "${var.zappa_bucket_name}-${random_id.suffix.hex}"
3745
force_destroy = var.force_destroy_bucket
3846
tags = merge(var.common_tags, {
3947
Name = "${var.project_name}-${var.environment}-zappa-deployments"

infrastructure/modules/storage/modules/s3-bucket/main.tf

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@ data "aws_iam_policy_document" "this" {
3535
}
3636
}
3737

38-
resource "aws_s3_bucket" "this" { #NOSONAR
38+
resource "aws_s3_bucket" "this" { # NOSONAR
3939
bucket = var.bucket_name
4040
force_destroy = var.force_destroy
4141
tags = var.tags
42+
43+
lifecycle {
44+
prevent_destroy = true
45+
}
4246
}
4347

4448
resource "aws_s3_bucket_policy" "this" {

infrastructure/modules/storage/modules/s3-bucket/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ variable "force_destroy" {
1616
}
1717

1818
variable "noncurrent_version_expiration_days" {
19-
description = "Specifies the number of days an object is noncurrent before it is expired."
19+
description = "The number of days an object is noncurrent before it is expired."
2020
type = number
2121
default = 30
2222
}

infrastructure/modules/storage/variables.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ variable "environment" {
99
type = string
1010
}
1111

12-
variable "fixtures_s3_bucket" {
12+
variable "fixtures_bucket_name" {
1313
description = "The name of the S3 bucket for fixtures"
1414
type = string
1515
}
@@ -25,7 +25,7 @@ variable "project_name" {
2525
type = string
2626
}
2727

28-
variable "zappa_s3_bucket" {
28+
variable "zappa_bucket_name" {
2929
description = "The name of the S3 bucket for Zappa deployments"
3030
type = string
3131
}

infrastructure/staging/main.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ module "ecs" {
6060
ecs_sg_id = module.security.ecs_sg_id
6161
environment = var.environment
6262
fixtures_read_only_policy_arn = module.storage.fixtures_read_only_policy_arn
63-
fixtures_s3_bucket = var.fixtures_s3_bucket
63+
fixtures_bucket_name = var.fixtures_bucket_name
6464
private_subnet_ids = module.networking.private_subnet_ids
6565
project_name = var.project_name
6666
}
@@ -109,8 +109,8 @@ module "storage" {
109109

110110
common_tags = local.common_tags
111111
environment = var.environment
112-
fixtures_s3_bucket = var.fixtures_s3_bucket
112+
fixtures_bucket_name = var.fixtures_bucket_name
113113
force_destroy_bucket = var.force_destroy_bucket
114114
project_name = var.project_name
115-
zappa_s3_bucket = var.zappa_s3_bucket
115+
zappa_bucket_name = var.zappa_bucket_name
116116
}

infrastructure/staging/variables.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ variable "force_destroy_bucket" {
8787
default = false
8888
}
8989

90-
variable "fixtures_s3_bucket" {
90+
variable "fixtures_bucket_name" {
9191
description = "The name of the S3 bucket for fixtures"
9292
type = string
93-
default = "nest-fixtures"
93+
default = "owasp-nest-fixtures"
9494
}
9595

9696
variable "private_subnet_cidrs" {
@@ -141,7 +141,7 @@ variable "vpc_cidr" {
141141
default = "10.0.0.0/16"
142142
}
143143

144-
variable "zappa_s3_bucket" {
144+
variable "zappa_bucket_name" {
145145
description = "The name of the S3 bucket for Zappa deployments"
146146
type = string
147147
default = "owasp-nest-zappa-deployments"

0 commit comments

Comments
 (0)