Skip to content

Commit 4711c5c

Browse files
committed
fix: Address feedback from SonarQube and CodeRabbit reviews
1 parent 8ca9bcb commit 4711c5c

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

Terraform/Operational-Guide.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,27 @@ This infrastructure requires an S3 bucket and a DynamoDB table for managing Terr
4646
2. **Create the S3 Bucket for Terraform State:**
4747
*This bucket will store the `.tfstate` file, which is Terraform's map of your infrastructure.*
4848
```bash
49-
aws s3api create-bucket \
50-
--bucket ${TF_STATE_BUCKET} \
51-
--region ${AWS_REGION} \
52-
--create-bucket-configuration LocationConstraint=${AWS_REGION}
49+
if [ "${AWS_REGION}" = "us-east-1" ]; then
50+
aws s3api create-bucket --bucket "${TF_STATE_BUCKET}" --region "${AWS_REGION}"
51+
else
52+
aws s3api create-bucket \
53+
--bucket "${TF_STATE_BUCKET}" \
54+
--region "${AWS_REGION}" \
55+
--create-bucket-configuration LocationConstraint=${AWS_REGION}
56+
fi
5357
5458
aws s3api put-bucket-versioning \
5559
--bucket ${TF_STATE_BUCKET} \
5660
--versioning-configuration Status=Enabled
61+
62+
63+
aws s3api put-public-access-block \
64+
--bucket "${TF_STATE_BUCKET}" \
65+
--public-access-block-configuration 'BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true'
66+
67+
aws s3api put-bucket-encryption \
68+
--bucket "${TF_STATE_BUCKET}" \
69+
--server-side-encryption-configuration '{"Rules":[{"ApplyServerSideEncryptionByDefault":{"SSEAlgorithm":"AES256"}}]}'
5770
```
5871
5972
3. **Create the DynamoDB Table for State Locking:**
@@ -108,7 +121,7 @@ To deploy an environment, navigate to its directory and run the standard Terrafo
108121
109122
1. **Navigate to the Environment Directory:**
110123
```bash
111-
cd terraform/environments/dev
124+
cd Terraform/environments/Dev
112125
```
113126
114127
2. **Create a `terraform.tfvars` file:**

Terraform/modules/01-Network/main.tf

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ resource "aws_eip" "nat" {
7373
}
7474

7575
resource "aws_nat_gateway" "main" {
76-
# Only one NAT Gateway, placed in the first public subnet for simplicity.
77-
# As AWS automatically handles failover at the infrastructure level.
76+
# Cost-optimized: a single NAT Gateway in the first public subnet.
77+
# NOTE: This is a single-AZ SPOF for egress. A per-AZ NAT option could be added for higher availability.
7878
allocation_id = aws_eip.nat.id
7979
subnet_id = aws_subnet.public[0].id
8080

@@ -182,6 +182,7 @@ resource "aws_lb" "main" {
182182
load_balancer_type = "application"
183183
security_groups = [aws_security_group.alb.id]
184184
subnets = aws_subnet.public[*].id
185+
drop_invalid_header_fields = true
185186

186187
# Deletion protection should be enabled via a variable for production.
187188
enable_deletion_protection = var.environment == "prod" ? true : false
@@ -213,7 +214,7 @@ resource "aws_lb_listener" "https" {
213214
load_balancer_arn = aws_lb.main.arn
214215
port = 443
215216
protocol = "HTTPS"
216-
ssl_policy = "ELBSecurityPolicy-2016-08"
217+
ssl_policy = "ELBSecurityPolicy-TLS13-1-2-2021-06"
217218
certificate_arn = var.acm_certificate_arn
218219

219220
default_action {

Terraform/modules/01-Network/variables.tf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ variable "public_subnet_cidrs" {
3131
description = "A list of CIDR blocks for the public subnets. The number of CIDRs must match the number of availability_zones."
3232
type = list(string)
3333
validation {
34-
condition = length(var.public_subnet_cidrs) > 0
35-
error_message = "At least one public subnet CIDR must be provided."
36-
}
34+
condition = length(var.public_subnet_cidrs) > 0 && length(var.public_subnet_cidrs) == length(var.availability_zones)
35+
error_message = "Provide at least one public subnet CIDR, and ensure its count matches availability_zones."
36+
}
3737
}
3838

3939
variable "private_subnet_cidrs" {
4040
description = "A list of CIDR blocks for the private subnets. The number of CIDRs must match the number of availability_zones."
4141
type = list(string)
4242
validation {
43-
condition = length(var.private_subnet_cidrs) > 0
44-
error_message = "At least one private subnet CIDR must be provided."
43+
condition = length(var.private_subnet_cidrs) > 0 && length(var.private_subnet_cidrs) == length(var.availability_zones)
44+
error_message = "Provide at least one private subnet CIDR, and ensure its count matches availability_zones."
4545
}
4646
}
4747

0 commit comments

Comments
 (0)