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