Skip to content

Commit 02cd229

Browse files
CCM-12875: Added toggle for mock in environments
1 parent e2e199b commit 02cd229

File tree

5 files changed

+83
-44
lines changed

5 files changed

+83
-44
lines changed

infrastructure/terraform/components/dl/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ No requirements.
1717
| <a name="input_component"></a> [component](#input\_component) | The variable encapsulating the name of this component | `string` | `"dl"` | no |
1818
| <a name="input_default_tags"></a> [default\_tags](#input\_default\_tags) | A map of default tags to apply to all taggable resources within the component | `map(string)` | `{}` | no |
1919
| <a name="input_enable_dynamodb_delete_protection"></a> [enable\_dynamodb\_delete\_protection](#input\_enable\_dynamodb\_delete\_protection) | Enable DynamoDB Delete Protection on all Tables | `bool` | `true` | no |
20+
| <a name="input_enable_pdm_mock"></a> [enable\_pdm\_mock](#input\_enable\_pdm\_mock) | Flag indicating whether to deploy PDM mock API (should be false in production environments) | `bool` | `true` | no |
2021
| <a name="input_environment"></a> [environment](#input\_environment) | The name of the tfscaffold environment | `string` | n/a | yes |
2122
| <a name="input_force_destroy"></a> [force\_destroy](#input\_force\_destroy) | Flag to force deletion of S3 buckets | `bool` | `false` | no |
2223
| <a name="input_force_lambda_code_deploy"></a> [force\_lambda\_code\_deploy](#input\_force\_lambda\_code\_deploy) | If the lambda package in s3 has the same commit id tag as the terraform build branch, the lambda will not update automatically. Set to True if making changes to Lambda code from on the same commit for example during development | `bool` | `false` | no |
@@ -57,8 +58,8 @@ No requirements.
5758
| Name | Description |
5859
|------|-------------|
5960
| <a name="output_deployment"></a> [deployment](#output\_deployment) | Deployment details used for post-deployment scripts |
60-
| <a name="output_pdm_mock_lambda_endpoint"></a> [pdm\_mock\_lambda\_endpoint](#output\_pdm\_mock\_lambda\_endpoint) | The base URL of the PDM Mock Lambda |
61-
| <a name="output_pdm_mock_lambda_id"></a> [pdm\_mock\_lambda\_id](#output\_pdm\_mock\_lambda\_id) | The ID of the PDM Mock Lambda API Gateway |
61+
| <a name="output_pdm_mock_lambda_endpoint"></a> [pdm\_mock\_lambda\_endpoint](#output\_pdm\_mock\_lambda\_endpoint) | The base URL of the PDM Mock Lambda (null when not deployed) |
62+
| <a name="output_pdm_mock_lambda_id"></a> [pdm\_mock\_lambda\_id](#output\_pdm\_mock\_lambda\_id) | The ID of the PDM Mock Lambda API Gateway (null when not deployed) |
6263
<!-- vale on -->
6364
<!-- markdownlint-enable -->
6465
<!-- END_TF_DOCS -->
Lines changed: 69 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
resource "aws_api_gateway_rest_api" "pdm_mock" {
2+
count = local.deploy_pdm_mock ? 1 : 0
3+
24
name = "${var.project}-${var.environment}-pdm-mock-lambda"
35
description = "PDM Mock API for testing integration with Patient Data Manager"
46

@@ -15,97 +17,119 @@ resource "aws_api_gateway_rest_api" "pdm_mock" {
1517
}
1618

1719
resource "aws_api_gateway_resource" "patient_data_manager" {
18-
rest_api_id = aws_api_gateway_rest_api.pdm_mock.id
19-
parent_id = aws_api_gateway_rest_api.pdm_mock.root_resource_id
20+
count = local.deploy_pdm_mock ? 1 : 0
21+
22+
rest_api_id = aws_api_gateway_rest_api.pdm_mock[0].id
23+
parent_id = aws_api_gateway_rest_api.pdm_mock[0].root_resource_id
2024
path_part = "patient-data-manager"
2125
}
2226

2327
resource "aws_api_gateway_resource" "fhir" {
24-
rest_api_id = aws_api_gateway_rest_api.pdm_mock.id
25-
parent_id = aws_api_gateway_resource.patient_data_manager.id
28+
count = local.deploy_pdm_mock ? 1 : 0
29+
30+
rest_api_id = aws_api_gateway_rest_api.pdm_mock[0].id
31+
parent_id = aws_api_gateway_resource.patient_data_manager[0].id
2632
path_part = "FHIR"
2733
}
2834

2935
resource "aws_api_gateway_resource" "r4" {
30-
rest_api_id = aws_api_gateway_rest_api.pdm_mock.id
31-
parent_id = aws_api_gateway_resource.fhir.id
36+
count = local.deploy_pdm_mock ? 1 : 0
37+
38+
rest_api_id = aws_api_gateway_rest_api.pdm_mock[0].id
39+
parent_id = aws_api_gateway_resource.fhir[0].id
3240
path_part = "R4"
3341
}
3442

3543
resource "aws_api_gateway_resource" "document_reference" {
36-
rest_api_id = aws_api_gateway_rest_api.pdm_mock.id
37-
parent_id = aws_api_gateway_resource.r4.id
44+
count = local.deploy_pdm_mock ? 1 : 0
45+
46+
rest_api_id = aws_api_gateway_rest_api.pdm_mock[0].id
47+
parent_id = aws_api_gateway_resource.r4[0].id
3848
path_part = "DocumentReference"
3949
}
4050

4151
resource "aws_api_gateway_resource" "document_reference_id" {
42-
rest_api_id = aws_api_gateway_rest_api.pdm_mock.id
43-
parent_id = aws_api_gateway_resource.document_reference.id
52+
count = local.deploy_pdm_mock ? 1 : 0
53+
54+
rest_api_id = aws_api_gateway_rest_api.pdm_mock[0].id
55+
parent_id = aws_api_gateway_resource.document_reference[0].id
4456
path_part = "{id}"
4557
}
4658

4759
resource "aws_api_gateway_method" "create_document_reference" {
48-
rest_api_id = aws_api_gateway_rest_api.pdm_mock.id
49-
resource_id = aws_api_gateway_resource.document_reference.id
60+
count = local.deploy_pdm_mock ? 1 : 0
61+
62+
rest_api_id = aws_api_gateway_rest_api.pdm_mock[0].id
63+
resource_id = aws_api_gateway_resource.document_reference[0].id
5064
http_method = "POST"
5165
authorization = "AWS_IAM"
5266
}
5367

5468
resource "aws_api_gateway_integration" "create_document_reference" {
55-
rest_api_id = aws_api_gateway_rest_api.pdm_mock.id
56-
resource_id = aws_api_gateway_resource.document_reference.id
57-
http_method = aws_api_gateway_method.create_document_reference.http_method
69+
count = local.deploy_pdm_mock ? 1 : 0
70+
71+
rest_api_id = aws_api_gateway_rest_api.pdm_mock[0].id
72+
resource_id = aws_api_gateway_resource.document_reference[0].id
73+
http_method = aws_api_gateway_method.create_document_reference[0].http_method
5874

5975
integration_http_method = "POST"
6076
type = "AWS_PROXY"
61-
uri = module.pdm_mock_lambda.function_invoke_arn
77+
uri = module.pdm_mock_lambda[0].function_invoke_arn
6278
}
6379

6480
resource "aws_api_gateway_method" "get_document_reference" {
65-
rest_api_id = aws_api_gateway_rest_api.pdm_mock.id
66-
resource_id = aws_api_gateway_resource.document_reference_id.id
81+
count = local.deploy_pdm_mock ? 1 : 0
82+
83+
rest_api_id = aws_api_gateway_rest_api.pdm_mock[0].id
84+
resource_id = aws_api_gateway_resource.document_reference_id[0].id
6785
http_method = "GET"
6886
authorization = "AWS_IAM"
6987
}
7088

7189
resource "aws_api_gateway_integration" "get_document_reference" {
72-
rest_api_id = aws_api_gateway_rest_api.pdm_mock.id
73-
resource_id = aws_api_gateway_resource.document_reference_id.id
74-
http_method = aws_api_gateway_method.get_document_reference.http_method
90+
count = local.deploy_pdm_mock ? 1 : 0
91+
92+
rest_api_id = aws_api_gateway_rest_api.pdm_mock[0].id
93+
resource_id = aws_api_gateway_resource.document_reference_id[0].id
94+
http_method = aws_api_gateway_method.get_document_reference[0].http_method
7595

7696
integration_http_method = "POST"
7797
type = "AWS_PROXY"
78-
uri = module.pdm_mock_lambda.function_invoke_arn
98+
uri = module.pdm_mock_lambda[0].function_invoke_arn
7999
}
80100

81101
resource "aws_lambda_permission" "pdm_mock_lambda_gateway" {
102+
count = local.deploy_pdm_mock ? 1 : 0
103+
82104
statement_id = "AllowAPIGatewayInvoke"
83105
action = "lambda:InvokeFunction"
84-
function_name = module.pdm_mock_lambda.function_name
106+
function_name = module.pdm_mock_lambda[0].function_name
85107
principal = "apigateway.amazonaws.com"
86108

87-
source_arn = "${aws_api_gateway_rest_api.pdm_mock.execution_arn}/*/*"
109+
source_arn = "${aws_api_gateway_rest_api.pdm_mock[0].execution_arn}/*/*"
88110
}
89111

90112
resource "aws_api_gateway_deployment" "pdm_mock" {
113+
count = local.deploy_pdm_mock ? 1 : 0
114+
91115
depends_on = [
92116
aws_api_gateway_integration.create_document_reference,
93117
aws_api_gateway_integration.get_document_reference,
94118
]
95119

96-
rest_api_id = aws_api_gateway_rest_api.pdm_mock.id
120+
rest_api_id = aws_api_gateway_rest_api.pdm_mock[0].id
97121

98122
triggers = {
99123
redeployment = sha1(jsonencode([
100-
aws_api_gateway_resource.patient_data_manager.id,
101-
aws_api_gateway_resource.fhir.id,
102-
aws_api_gateway_resource.r4.id,
103-
aws_api_gateway_resource.document_reference.id,
104-
aws_api_gateway_resource.document_reference_id.id,
105-
aws_api_gateway_method.create_document_reference.id,
106-
aws_api_gateway_method.get_document_reference.id,
107-
aws_api_gateway_integration.create_document_reference.id,
108-
aws_api_gateway_integration.get_document_reference.id,
124+
aws_api_gateway_resource.patient_data_manager[0].id,
125+
aws_api_gateway_resource.fhir[0].id,
126+
aws_api_gateway_resource.r4[0].id,
127+
aws_api_gateway_resource.document_reference[0].id,
128+
aws_api_gateway_resource.document_reference_id[0].id,
129+
aws_api_gateway_method.create_document_reference[0].id,
130+
aws_api_gateway_method.get_document_reference[0].id,
131+
aws_api_gateway_integration.create_document_reference[0].id,
132+
aws_api_gateway_integration.get_document_reference[0].id,
109133
]))
110134
}
111135

@@ -114,14 +138,16 @@ resource "aws_api_gateway_deployment" "pdm_mock" {
114138
}
115139
}
116140
resource "aws_api_gateway_stage" "pdm_mock" {
117-
deployment_id = aws_api_gateway_deployment.pdm_mock.id
118-
rest_api_id = aws_api_gateway_rest_api.pdm_mock.id
141+
count = local.deploy_pdm_mock ? 1 : 0
142+
143+
deployment_id = aws_api_gateway_deployment.pdm_mock[0].id
144+
rest_api_id = aws_api_gateway_rest_api.pdm_mock[0].id
119145
stage_name = var.environment
120146

121147
xray_tracing_enabled = true
122148

123149
access_log_settings {
124-
destination_arn = aws_cloudwatch_log_group.pdm_mock_lambda_gateway.arn
150+
destination_arn = aws_cloudwatch_log_group.pdm_mock_lambda_gateway[0].arn
125151
format = jsonencode({
126152
requestId = "$context.requestId"
127153
ip = "$context.identity.sourceIp"
@@ -145,6 +171,8 @@ resource "aws_api_gateway_stage" "pdm_mock" {
145171
}
146172

147173
resource "aws_cloudwatch_log_group" "pdm_mock_lambda_gateway" {
174+
count = local.deploy_pdm_mock ? 1 : 0
175+
148176
name = "/aws/apigateway/${var.project}-${var.environment}-pdm-mock-lambda"
149177
retention_in_days = var.log_retention_in_days
150178
kms_key_id = module.kms.key_arn
@@ -158,11 +186,11 @@ resource "aws_cloudwatch_log_group" "pdm_mock_lambda_gateway" {
158186
}
159187

160188
output "pdm_mock_lambda_endpoint" {
161-
description = "The base URL of the PDM Mock Lambda"
162-
value = aws_api_gateway_stage.pdm_mock.invoke_url
189+
description = "The base URL of the PDM Mock Lambda (null when not deployed)"
190+
value = local.deploy_pdm_mock ? aws_api_gateway_stage.pdm_mock[0].invoke_url : null
163191
}
164192

165193
output "pdm_mock_lambda_id" {
166-
description = "The ID of the PDM Mock Lambda API Gateway"
167-
value = aws_api_gateway_rest_api.pdm_mock.id
194+
description = "The ID of the PDM Mock Lambda API Gateway (null when not deployed)"
195+
value = local.deploy_pdm_mock ? aws_api_gateway_rest_api.pdm_mock[0].id : null
168196
}

infrastructure/terraform/components/dl/locals.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ locals {
88
root_domain_name = "${var.environment}.${local.acct.route53_zone_names["digital-letters"]}"
99
root_domain_id = local.acct.route53_zone_ids["digital-letters"]
1010
ttl_shard_count = 3
11+
deploy_pdm_mock = var.enable_pdm_mock
1112
}

infrastructure/terraform/components/dl/module_lambda_pdm_mock_api.tf

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module "pdm_mock_lambda" {
2+
count = local.deploy_pdm_mock ? 1 : 0
23
source = "https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-lambda.zip"
34

45
function_name = "pdm-mock-lambda"
@@ -15,7 +16,7 @@ module "pdm_mock_lambda" {
1516
kms_key_arn = module.kms.key_arn
1617

1718
iam_policy_document = {
18-
body = data.aws_iam_policy_document.pdm_mock_lambda.json
19+
body = data.aws_iam_policy_document.pdm_mock_lambda[0].json
1920
}
2021

2122
function_s3_bucket = local.acct.s3_buckets["lambda_function_artefacts"]["id"]
@@ -43,6 +44,8 @@ module "pdm_mock_lambda" {
4344
}
4445

4546
data "aws_iam_policy_document" "pdm_mock_lambda" {
47+
count = local.deploy_pdm_mock ? 1 : 0
48+
4649
statement {
4750
sid = "KMSPermissions"
4851
effect = "Allow"

infrastructure/terraform/components/dl/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,9 @@ variable "force_destroy" {
157157
description = "Flag to force deletion of S3 buckets"
158158
default = false
159159
}
160+
161+
variable "enable_pdm_mock" {
162+
type = bool
163+
description = "Flag indicating whether to deploy PDM mock API (should be false in production environments)"
164+
default = true
165+
}

0 commit comments

Comments
 (0)