Skip to content

Commit 5e7f79e

Browse files
committed
More changes made during the video
1 parent 17db1e1 commit 5e7f79e

File tree

5 files changed

+40
-33
lines changed

5 files changed

+40
-33
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ jobs:
160160
outputfile.txt
161161
env:
162162
NODE_ENV: development
163-
DATABASE_URL: postgres://${{ secrets.DB_USERNAME }}:${{ secrets.DB_PASSWORD }}@${{ secrets.DB_HOST }}:${{ secrets.DB_PORT }}/${{ secrets.DB_NAME }}
164163

164+
# sets up Docker Buildx to enable advanced build features in the workflow.
165165
- name: Set up Docker Buildx
166166
uses: docker/setup-buildx-action@v1
167167

terraform/modules/api_gateway/main.tf

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,24 @@ resource "aws_api_gateway_rest_api" "api" {
66
resource "aws_api_gateway_resource" "proxy" {
77
rest_api_id = aws_api_gateway_rest_api.api.id
88
parent_id = aws_api_gateway_rest_api.api.root_resource_id
9-
path_part = "{proxy+}"
9+
path_part = "{proxy+}" # Path part that acts as a catch-all proxy for any request path.
1010

11-
depends_on = [ aws_api_gateway_rest_api.api ]
11+
depends_on = [ aws_api_gateway_rest_api.api ] # Ensure the API is created before creating the resource.
1212
}
1313

1414
resource "aws_api_gateway_method" "proxy_method" {
1515
rest_api_id = aws_api_gateway_rest_api.api.id
1616
resource_id = aws_api_gateway_resource.proxy.id
17-
http_method = "ANY"
18-
authorization = "NONE"
19-
api_key_required = false
17+
http_method = "ANY" # Handle every type of HTTP request
18+
authorization = "NONE" # No authorization required (yet)
19+
api_key_required = false # No API key required (yet)
2020
request_parameters = {
2121
"method.request.path.proxy" = true
2222
}
23+
# This configuration allows the API Gateway to serve as a proxy for my actual backend application, handling all types of HTTP requests and forwarding them to the backend.
2324
}
2425

26+
// Define the OPTIONS method for the proxy resource (for CORS preflight requests)
2527
resource "aws_api_gateway_method" "proxy_options" {
2628
rest_api_id = aws_api_gateway_rest_api.api.id
2729
resource_id = aws_api_gateway_resource.proxy.id
@@ -30,12 +32,14 @@ resource "aws_api_gateway_method" "proxy_options" {
3032
api_key_required = false
3133
}
3234

35+
# Define the integration between the proxy resource and the backend application. Basically, the API Gateway will forward all requests to the backend application.
3336
resource "aws_api_gateway_integration" "proxy_integration" {
3437
rest_api_id = aws_api_gateway_rest_api.api.id
3538
resource_id = aws_api_gateway_resource.proxy.id
3639
http_method = aws_api_gateway_method.proxy_method.http_method
3740
type = "HTTP_PROXY"
3841
integration_http_method = "ANY"
42+
# Load balancer knows that port 3000 is the backend application
3943
uri = "http://${var.lb_dns_name}:3000/{proxy}"
4044
request_parameters = {
4145
"integration.request.path.proxy" = "method.request.path.proxy"
@@ -66,6 +70,7 @@ resource "aws_api_gateway_integration" "health_integration" {
6670
uri = "http://${var.lb_dns_name}:3000/health"
6771
}
6872

73+
# Defines how API Gateway should handle the OPTIONS method for the proxy resource. In this case, it uses a MOCK integration to generate a mock response.
6974
resource "aws_api_gateway_integration" "proxy_options_integration" {
7075
rest_api_id = aws_api_gateway_rest_api.api.id
7176
resource_id = aws_api_gateway_resource.proxy.id
@@ -76,6 +81,9 @@ resource "aws_api_gateway_integration" "proxy_options_integration" {
7681
}
7782
}
7883

84+
# In Amazon API Gateway, an aws_api_gateway_method_response specifies the possible responses from an API Gateway, while an aws_api_gateway_integration_response maps the response from an integration to the API Gateway response.
85+
86+
# This resource specifies the response parameters (headers) that the integration should return. It is part of the integration setup and tells API Gateway what to include in the response when the OPTIONS method is called.
7987
resource "aws_api_gateway_integration_response" "proxy_options_integration_response" {
8088
rest_api_id = aws_api_gateway_rest_api.api.id
8189
resource_id = aws_api_gateway_resource.proxy.id
@@ -88,6 +96,7 @@ resource "aws_api_gateway_integration_response" "proxy_options_integration_respo
8896
}
8997
}
9098

99+
# This resource specifies the method response parameters (headers) that the method should return. It is part of the method setup and ensures that the headers specified in the integration response are actually included in the final response sent to the client.
91100
resource "aws_api_gateway_method_response" "proxy_options_response" {
92101
rest_api_id = aws_api_gateway_rest_api.api.id
93102
resource_id = aws_api_gateway_resource.proxy.id
@@ -109,15 +118,18 @@ resource "aws_api_gateway_deployment" "api_deployment" {
109118
]
110119
rest_api_id = aws_api_gateway_rest_api.api.id
111120

121+
# This effectively triggers a redeployment whenever I do `terraform apply`, even if there are no actual changes to the configuration. I need to experiment with this setting.
112122
triggers = {
113123
redeployment = "${timestamp()}"
114124
}
115125

126+
# Minimize downtime by creating the new deployment before destroying the old one. And... because I don't think AWS would let me destroy the API given that it's in use by the load balancer.
116127
lifecycle {
117128
create_before_destroy = true
118129
}
119130
}
120131

132+
# An API Gateway stage is a logical reference to a lifecycle state of your API (for example, dev, test, prod). Stages are used to manage and deploy different versions of your API, allowing you to test changes in a development environment before promoting them to production.
121133
resource "aws_api_gateway_stage" "api_stage" {
122134
deployment_id = aws_api_gateway_deployment.api_deployment.id
123135
rest_api_id = aws_api_gateway_rest_api.api.id
@@ -132,11 +144,11 @@ resource "aws_api_gateway_stage" "api_stage" {
132144
resource "aws_api_gateway_method_settings" "api_method_settings" {
133145
rest_api_id = aws_api_gateway_rest_api.api.id
134146
stage_name = aws_api_gateway_stage.api_stage.stage_name
135-
method_path = "*/*"
147+
method_path = "*/*" # The path and method for which these settings apply. The format is HTTP_METHOD/RESOURCE_PATH. You can use */* to apply the settings to all methods and resources.
136148
settings {
137-
metrics_enabled = true
138-
logging_level = "INFO"
139-
data_trace_enabled = true
149+
metrics_enabled = true # Enable CloudWatch metrics for the method.
150+
logging_level = "INFO" # E.g., INFO, ERROR
151+
data_trace_enabled = true # Can generate a large volume of log data, especially for APIs with high traffic or large payloads.
140152
}
141153
}
142154

@@ -145,6 +157,10 @@ resource "aws_cloudwatch_log_group" "api_gateway_log_group" {
145157
retention_in_days = 7
146158
}
147159

160+
# IAM stuff should probably be in IAM module.
161+
162+
# It could be to have a root-level configuration to enable logging for the various modules.
163+
148164
resource "aws_iam_role" "api_gateway_cloudwatch_role" {
149165
name = "${var.environment}-interview-prep-api-gateway-cloudwatch-role"
150166
assume_role_policy = jsonencode({
@@ -185,16 +201,18 @@ resource "aws_iam_role_policy_attachment" "api_gateway_cloudwatch_policy_attachm
185201
role = aws_iam_role.api_gateway_cloudwatch_role.name
186202
}
187203

204+
# custom_domain_name and custom_domain_zone_id are output and used in the dns module.
188205
resource "aws_api_gateway_domain_name" "custom_domain" {
189206
domain_name = "api.dev.interviewprep.onyxdevtutorials.com"
190207

191208
endpoint_configuration {
192-
types = ["EDGE"]
209+
types = ["EDGE"] # The endpoint type (EDGE, REGIONAL, or PRIVATE)
193210
}
194211

195-
certificate_arn = var.certificate_arn
212+
certificate_arn = var.certificate_arn # The ARN of the SSL certificate to use for the custom domain.
196213
}
197214

215+
# Used to map the custom domain to the API Gateway stage.
198216
resource "aws_api_gateway_base_path_mapping" "custom_domain_mapping" {
199217
api_id = aws_api_gateway_rest_api.api.id
200218
stage_name = aws_api_gateway_stage.api_stage.stage_name

terraform/modules/bastion/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ resource "aws_instance" "bastion" {
33
instance_type = var.instance_type
44
subnet_id = var.public_subnet_id
55
vpc_security_group_ids = [var.bastion_sg_id]
6-
key_name = var.key_name
6+
key_name = var.key_name # SSH key pair name
77

88
tags = {
99
Name = "${var.environment}-interview-prep-bastion"

terraform/modules/iam/main.tf

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ resource "aws_iam_role_policy_attachment" "ecs_task_execution_role_policy" {
2525
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
2626
}
2727

28-
# resource "aws_iam_role_policy_attachment" "ecr_pull_policy" {
29-
# role = aws_iam_role.ecs_task_execution_role.name
30-
# policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"
31-
# }
32-
3328
# Role is assumed by the ECS tasks themselves. It allows the containers running within the tasks to interact with other AWS services. Common actions: reading from or writing to S3 buckets; accessing secrets from SSM Parameter Store; interacting with DynamoDB tables, etc. Here, because we don't attach any policies, the role does not grant any permissions to perform actions on AWS resources.
3429
resource "aws_iam_role" "ecs_task_role" {
3530
name = "${var.environment}-ecs-task-role"
@@ -71,6 +66,7 @@ resource "aws_iam_role" "lambda_exec" {
7166
}
7267
}
7368

69+
# The ec2 permissions are needed because the Lambda function is running in a VPC and needs to create and delete network interfaces.
7470
resource "aws_iam_policy" "lambda_exec_policy" {
7571
name = "${var.environment}-interview-prep-lambda-exec-policy"
7672
description = "Policy for Lambda execution role"
@@ -109,13 +105,6 @@ resource "aws_iam_policy" "lambda_exec_policy" {
109105
"arn:aws:ssm:${var.region}:${var.account_id}:parameter/interview-prep/${var.environment}/*"
110106
]
111107
},
112-
# {
113-
# Effect = "Allow",
114-
# Action = [
115-
# "kms:Decrypt"
116-
# ],
117-
# Resource = "arn:aws:kms:${var.region}:${var.account_id}:key/169ce983-7b59-4ff6-9c74-533af48cf478"
118-
# },
119108
]
120109
})
121110
}

terraform/modules/load_balancer/main.tf

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
resource "aws_lb" "this" {
22
name = "${var.environment}-interview-prep-lb"
3-
internal = false
4-
load_balancer_type = "application"
3+
internal = false # Set to false to create an internet-facing load balancer
4+
load_balancer_type = "application" # Other types include network and gateway
55
security_groups = var.security_groups
66
subnets = var.public_subnet_ids
77

8-
enable_deletion_protection = false
9-
enable_http2 = false
10-
enable_cross_zone_load_balancing = true
8+
enable_deletion_protection = false # Set to true to enable accidental deletion protection
9+
enable_http2 = false # Set to true to enable HTTP/2
10+
enable_cross_zone_load_balancing = true # Set to true to enable cross-zone load balancing. This distributes incoming requests evenly across all registered targets in all enabled Availability Zones.
1111

1212
tags = {
1313
Name = "${var.environment}-interview-prep-lb"
@@ -20,7 +20,7 @@ resource "aws_lb_target_group" "frontend" {
2020
port = 80
2121
protocol = "HTTP"
2222
vpc_id = var.vpc_id
23-
target_type = "ip"
23+
target_type = "ip" # Other types include instance and lambda
2424

2525
health_check {
2626
path = var.frontend_health_check_path
@@ -66,7 +66,7 @@ resource "aws_lb_listener" "http_frontend" {
6666

6767
default_action {
6868
type = "forward"
69-
target_group_arn = aws_lb_target_group.frontend.arn
69+
target_group_arn = aws_lb_target_group.frontend.arn # Refer to the ECS module to see how the target group ARN is passed to the ECS service.
7070
}
7171
}
7272

@@ -77,6 +77,6 @@ resource "aws_lb_listener" "http_backend" {
7777

7878
default_action {
7979
type = "forward"
80-
target_group_arn = aws_lb_target_group.backend.arn
80+
target_group_arn = aws_lb_target_group.backend.arn # Refer to the ECS module to see how the target group ARN is passed to the ECS service.
8181
}
8282
}

0 commit comments

Comments
 (0)