-
Notifications
You must be signed in to change notification settings - Fork 4
VED-1235 Lambda-to-mock-PDS-in-Ref #1427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
2158076
1f5dfc5
d7421df
28cac67
98dc17b
d7692be
ec65ea0
194e8e8
e18d510
611e304
87d68b3
989d10f
d4a04fa
813a0e0
d5d0318
1fd7c4c
5f97f5f
b74a32c
bdf2fe4
aae6283
ba7d55b
0dff83c
708197e
3aa57e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,7 @@ on: | |
| lambda_build_flags: | ||
| description: > | ||
| JSON map of lambda_name -> force-build flag. | ||
| e.g. {"backend":true,"recordprocessor":true,"ack-backend":false} | ||
| e.g. {"backend":true,"recordprocessor":true,"ack-backend":false,"mock_pds":false} | ||
| required: false | ||
| type: string | ||
| default: "{}" | ||
|
|
@@ -73,7 +73,7 @@ on: | |
| lambda_build_flags: | ||
| description: > | ||
| JSON map of lambda_name -> force-build flag. | ||
| e.g. {"backend":true,"recordprocessor":true,"ack-backend":false} | ||
| e.g. {"backend":true,"recordprocessor":true,"ack-backend":false,"mock_pds":false} | ||
| required: false | ||
| type: string | ||
| default: "{}" | ||
|
|
@@ -151,6 +151,9 @@ jobs: | |
| - lambda_name: ack-backend | ||
| ecr_repository: imms-ackbackend-repo | ||
| lambda_dir: ack_backend | ||
| - lambda_name: mock_pds | ||
| ecr_repository: imms-mock-pds-repo | ||
| lambda_dir: mock_pds | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does this mean the mock_pds lambda will be created in all envs (including ref)? Dont we only need it for ref?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, it won’t be created in all environments. mock_pds is in the deploy list, but Terraform only creates the Lambda when mock_pds_enabled=true. |
||
| uses: ./.github/workflows/deploy-lambda-artifact.yml | ||
| with: | ||
| lambda_name: ${{ matrix.lambda_name }} | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| resource "aws_ecr_repository" "mock_pds_repository" { | ||
| image_scanning_configuration { | ||
| scan_on_push = true | ||
| } | ||
| image_tag_mutability = "IMMUTABLE" | ||
| name = "imms-mock-pds-repo" | ||
| } | ||
| # Module for building and pushing Docker image to ECR | ||
| resource "aws_ecr_repository_policy" "mock_pds_repository_lambda_image_retrieval_policy" { | ||
| repository = aws_ecr_repository.mock_pds_repository.name | ||
|
|
||
| policy = jsonencode({ | ||
| Version = "2012-10-17" | ||
| Statement = [ | ||
| { | ||
| Sid = "LambdaECRImageRetrievalPolicy" | ||
| Effect = "Allow" | ||
| Principal = { | ||
| Service = "lambda.amazonaws.com" | ||
| } | ||
| Action = [ | ||
| "ecr:BatchGetImage", | ||
| "ecr:GetDownloadUrlForLayer" | ||
| ] | ||
| Condition = { | ||
| StringLike = { | ||
| "aws:sourceArn" = "arn:aws:lambda:${var.aws_region}:${var.imms_account_id}:function:imms-*-mock-pds-lambda" | ||
| } | ||
| } | ||
| } | ||
| ] | ||
| }) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| locals { | ||
| mock_pds_lambda_name = "${local.short_prefix}-mock-pds-lambda" | ||
| mock_pds_base_url = var.mock_pds_enabled ? "${aws_lambda_function_url.mock_pds_lambda_url[0].function_url}Patient" : "" | ||
| } | ||
|
|
||
| resource "aws_iam_role" "mock_pds_lambda_exec_role" { | ||
| count = var.mock_pds_enabled ? 1 : 0 | ||
|
|
||
| name = "${local.mock_pds_lambda_name}-exec-role" | ||
| assume_role_policy = jsonencode({ | ||
| Version = "2012-10-17", | ||
| Statement = [{ | ||
| Effect = "Allow", | ||
| Sid = "", | ||
| Principal = { | ||
| Service = "lambda.amazonaws.com" | ||
| }, | ||
| Action = "sts:AssumeRole" | ||
| }] | ||
| }) | ||
| } | ||
|
|
||
| resource "aws_iam_policy" "mock_pds_lambda_exec_policy" { | ||
| count = var.mock_pds_enabled ? 1 : 0 | ||
|
|
||
| name = "${local.mock_pds_lambda_name}-exec-policy" | ||
| policy = jsonencode({ | ||
| Version = "2012-10-17", | ||
| Statement = [ | ||
| { | ||
| Effect = "Allow" | ||
| Action = [ | ||
| "logs:CreateLogGroup", | ||
| "logs:CreateLogStream", | ||
| "logs:PutLogEvents" | ||
| ] | ||
| Resource = "arn:aws:logs:${var.aws_region}:${var.immunisation_account_id}:log-group:/aws/lambda/${local.mock_pds_lambda_name}:*" | ||
| }, | ||
| { | ||
| Effect = "Allow", | ||
| Action = [ | ||
| "ec2:CreateNetworkInterface", | ||
| "ec2:DescribeNetworkInterfaces", | ||
| "ec2:DeleteNetworkInterface" | ||
| ], | ||
| Resource = "*" | ||
| } | ||
| ] | ||
| }) | ||
| } | ||
|
|
||
| resource "aws_iam_policy" "mock_pds_lambda_kms_access_policy" { | ||
| count = var.mock_pds_enabled ? 1 : 0 | ||
|
|
||
| name = "${local.mock_pds_lambda_name}-kms-policy" | ||
| description = "Allow mock PDS Lambda to decrypt environment variables" | ||
|
|
||
| policy = jsonencode({ | ||
| Version = "2012-10-17" | ||
| Statement = [ | ||
| { | ||
| Effect = "Allow" | ||
| Action = [ | ||
| "kms:Decrypt" | ||
| ] | ||
| Resource = data.aws_kms_key.existing_lambda_encryption_key.arn | ||
| } | ||
| ] | ||
| }) | ||
| } | ||
|
|
||
| resource "aws_iam_role_policy_attachment" "mock_pds_lambda_exec_policy_attachment" { | ||
| count = var.mock_pds_enabled ? 1 : 0 | ||
|
|
||
| role = aws_iam_role.mock_pds_lambda_exec_role[0].name | ||
| policy_arn = aws_iam_policy.mock_pds_lambda_exec_policy[0].arn | ||
| } | ||
|
|
||
| resource "aws_iam_role_policy_attachment" "mock_pds_lambda_kms_policy_attachment" { | ||
| count = var.mock_pds_enabled ? 1 : 0 | ||
|
|
||
| role = aws_iam_role.mock_pds_lambda_exec_role[0].name | ||
| policy_arn = aws_iam_policy.mock_pds_lambda_kms_access_policy[0].arn | ||
| } | ||
|
|
||
| resource "aws_cloudwatch_log_group" "mock_pds_lambda_log_group" { | ||
| count = var.mock_pds_enabled ? 1 : 0 | ||
|
|
||
| name = "/aws/lambda/${local.mock_pds_lambda_name}" | ||
| retention_in_days = 30 | ||
| } | ||
|
|
||
| resource "aws_lambda_function" "mock_pds_lambda" { | ||
| count = var.mock_pds_enabled ? 1 : 0 | ||
|
|
||
| function_name = local.mock_pds_lambda_name | ||
| role = aws_iam_role.mock_pds_lambda_exec_role[0].arn | ||
| package_type = "Image" | ||
| image_uri = var.mock_pds_image_uri | ||
| architectures = ["x86_64"] | ||
| timeout = 30 | ||
|
|
||
| vpc_config { | ||
| subnet_ids = local.private_subnet_ids | ||
| security_group_ids = [data.aws_security_group.existing_securitygroup.id] | ||
| } | ||
|
|
||
| environment { | ||
| variables = { | ||
| REDIS_HOST = data.aws_elasticache_cluster.existing_redis.cache_nodes[0].address | ||
| REDIS_PORT = tostring(data.aws_elasticache_cluster.existing_redis.port) | ||
| MOCK_PDS_AVERAGE_LIMIT = tostring(var.mock_pds_average_rate_limit) | ||
| MOCK_PDS_AVERAGE_WINDOW_SECONDS = tostring(var.mock_pds_average_window_seconds) | ||
| MOCK_PDS_SPIKE_LIMIT = tostring(var.mock_pds_spike_rate_limit) | ||
| MOCK_PDS_SPIKE_WINDOW_SECONDS = tostring(var.mock_pds_spike_window_seconds) | ||
| MOCK_PDS_GP_ODS_CODE = var.mock_pds_gp_ods_code | ||
| } | ||
| } | ||
|
|
||
| kms_key_arn = data.aws_kms_key.existing_lambda_encryption_key.arn | ||
|
|
||
| depends_on = [ | ||
| aws_cloudwatch_log_group.mock_pds_lambda_log_group, | ||
| aws_iam_policy.mock_pds_lambda_exec_policy | ||
| ] | ||
| } | ||
|
|
||
| resource "aws_lambda_function_url" "mock_pds_lambda_url" { | ||
| count = var.mock_pds_enabled ? 1 : 0 | ||
|
|
||
| function_name = aws_lambda_function.mock_pds_lambda[0].function_name | ||
| authorization_type = "NONE" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are we doing authorization_type = NONE?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, authorization_type = "NONE" makes the Function URL public. We used this to keep the mock PDS endpoint simple for lower-env integration/perf testing. It does not expose real PDS data, but you’re right that it is internet-reachable. |
||
| } | ||
|
|
||
| resource "aws_lambda_permission" "mock_pds_lambda_url_invoke" { | ||
| count = var.mock_pds_enabled ? 1 : 0 | ||
|
|
||
| statement_id = "AllowPublicInvokeFunctionUrl" | ||
| action = "lambda:InvokeFunctionUrl" | ||
| function_name = aws_lambda_function.mock_pds_lambda[0].function_name | ||
| principal = "*" | ||
| function_url_auth_type = "NONE" | ||
| } | ||
|
|
||
| resource "aws_cloudwatch_log_metric_filter" "mock_pds_throttle_logs" { | ||
| count = var.mock_pds_enabled ? 1 : 0 | ||
|
|
||
| name = "${local.short_prefix}-MockPdsThrottleLogs" | ||
| pattern = "Mock PDS rate limit exceeded" | ||
| log_group_name = aws_cloudwatch_log_group.mock_pds_lambda_log_group[0].name | ||
|
|
||
| metric_transformation { | ||
| name = "${local.short_prefix}-MockPdsThrottleRequests" | ||
| namespace = "${local.short_prefix}-MockPds" | ||
| value = "1" | ||
| } | ||
| } | ||
|
|
||
| output "mock_pds_function_url" { | ||
| value = var.mock_pds_enabled ? aws_lambda_function_url.mock_pds_lambda_url[0].function_url : null | ||
| description = "Function URL for the mock PDS endpoint." | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.