Skip to content

Commit 3297769

Browse files
committed
Define the infrastucture. Dummy lambda that runs frequently
1 parent 6d53ab1 commit 3297769

22 files changed

Lines changed: 460 additions & 44 deletions

.pre-commit-config.yaml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ repos:
8686
files: ^packages\/checkPrescriptionStatusUpdates
8787
types_or: [ts, tsx, javascript, jsx, json]
8888
pass_filenames: false
89-
89+
9090
- id: lint-nhsNotifyLambda
9191
name: Lint nhsNotifyLambda
9292
entry: npm
@@ -97,6 +97,16 @@ repos:
9797
types_or: [ts, tsx, javascript, jsx, json]
9898
pass_filenames: false
9999

100+
- id: lint-postDatedLambda
101+
name: List Post-dated handling lambda
102+
entry: npm
103+
args:
104+
["run", "--prefix=packages/postDatedLambda", "lint"]
105+
language: system
106+
files: ^packages\/postDatedLambda
107+
types_or: [ts, tsx, javascript, jsx, json]
108+
pass_filenames: false
109+
100110
- id: lint-commonTesting
101111
name: Lint common/testing
102112
entry: npm

.vscode/eps-prescription-status-update-api.code-workspace

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
"name": "packages/nhsNotifyLambda",
3333
"path": "../packages/nhsNotifyLambda"
3434
},
35+
{
36+
"name": "packages/postDatedLambda",
37+
"path": "../packages/postDatedLambda"
38+
},
3539
{
3640
"name": "packages/nhsNotifyUpdateCallback",
3741
"path": "../packages/nhsNotifyUpdateCallback"

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ sam-list-resources: guard-AWS_DEFAULT_PROFILE guard-stack_name
5656
sam-list-outputs: guard-AWS_DEFAULT_PROFILE guard-stack_name
5757
sam list stack-outputs --stack-name $$stack_name
5858

59-
sam-validate:
59+
sam-validate:
6060
sam validate --template-file SAMtemplates/main_template.yaml --region eu-west-2
6161
sam validate --template-file SAMtemplates/apis/main.yaml --region eu-west-2
6262
sam validate --template-file SAMtemplates/apis/api_resources.yaml --region eu-west-2
@@ -120,6 +120,7 @@ lint-node: compile-node
120120
npm run lint --workspace packages/cpsuLambda
121121
npm run lint --workspace packages/checkPrescriptionStatusUpdates
122122
npm run lint --workspace packages/nhsNotifyLambda
123+
npm run lint --workspace packages/postDatedLambda
123124
npm run lint --workspace packages/nhsNotifyUpdateCallback
124125
npm run lint --workspace packages/common/testing
125126
npm run lint --workspace packages/common/middyErrorHandler
@@ -152,6 +153,7 @@ test: compile
152153
npm run test --workspace packages/cpsuLambda
153154
npm run test --workspace packages/checkPrescriptionStatusUpdates
154155
npm run test --workspace packages/nhsNotifyLambda
156+
npm run test --workspace packages/postDatedLambda
155157
npm run test --workspace packages/nhsNotifyUpdateCallback
156158
npm run test --workspace packages/common/middyErrorHandler
157159
npm run test --workspace packages/psuRestoreValidationLambda
@@ -172,6 +174,8 @@ clean:
172174
rm -rf packages/cpsuLambda/lib
173175
rm -rf packages/nhsNotifyLambda/coverage
174176
rm -rf packages/nhsNotifyLambda/lib
177+
rm -rf packages/postDatedLambda/coverage
178+
rm -rf packages/postDatedLambda/lib
175179
rm -rf packages/nhsNotifyUpdateCallback/coverage
176180
rm -rf packages/nhsNotifyUpdateCallback/lib
177181
rm -rf packages/checkPrescriptionStatusUpdates/lib

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# EPS Prescription Status Update API
22

3-
![Build](https://github.com/NHSDigital/eps-prescription-status-update-api/actions/workflows/ci.yml/badge.svg?branch=main)
3+
![Build](https://github.com/NHSDigital/eps-prescription-status-update-api/actions/workflows/ci.yml/badge.svg?branch=main)
44
![Release](https://github.com/NHSDigital/eps-prescription-status-update-api/actions/workflows/release.yml/badge.svg?branch=main)
55

66
## Versions and deployments
77

8-
Version release history can be found ot https://github.com/NHSDigital/eps-prescription-status-update-api/releases.
9-
We use eslint convention for commit messages for commits to main branch. Descriptions for the types of changes in a release can be found in the [contributing guidelines](./CONTRIBUTING.md)
8+
Version release history can be found ot https://github.com/NHSDigital/eps-prescription-status-update-api/releases.
9+
We use eslint convention for commit messages for commits to main branch. Descriptions for the types of changes in a release can be found in the [contributing guidelines](./CONTRIBUTING.md)
1010
Deployment history can be found at https://nhsdigital.github.io/eps-prescription-status-update-api/
1111

1212
## Introduction
@@ -20,6 +20,7 @@ This is the AWS layer that provides an API for EPS Prescription Status Update.
2020
- `packages/capabilityStatement` Returns a static capability statement.
2121
- `packages/cpsuLambda` Handles updating prescription status using a custom format.
2222
- `packages/nhsNotifyLambda` Handles sending prescription notifications to the NHS Notify service.
23+
- `packages/postDatedLambda` Handles business logic for post-dated prescriptions getting notifications \[deprecated\].
2324
- `packages/nhsNotifyUpdateCallback` Handles receiving notification updates from the NHS Notify service.
2425
- `packages/checkPrescriptionStatusUpdates` Validates and retrieves prescription status update data.
2526
- `packages/gsul` Expose data owned by PSU but needed by [PfP](https://github.com/NHSDigital/prescriptionsforpatients)
@@ -56,9 +57,9 @@ The contents of this repository are protected by Crown Copyright (C).
5657

5758
## Development
5859

59-
It is recommended that you use visual studio code and a devcontainer as this will install all necessary components and correct versions of tools and languages.
60-
See https://code.visualstudio.com/docs/devcontainers/containers for details on how to set this up on your host machine.
61-
There is also a workspace file in .vscode that should be opened once you have started the devcontainer. The workspace file can also be opened outside of a devcontainer if you wish.
60+
It is recommended that you use visual studio code and a devcontainer as this will install all necessary components and correct versions of tools and languages.
61+
See https://code.visualstudio.com/docs/devcontainers/containers for details on how to set this up on your host machine.
62+
There is also a workspace file in .vscode that should be opened once you have started the devcontainer. The workspace file can also be opened outside of a devcontainer if you wish.
6263
The project uses [SAM](https://aws.amazon.com/serverless/sam/) to develop and deploy the APIs and associated resources.
6364

6465
All commits must be made using [signed commits](https://docs.github.com/en/authentication/managing-commit-signature-verification/signing-commits).
@@ -239,8 +240,8 @@ Workflows are in the `.github/workflows` folder:
239240

240241
### Github pages
241242

242-
Github pages is used to display deployment information. The source for github pages is in the gh-pages branch.
243-
As part of the ci and release workflows, the release tag (either the short commit SHA or release tag) is appended to \_data/{environment}\_deployments.csv so we have a history of releases and replaced in \_data/{environment}\_latest.csv so we now what the latest released version is.
243+
Github pages is used to display deployment information. The source for github pages is in the gh-pages branch.
244+
As part of the ci and release workflows, the release tag (either the short commit SHA or release tag) is appended to \_data/{environment}\_deployments.csv so we have a history of releases and replaced in \_data/{environment}\_latest.csv so we now what the latest released version is.
244245
There are different makefile targets in this branch. These are
245246

246247
- `run-jekyll` - runs the site locally so changes can be previewed during development

SAMtemplates/functions/main.yaml

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ Parameters:
3333
Type: String
3434
Default: none
3535

36+
PostDatedNotificationsSQSQueueUrl:
37+
Type: String
38+
Default: none
39+
3640
SQSSaltSecret:
3741
Type: AWS::SecretsManager::Secret::Name<String>
3842

@@ -121,6 +125,7 @@ Resources:
121125
Variables:
122126
TABLE_NAME: !Ref PrescriptionStatusUpdatesTableName
123127
NHS_NOTIFY_PRESCRIPTIONS_SQS_QUEUE_URL: !Ref NHSNotifyPrescriptionsSQSQueueUrl
128+
POST_DATED_PRESCRIPTIONS_SQS_QUEUE_URL: !Ref PostDatedNotificationsSQSQueueUrl
124129
SQS_SALT: !Ref SQSSaltSecret
125130
ENABLED_SITE_ODS_CODES_PARAM: !Ref EnabledSiteODSCodesParam
126131
ENABLED_SYSTEMS_PARAM: !Ref EnabledSystemsParam
@@ -165,6 +170,7 @@ Resources:
165170
- Fn::ImportValue: !Sub ${StackName}:tables:UsePrescriptionStatusUpdatesKMSKeyPolicyArn
166171
- Fn::ImportValue: !Sub ${StackName}-UseNotificationSQSQueueKMSKeyPolicyArn
167172
- Fn::ImportValue: !Sub ${StackName}-WriteNHSNotifyPrescriptionsSQSQueuePolicyArn
173+
- Fn::ImportValue: !Sub ${StackName}-WritePostDatedNotificationsSQSQueuePolicyArn
168174
- Fn::ImportValue: !Sub ${StackName}-GetSQSSaltSecretPolicy
169175
- Fn::ImportValue: !Sub ${StackName}-GetNotificationsParameterPolicy
170176
LogRetentionInDays: !Ref LogRetentionInDays
@@ -489,6 +495,98 @@ Resources:
489495
- Fn::ImportValue: !Sub ${StackName}-GetPSUSecretPolicy
490496
- Fn::ImportValue: !Sub ${StackName}-UsePSUSecretsKMSKeyPolicyArn
491497

498+
## Post-dated lambda definitions
499+
PostDatedNotifyLambdaScheduleEventRole:
500+
Type: AWS::IAM::Role
501+
Properties:
502+
AssumeRolePolicyDocument:
503+
Version: 2012-10-17
504+
Statement:
505+
- Effect: Allow
506+
Principal:
507+
Service:
508+
- scheduler.amazonaws.com
509+
Action:
510+
- sts:AssumeRole
511+
ManagedPolicyArns:
512+
- !Ref PostDatedNotifyLambdaScheduleEventRolePolicy
513+
514+
PostDatedNotifyLambdaScheduleEventRolePolicy:
515+
Type: AWS::IAM::ManagedPolicy
516+
Properties:
517+
PolicyDocument:
518+
Version: 2012-10-17
519+
Statement:
520+
- Effect: Allow
521+
Action:
522+
- lambda:InvokeFunction
523+
Resource:
524+
- !GetAtt PostDatedNotifyLambda.Arn
525+
526+
PostDatedNotifyLambda:
527+
Type: AWS::Serverless::Function
528+
Properties:
529+
FunctionName: !Sub ${StackName}-postDatedNotifyLambda
530+
Timeout: 900
531+
CodeUri: ../../packages/
532+
Handler: main.handler
533+
Role: !GetAtt PostDatedNotifyLambdaResources.Outputs.LambdaRoleArn
534+
Environment:
535+
Variables:
536+
LOG_LEVEL: !Ref LogLevel
537+
NHS_NOTIFY_PRESCRIPTIONS_SQS_QUEUE_URL: !Ref NHSNotifyPrescriptionsSQSQueueUrl
538+
POST_DATED_PRESCRIPTIONS_SQS_QUEUE_URL: !Ref PostDatedNotificationsSQSQueueUrl
539+
TABLE_NAME: !Ref PrescriptionNotificationStatesTableName
540+
Events:
541+
ScheduleEvent:
542+
Type: ScheduleV2
543+
Properties:
544+
Name: !Sub ${StackName}-PostDatedNotifySchedule
545+
ScheduleExpression: "rate(3 minutes)"
546+
RoleArn: !GetAtt PostDatedNotifyLambdaScheduleEventRole.Arn
547+
Metadata:
548+
BuildMethod: esbuild
549+
guard:
550+
SuppressedRules:
551+
- LAMBDA_DLQ_CHECK
552+
- LAMBDA_INSIDE_VPC
553+
- LAMBDA_CONCURRENCY_CHECK
554+
BuildProperties:
555+
Minify: true
556+
Target: es2020
557+
Sourcemap: true
558+
packages: bundle
559+
# set tsconfig path to whatever you actually have for this lambda package
560+
tsconfig: postDatedLambda/tsconfig.json
561+
EntryPoints:
562+
- postDatedLambda/src/main.ts
563+
564+
PostDatedNotifyLambdaResources:
565+
Type: AWS::Serverless::Application
566+
Properties:
567+
Location: lambda_resources.yaml
568+
Parameters:
569+
StackName: !Ref StackName
570+
LambdaName: !Sub ${StackName}-postDatedNotifyLambda
571+
LambdaArn: !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${StackName}-postDatedNotifyLambda
572+
LogRetentionInDays: !Ref LogRetentionInDays
573+
CloudWatchKMSKeyId: !ImportValue account-resources:CloudwatchLogsKmsKeyArn
574+
EnableSplunk: !Ref EnableSplunk
575+
SplunkSubscriptionFilterRole: !ImportValue lambda-resources:SplunkSubscriptionFilterRole
576+
SplunkDeliveryStreamArn: !ImportValue lambda-resources:SplunkDeliveryStream
577+
IncludeAdditionalPolicies: true
578+
AdditionalPolicies: !Join
579+
- ","
580+
- - Fn::ImportValue: !Sub ${StackName}-WriteNHSNotifyPrescriptionsSQSQueuePolicyArn
581+
- Fn::ImportValue: !Sub ${StackName}-ReadNHSNotifyPrescriptionsSQSQueuePolicyArn
582+
- Fn::ImportValue: !Sub ${StackName}-UseNotificationSQSQueueKMSKeyPolicyArn
583+
- Fn::ImportValue: !Sub ${StackName}-WritePostDatedNotificationsSQSQueuePolicyArn
584+
- Fn::ImportValue: !Sub ${StackName}-ReadPostDatedNotificationsSQSQueuePolicyArn
585+
- Fn::ImportValue: !Sub ${StackName}:tables:${PrescriptionNotificationStatesTableName}:TableReadPolicyArn
586+
- Fn::ImportValue: !Sub ${StackName}:tables:${PrescriptionNotificationStatesTableName}:TableWritePolicyArn
587+
- Fn::ImportValue: !Sub ${StackName}:tables:UsePrescriptionNotificationStatesKMSKeyPolicyArn
588+
## End of post-dated lambda bits
589+
492590
NHSNotifyUpdateCallback:
493591
Type: AWS::Serverless::Function
494592
Properties:
@@ -719,3 +817,12 @@ Outputs:
719817
NHSNotifyUpdateCallbackFunctionArn:
720818
Description: The function ARN of the NHSNotifyUpdateCallback lambda
721819
Value: !GetAtt NHSNotifyUpdateCallback.Arn
820+
821+
# Post-dated lambda outputs
822+
PostDatedNotifyLambdaFunctionName:
823+
Description: The function name of the postDatedNotifyLambda
824+
Value: !Ref PostDatedNotifyLambda
825+
826+
PostDatedNotifyLambdaFunctionArn:
827+
Description: The function ARN of the postDatedNotifyLambda
828+
Value: !GetAtt PostDatedNotifyLambda.Arn

SAMtemplates/messaging/main.yaml

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,65 @@ Resources:
102102
- kms:Decrypt
103103
Resource: !GetAtt NHSNotifyPrescriptionsSQSQueue.Arn
104104

105+
## Post-dated SQS queue starts here
106+
PostDatedNotificationsSQSQueue:
107+
Type: AWS::SQS::Queue
108+
Properties:
109+
QueueName: !Sub ${StackName}-PostDatedNotifications.fifo
110+
FifoQueue: true
111+
ContentBasedDeduplication: false
112+
KmsMasterKeyId: !Ref NotificationSQSQueueKMSKeyAlias
113+
MessageRetentionPeriod: 86400 # 1 day in seconds
114+
RedrivePolicy:
115+
deadLetterTargetArn: !GetAtt PostDatedNotificationsDeadLetterQueue.Arn
116+
maxReceiveCount: 13 # processed every 6 hours for 3 days, plus one for the first time it's placed on queue.
117+
VisibilityTimeout: 300
118+
119+
PostDatedNotificationsDeadLetterQueue:
120+
Type: AWS::SQS::Queue
121+
Properties:
122+
QueueName: !Sub ${StackName}-PostDatedNotificationsDeadLetter.fifo
123+
FifoQueue: true
124+
ContentBasedDeduplication: false
125+
KmsMasterKeyId: !Ref NotificationSQSQueueKMSKeyAlias
126+
MessageRetentionPeriod: 604800 # 1 week in seconds
127+
VisibilityTimeout: 300
128+
129+
ReadPostDatedNotificationsSQSQueuePolicy:
130+
Type: AWS::IAM::ManagedPolicy
131+
Properties:
132+
PolicyDocument:
133+
Version: 2012-10-17
134+
Statement:
135+
- Effect: Allow
136+
Action:
137+
- sqs:ReceiveMessage
138+
- sqs:DeleteMessage
139+
- sqs:ChangeMessageVisibility
140+
- sqs:GetQueueAttributes
141+
- sqs:GetQueueUrl
142+
- kms:GenerateDataKey
143+
- kms:Decrypt
144+
Resource: !GetAtt PostDatedNotificationsSQSQueue.Arn
145+
146+
WritePostDatedNotificationsSQSQueuePolicy:
147+
Type: AWS::IAM::ManagedPolicy
148+
Properties:
149+
ManagedPolicyName: !Sub ${StackName}-PostDatedNotificationsSendMessagePolicy
150+
PolicyDocument:
151+
Version: "2012-10-17"
152+
Statement:
153+
- Effect: Allow
154+
Action:
155+
- sqs:SendMessage
156+
- sqs:SendMessageBatch
157+
- sqs:DeleteMessage
158+
- sqs:GetQueueUrl
159+
- kms:GenerateDataKey
160+
- kms:Decrypt
161+
Resource: !GetAtt PostDatedNotificationsSQSQueue.Arn
162+
# End of post-dated stuff
163+
105164
Outputs:
106165
NHSNotifyPrescriptionsSQSQueueUrl:
107166
Description: The URL of the NHS Notify Prescriptions SQS Queue
@@ -138,3 +197,35 @@ Outputs:
138197
Value: !Ref UseNotificationSQSQueueKMSKeyPolicy
139198
Export:
140199
Name: !Sub ${StackName}-UseNotificationSQSQueueKMSKeyPolicyArn
200+
201+
# Post dated SQS outputs start here
202+
PostDatedNotificationsSQSQueueUrl:
203+
Description: The URL of the Post Dated Notifications SQS Queue
204+
Value: !Ref PostDatedNotificationsSQSQueue
205+
Export:
206+
Name: !Sub ${StackName}-PostDatedNotificationsSQSQueueUrl
207+
208+
PostDatedNotificationsSQSQueueArn:
209+
Description: The ARN of the Post Dated Notifications SQS Queue
210+
Value: !GetAtt PostDatedNotificationsSQSQueue.Arn
211+
Export:
212+
Name: !Sub ${StackName}-PostDatedNotificationsSQSQueueArn
213+
214+
PostDatedNotificationsDeadLetterQueueArn:
215+
Description: The ARN of the Post Dated Notifications Dead Letter Queue
216+
Value: !GetAtt PostDatedNotificationsDeadLetterQueue.Arn
217+
Export:
218+
Name: !Sub ${StackName}-PostDatedNotificationsDeadLetterQueueArn
219+
220+
ReadPostDatedNotificationsSQSQueuePolicyArn:
221+
Description: ARN of policy granting permission to read the post dated notifications queue
222+
Value: !Ref ReadPostDatedNotificationsSQSQueuePolicy
223+
Export:
224+
Name: !Sub ${StackName}-ReadPostDatedNotificationsSQSQueuePolicyArn
225+
226+
WritePostDatedNotificationsSQSQueuePolicyArn:
227+
Description: ARN of policy granting permission to write to the post dated notifications queue
228+
Value: !Ref WritePostDatedNotificationsSQSQueuePolicy
229+
Export:
230+
Name: !Sub ${StackName}-WritePostDatedNotificationsSQSQueuePolicyArn
231+
# End

0 commit comments

Comments
 (0)