Skip to content

Commit 15084e1

Browse files
authored
feat: add required API key to gateway (#4)
Also add Terraform security scan with Checkov
1 parent 91c4495 commit 15084e1

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

.github/workflows/terraform.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Terraform security
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
paths:
8+
- "infra/**"
9+
- ".github/workflows/terraform.yml"
10+
pull_request:
11+
paths:
12+
- "infra/**"
13+
- ".github/workflows/terraform.yml"
14+
15+
jobs:
16+
terraform-security:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@v2
21+
22+
- name: Checkov security scan
23+
id: checkov
24+
uses: bridgecrewio/checkov-action@v12.641.0
25+
with:
26+
directory: infra
27+
framework: terraform
28+
output_format: cli
29+
download_external_modules: true

infra/modules/api-gateway/api-gateway.tf

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ resource "aws_api_gateway_rest_api" "api_gateway" {
1414
}
1515

1616
resource "aws_api_gateway_deployment" "api_deployment" {
17-
rest_api_id = aws_api_gateway_rest_api.api_gateway.id
17+
rest_api_id = aws_api_gateway_rest_api.api_gateway.id
18+
stage_description = md5(file("api-gateway.tf")) # Force a new deployment when this file changes
1819

1920
lifecycle {
2021
create_before_destroy = true
@@ -48,10 +49,11 @@ resource "aws_api_gateway_resource" "api_gateway_resource" {
4849
}
4950

5051
resource "aws_api_gateway_method" "api_gateway_proxy_method" {
51-
rest_api_id = aws_api_gateway_rest_api.api_gateway.id
52-
resource_id = aws_api_gateway_resource.api_gateway_resource.id
53-
http_method = "ANY"
54-
authorization = "NONE"
52+
rest_api_id = aws_api_gateway_rest_api.api_gateway.id
53+
resource_id = aws_api_gateway_resource.api_gateway_resource.id
54+
http_method = "ANY"
55+
authorization = "NONE"
56+
api_key_required = true
5557

5658
request_parameters = {
5759
"method.request.path.proxy" = true
@@ -80,3 +82,25 @@ resource "aws_api_gateway_integration" "api_proxy_integration" {
8082
type = "AWS_PROXY"
8183
uri = aws_lambda_function.api_lambda.invoke_arn
8284
}
85+
86+
#
87+
# API gateway usage plan and key
88+
#
89+
resource "aws_api_gateway_usage_plan" "api_gateway_usage_plan" {
90+
name = "FastAPIUsagePlan"
91+
92+
api_stages {
93+
api_id = aws_api_gateway_rest_api.api_gateway.id
94+
stage = aws_api_gateway_stage.api_stage.stage_name
95+
}
96+
}
97+
98+
resource "aws_api_gateway_api_key" "api_key" {
99+
name = "FastAPI"
100+
}
101+
102+
resource "aws_api_gateway_usage_plan_key" "api_gateway_usage_plan_key" {
103+
key_id = aws_api_gateway_api_key.api_key.id
104+
key_type = "API_KEY"
105+
usage_plan_id = aws_api_gateway_usage_plan.api_gateway_usage_plan.id
106+
}

0 commit comments

Comments
 (0)