Skip to content

Commit 5fd5b26

Browse files
committed
feat: simplify terraform.
1 parent 14dd88c commit 5fd5b26

File tree

12 files changed

+90
-187
lines changed

12 files changed

+90
-187
lines changed

.github/actions/terraform-plan/action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ runs:
1717
shell: bash
1818
run: ./import_resources.sh
1919

20+
- name: Terraform Import Existing Secrets
21+
working-directory: terraform
22+
shell: bash
23+
run: ./import_secrets.sh
24+
2025
- name: Validate Terraform
2126
working-directory: terraform
2227
shell: bash

terraform/app_runner.tf

Lines changed: 0 additions & 52 deletions
This file was deleted.

terraform/iam.tf

Lines changed: 0 additions & 70 deletions
This file was deleted.

terraform/import_secrets.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
# Define the environments
4+
environments=("test" "prod")
5+
6+
# Loop through each environment
7+
for env in "${environments[@]}"; do
8+
# Get the ARN of the secret
9+
secret_arn=$(aws secretsmanager list-secrets --query "SecretList[?Name=='${env}/postgresql/credentials'].ARN" --output text)
10+
11+
# Check if the secret exists
12+
if [ -n "$secret_arn" ]; then
13+
echo "Importing secret for environment: $env"
14+
terraform import "aws_secretsmanager_secret.db_secrets[\"$env\"]" "$secret_arn"
15+
else
16+
echo "Secret for environment: $env does not exist"
17+
fi
18+
done

terraform/outputs.tf

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,3 @@ output "endpoints" {
3737
env => instance.endpoint
3838
}
3939
}
40-
41-
output "app_runner_services" {
42-
value = {
43-
for env, service in aws_apprunner_service.service : env => {
44-
service_url = service.service_url
45-
service_id = service.service_id
46-
status = service.status
47-
}
48-
}
49-
}
50-
51-
output "custom_domains" {
52-
value = {
53-
for env, domain in aws_apprunner_custom_domain_association.domain : env => {
54-
domain_name = domain.domain_name
55-
status = domain.status
56-
dns_target = domain.dns_target
57-
}
58-
}
59-
}

terraform/rds.tf

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
resource "aws_db_subnet_group" "postgresql" {
22
name = "postgresql-subnet-group"
33
subnet_ids = aws_subnet.private[*].id
4+
5+
lifecycle {
6+
prevent_destroy = true
7+
}
48
}
59

610
resource "aws_db_instance" "postgresql" {
@@ -18,11 +22,15 @@ resource "aws_db_instance" "postgresql" {
1822

1923
skip_final_snapshot = true
2024

21-
vpc_security_group_ids = [aws_security_group.rds_sg.id]
25+
vpc_security_group_ids = [var.rds_security_group_id]
2226
db_subnet_group_name = aws_db_subnet_group.postgresql.name
2327

2428
publicly_accessible = false
2529
multi_az = false
2630

2731
tags = each.value.tags
32+
33+
lifecycle {
34+
prevent_destroy = true
35+
}
2836
}

terraform/secrets.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ resource "aws_secretsmanager_secret" "db_secrets" {
22
for_each = var.environments
33
name = "${each.key}/postgresql/credentials"
44
tags = each.value.tags
5+
6+
lifecycle {
7+
prevent_destroy = true
8+
}
59
}
610

711
resource "aws_secretsmanager_secret_version" "secret_versions" {
@@ -16,4 +20,8 @@ resource "aws_secretsmanager_secret_version" "secret_versions" {
1620
dbname = each.value.db_name
1721
})
1822
depends_on = [aws_db_instance.postgresql]
23+
24+
lifecycle {
25+
create_before_destroy = true
26+
}
1927
}

terraform/security.tf

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,8 @@ resource "aws_security_group" "rds_sg" {
99
protocol = "tcp"
1010
cidr_blocks = ["10.0.0.0/16"] # Replace with appropriate CIDR block
1111
}
12-
}
13-
14-
resource "aws_security_group" "vpce" {
15-
name = "vpce-security-group"
16-
description = "Security group for VPC Endpoints"
17-
vpc_id = aws_vpc.main.id
18-
19-
ingress {
20-
from_port = 443
21-
to_port = 443
22-
protocol = "tcp"
23-
cidr_blocks = [aws_vpc.main.cidr_block]
24-
}
25-
}
26-
27-
resource "aws_security_group" "app_runner" {
28-
name = "app-runner-security-group"
29-
description = "Security group for App Runner VPC Connector"
30-
vpc_id = aws_vpc.main.id
31-
32-
# Allow outbound traffic to RDS
33-
egress {
34-
from_port = 5432
35-
to_port = 5432
36-
protocol = "tcp"
37-
cidr_blocks = ["10.0.0.0/16"] # Replace with appropriate CIDR block
38-
}
39-
40-
# Allow all other outbound traffic
41-
egress {
42-
from_port = 0
43-
to_port = 0
44-
protocol = "-1"
45-
cidr_blocks = ["0.0.0.0/0"]
46-
}
4712

48-
tags = {
49-
Name = "app-runner-sg"
13+
lifecycle {
14+
prevent_destroy = true
5015
}
5116
}

terraform/terraform.tfvars

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# Overrides the default values for the variables defined in variables.tf
2-
region = "us-west-2"
2+
region = "us-west-2"
3+
rds_security_group_id = "sg-0c40327281d3f4db4"

terraform/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,8 @@ variable "environments" {
2121
}
2222
}
2323
}
24+
25+
variable "rds_security_group_id" {
26+
description = "The security group ID for the RDS instance"
27+
type = string
28+
}

0 commit comments

Comments
 (0)