1+ # New API Gateway for mTLS
2+ resource "aws_api_gateway_rest_api" "ndr_doc_store_api_mtls" {
3+ name = " ${ terraform . workspace } _DocStoreApiMtls"
4+ description = " Document store API with mTLS enabled"
5+ disable_execute_api_endpoint = true
6+
7+ endpoint_configuration {
8+ types = [" REGIONAL" ]
9+ }
10+
11+ tags = {
12+ Name = " ${ terraform . workspace } _DocStoreApiMtls"
13+ }
14+ }
15+
16+ resource "aws_api_gateway_domain_name" "custom_api_domain_mtls" {
17+ domain_name = local. mtls_api_gateway_full_domain_name
18+ regional_certificate_arn = aws_acm_certificate_validation. mtls_api_gateway_cert . certificate_arn
19+ security_policy = " TLS_1_2"
20+
21+ endpoint_configuration {
22+ types = [" REGIONAL" ]
23+ }
24+
25+ mutual_tls_authentication {
26+ truststore_uri = local. truststore_uri
27+ truststore_version = data. aws_s3_object . truststore_ext_cert . version_id
28+ }
29+ }
30+
31+ resource "aws_api_gateway_base_path_mapping" "api_mapping_mtls" {
32+ api_id = aws_api_gateway_rest_api. ndr_doc_store_api_mtls . id
33+ stage_name = var. environment
34+ domain_name = aws_api_gateway_domain_name. custom_api_domain_mtls . domain_name
35+
36+ depends_on = [aws_api_gateway_deployment . ndr_api_deploy_mtls ]
37+ }
38+
39+ resource "aws_api_gateway_deployment" "ndr_api_deploy_mtls" {
40+ rest_api_id = aws_api_gateway_rest_api. ndr_doc_store_api_mtls . id
41+
42+ depends_on = [
43+ aws_api_gateway_rest_api . ndr_doc_store_api_mtls ,
44+ aws_api_gateway_resource . get_document_reference_mtls ,
45+ module . get-doc-fhir-lambda ,
46+ aws_api_gateway_integration . get_doc_fhir_lambda_integration ,
47+ aws_lambda_permission . lambda_permission_get_mtls_api ,
48+ module . post-document-references-fhir-lambda ,
49+ aws_api_gateway_integration . post_doc_fhir_lambda_integration ,
50+ aws_lambda_permission . lambda_permission_post_mtls_api ,
51+ module . search-document-references-fhir-lambda ,
52+ aws_api_gateway_integration . search_doc_fhir_lambda_integration ,
53+ aws_lambda_permission . lambda_permission_search_mtls_api ,
54+ ]
55+
56+ lifecycle {
57+ create_before_destroy = true
58+ }
59+
60+ variables = {
61+ deployed_at = timestamp ()
62+ }
63+ }
64+
65+ resource "aws_api_gateway_stage" "ndr_api_mtls" {
66+ deployment_id = aws_api_gateway_deployment. ndr_api_deploy_mtls . id
67+ rest_api_id = aws_api_gateway_rest_api. ndr_doc_store_api_mtls . id
68+ stage_name = var. environment
69+ xray_tracing_enabled = var. enable_xray_tracing
70+ }
71+
72+ resource "aws_cloudwatch_log_group" "mtls_api_gateway_stage" {
73+ name = " API-Gateway-Execution-Logs_${ aws_api_gateway_rest_api . ndr_doc_store_api_mtls . id } /${ var . environment } "
74+ retention_in_days = 0
75+ depends_on = [
76+ aws_api_gateway_account . logging
77+ ]
78+ }
79+
80+ resource "aws_api_gateway_method_settings" "mtls_api_gateway_stage" {
81+ rest_api_id = aws_api_gateway_rest_api. ndr_doc_store_api_mtls . id
82+ stage_name = aws_api_gateway_stage. ndr_api_mtls . stage_name
83+ method_path = " */*"
84+
85+ settings {
86+ logging_level = " INFO"
87+ metrics_enabled = true
88+ data_trace_enabled = true
89+ }
90+ }
91+
92+ resource "aws_api_gateway_gateway_response" "unauthorised_response_mtls" {
93+ rest_api_id = aws_api_gateway_rest_api. ndr_doc_store_api_mtls . id
94+ response_type = " DEFAULT_4XX"
95+
96+ response_templates = {
97+ " application/json" = " {\" message\" :$context.error.messageString}"
98+ }
99+
100+ response_parameters = {
101+ " gatewayresponse.header.Access-Control-Allow-Origin" = contains ([" prod" ], terraform. workspace ) ? " 'https://${ var . domain } '" : " 'https://${ terraform . workspace } .${ var . domain } '"
102+ " gatewayresponse.header.Access-Control-Allow-Methods" = " '*'"
103+ " gatewayresponse.header.Access-Control-Allow-Headers" = " 'Content-Type,X-Amz-Date,Authorization,X-Auth,X-Api-Key,X-Amz-Security-Token,X-Auth-Cookie,Accept'"
104+ " gatewayresponse.header.Access-Control-Allow-Credentials" = " 'true'"
105+ }
106+ }
107+
108+ resource "aws_api_gateway_gateway_response" "bad_gateway_response_mtls" {
109+ rest_api_id = aws_api_gateway_rest_api. ndr_doc_store_api_mtls . id
110+ response_type = " DEFAULT_5XX"
111+
112+ response_templates = {
113+ " application/json" = " {\" message\" :$context.error.messageString}"
114+ }
115+
116+ response_parameters = {
117+ " gatewayresponse.header.Access-Control-Allow-Origin" = contains ([" prod" ], terraform. workspace ) ? " 'https://${ var . domain } '" : " 'https://${ terraform . workspace } .${ var . domain } '"
118+ " gatewayresponse.header.Access-Control-Allow-Methods" = " '*'"
119+ " gatewayresponse.header.Access-Control-Allow-Headers" = " 'Content-Type,X-Amz-Date,Authorization,X-Auth,X-Api-Key,X-Amz-Security-Token,X-Auth-Cookie,Accept'"
120+ " gatewayresponse.header.Access-Control-Allow-Credentials" = " 'true'"
121+ }
122+ }
123+
124+ module "mtls_api_endpoint_url_ssm_parameter" {
125+ source = " ./modules/ssm_parameter"
126+ name = " ${ terraform . workspace } _ApiEndpointMtls"
127+ description = " mTLS api endpoint URL for ${ var . environment } "
128+ resource_depends_on = aws_api_gateway_deployment. ndr_api_deploy_mtls
129+ value = " https://${ aws_api_gateway_base_path_mapping . api_mapping_mtls . domain_name } "
130+ type = " SecureString"
131+ owner = var. owner
132+ environment = var. environment
133+ }
0 commit comments