Skip to content

Commit c7e9df2

Browse files
committed
Try a different approach
1 parent cab25a4 commit c7e9df2

4 files changed

Lines changed: 18 additions & 14 deletions

File tree

infrastructure/terraform/components/api/api_gateway_rest_api.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@ resource "aws_api_gateway_rest_api" "main" {
33
body = local.openapi_spec
44
description = "Suppliers API"
55
disable_execute_api_endpoint = var.disable_gateway_execute_endpoint
6+
7+
lifecycle {
8+
replace_triggered_by = [terraform_data.rest_api_security_policy]
9+
}
610
}
Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
# Terraform does not yet support setting the securityPolicy on aws_api_gateway_rest_api
2-
# directly. This terraform_data resource works around that by calling the AWS CLI
3-
# to enforce TLS 1.2 on the REST API after it is created or replaced.
4-
resource "terraform_data" "rest_api_tls_policy" {
5-
triggers_replace = [aws_api_gateway_rest_api.main.id]
1+
# AWS does not support updating the securityPolicy on an existing REST API from
2+
# TLS_1_0 to TLS_1_2 in-place. The policy must be set at creation time via the
3+
# x-amazon-apigateway-security-policy OpenAPI extension in the API body
4+
# (see spec.tmpl.json). This terraform_data resource forces recreation of the
5+
# aws_api_gateway_rest_api whenever the desired security policy changes, ensuring
6+
# the new API is always created with the correct TLS version.
7+
locals {
8+
rest_api_security_policy = "TLS_1_2"
9+
}
610

7-
provisioner "local-exec" {
8-
command = <<-EOT
9-
aws apigateway update-rest-api \
10-
--region ${var.region} \
11-
--rest-api-id ${aws_api_gateway_rest_api.main.id} \
12-
--patch-operations op=replace,path=/securityPolicy,value=TLS_1_2
13-
EOT
14-
}
11+
resource "terraform_data" "rest_api_security_policy" {
12+
input = local.rest_api_security_policy
1513
}

infrastructure/terraform/components/api/locals.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ locals {
77
openapi_spec = templatefile("${path.module}/resources/spec.tmpl.json", {
88
APIG_EXECUTION_ROLE_ARN = aws_iam_role.api_gateway_execution_role.arn
99
AWS_REGION = var.region
10+
TLS_VERSION = local.rest_api_security_policy
1011
AUTHORIZER_LAMBDA_ARN = module.authorizer_lambda.function_arn
1112
GET_LETTER_LAMBDA_ARN = module.get_letter.function_arn
1213
GET_LETTERS_LAMBDA_ARN = module.get_letters.function_arn

infrastructure/terraform/components/api/resources/spec.tmpl.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,5 +307,6 @@
307307
}
308308
}
309309
}
310-
}
310+
},
311+
"x-amazon-apigateway-security-policy": "${SECURITY_POLICY}"
311312
}

0 commit comments

Comments
 (0)