Skip to content

Commit 9113b02

Browse files
New: [AEA-5696] - Add feedback into EPS Assist Me (#40)
## Summary - Routine Change ### Details - add feedback - use layers for python dependencies - add working cdk-watch makefile target --------- Co-authored-by: Kris Szlapa <kris.szlapa1@nhs.net>
1 parent 70f4f55 commit 9113b02

36 files changed

Lines changed: 3349 additions & 804 deletions

.flake8

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[flake8]
22
max-line-length = 120
33
exclude = .*,venv,node_modules,cdk.out
4-
max-complexity = 10
4+
max-complexity = 12
55
ignore = F821,E203,W503

.github/scripts/fix_cdk_json.sh

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ fix_string_key() {
1515
jq \
1616
--arg key_value "${KEY_VALUE}" \
1717
--arg key_name "${KEY_NAME}" \
18-
'.context += {($key_name): $key_value}' .build/cdk.json > .build/cdk.new.json
19-
mv .build/cdk.new.json .build/cdk.json
18+
'. += {($key_name): $key_value}' "$OUTPUT_FILE_NAME" > "${TEMP_FILE}"
19+
mv "${TEMP_FILE}" "$OUTPUT_FILE_NAME"
2020
}
2121

2222
# helper function to set boolean and number values (without quotes)
@@ -31,17 +31,27 @@ fix_boolean_number_key() {
3131
jq \
3232
--argjson key_value "${KEY_VALUE}" \
3333
--arg key_name "${KEY_NAME}" \
34-
'.context += {($key_name): $key_value}' .build/cdk.json > .build/cdk.new.json
35-
mv .build/cdk.new.json .build/cdk.json
34+
'. += {($key_name): $key_value}' "$OUTPUT_FILE_NAME" > "${TEMP_FILE}"
35+
mv "${TEMP_FILE}" "$OUTPUT_FILE_NAME"
3636
}
3737

38+
39+
OUTPUT_FILE_NAME=$1
40+
if [ -z "${OUTPUT_FILE_NAME}" ]; then
41+
echo "OUTPUT_FILE_NAME value is unset or set to the empty string"
42+
exit 1
43+
fi
44+
echo "{}" > "$OUTPUT_FILE_NAME"
45+
TEMP_FILE=$(mktemp)
46+
3847
CFN_DRIFT_DETECTION_GROUP="epsam"
48+
IS_PULL_REQUEST="false"
3949
if [[ "$STACK_NAME" =~ -pr-[0-9]+$ ]]; then
4050
CFN_DRIFT_DETECTION_GROUP="epsam-pull-request"
51+
IS_PULL_REQUEST="true"
4152
fi
4253

4354
# go through all the key values we need to set
44-
fix_string_key accountId "${ACCOUNT_ID}"
4555
fix_string_key stackName "${STACK_NAME}"
4656
fix_string_key versionNumber "${VERSION_NUMBER}"
4757
fix_string_key commitId "${COMMIT_ID}"
@@ -50,3 +60,4 @@ fix_string_key logLevel "${LOG_LEVEL}"
5060
fix_string_key slackBotToken "${SLACK_BOT_TOKEN}"
5161
fix_string_key slackSigningSecret "${SLACK_SIGNING_SECRET}"
5262
fix_string_key cfnDriftDetectionGroup "${CFN_DRIFT_DETECTION_GROUP}"
63+
fix_boolean_number_key isPullRequest "${IS_PULL_REQUEST}"

.github/workflows/cdk_package_code.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ jobs:
6363
run: |
6464
poetry export --without-hashes --format=requirements.txt --with slackBotFunction > requirements_slackBotFunction
6565
poetry export --without-hashes --format=requirements.txt --with syncKnowledgeBaseFunction > requirements_syncKnowledgeBaseFunction
66-
pip3 install -r requirements_slackBotFunction -t packages/slackBotFunction
67-
pip3 install -r requirements_syncKnowledgeBaseFunction -t packages/syncKnowledgeBaseFunction
66+
pip3 install -r requirements_slackBotFunction -t .dependencies/slackBotFunction/python
67+
pip3 install -r requirements_syncKnowledgeBaseFunction -t .dependencies/syncKnowledgeBaseFunction/python
6868
6969
- name: 'Tar files'
7070
run: |
@@ -75,7 +75,8 @@ jobs:
7575
package.json \
7676
package-lock.json \
7777
tsconfig.defaults.json \
78-
cdk.json
78+
cdk.json \
79+
.dependencies
7980
8081
- uses: actions/upload-artifact@v4
8182
name: upload build artifact

.github/workflows/cdk_release_code.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ jobs:
107107

108108
- name: fix cdk.json for deployment
109109
run: |
110-
./.github/scripts/fix_cdk_json.sh
110+
./.github/scripts/fix_cdk_json.sh .build/epsam_app.json
111111
env:
112112
ACCOUNT_ID: "${{ env.ACCOUNT_ID }}"
113113
STACK_NAME: "${{ inputs.STACK_NAME }}"
@@ -128,6 +128,7 @@ jobs:
128128
-e AWS_REGION="eu-west-2" \
129129
-e SHOW_DIFF="true" \
130130
-e DEPLOY_CODE="false" \
131+
-e CONFIG_FILE_NAME="epsam_app.json" \
131132
-e CDK_APP_PATH="packages/cdk/bin/EpsAssistMeApp.ts" \
132133
cdk-utils-build-repo:latest
133134
@@ -142,6 +143,7 @@ jobs:
142143
-e AWS_REGION="eu-west-2" \
143144
-e SHOW_DIFF="false" \
144145
-e DEPLOY_CODE="true" \
146+
-e CONFIG_FILE_NAME="epsam_app.json" \
145147
-e CDK_APP_PATH="packages/cdk/bin/EpsAssistMeApp.ts" \
146148
cdk-utils-build-repo:latest
147149
shell: bash

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@ vendor
3030
.npmrc
3131
cdk.out
3232
.build
33+
.requirements_slackBotFunction
34+
.requirements_syncKnowledgeBaseFunction
35+
.local_config/
36+
.dependencies/

Makefile

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,13 @@ clean:
5454
rm -rf packages/cdk/lib
5555
rm -rf packages/slackBotFunction/coverage
5656
rm -rf packages/slackBotFunction/.coverage
57+
rm -rf packages/slackBotFunction/.dependencies
5758
rm -rf packages/syncKnowledgeBaseFunction/coverage
58-
rm -rf packages/syncKnowledgeBaseFunction/.coverage
59+
rm -rf .dependencies/
5960
rm -rf cdk.out
6061
rm -rf .build
62+
rm -rf .local_config
63+
rm -rf cfn_guard_output
6164
find . -name '.pytest_cache' -type d -prune -exec rm -rf '{}' +
6265

6366
deep-clean: clean
@@ -98,17 +101,20 @@ cdk-deploy: guard-STACK_NAME
98101
--context slackBotToken=$$SLACK_BOT_TOKEN \
99102
--context slackSigningSecret=$$SLACK_SIGNING_SECRET
100103
cdk-synth:
101-
npx cdk synth \
104+
mkdir -p .dependencies/slackBotFunction
105+
mkdir -p .dependencies/syncKnowledgeBaseFunction
106+
mkdir -p .local_config
107+
STACK_NAME=epsam \
108+
COMMIT_ID=undefined \
109+
VERSION_NUMBER=undefined \
110+
SLACK_BOT_TOKEN=dummy_token \
111+
SLACK_SIGNING_SECRET=dummy_secret \
112+
LOG_RETENTION_IN_DAYS=30 \
113+
LOG_LEVEL=debug \
114+
./.github/scripts/fix_cdk_json.sh .local_config/epsam.config.json
115+
CONFIG_FILE_NAME=.local_config/epsam.config.json npx cdk synth \
102116
--quiet \
103-
--app "npx ts-node --prefer-ts-exts packages/cdk/bin/EpsAssistMeApp.ts" \
104-
--context accountId=123456789012 \
105-
--context stackName=epsam \
106-
--context versionNumber=undefined \
107-
--context commitId=undefined \
108-
--context logRetentionInDays=30 \
109-
--context slackBotToken=dummy \
110-
--context slackSigningSecret=dummy \
111-
--context cfnDriftDetectionGroup=dummy
117+
--app "npx ts-node --prefer-ts-exts packages/cdk/bin/EpsAssistMeApp.ts"
112118

113119
cdk-diff:
114120
npx cdk diff \
@@ -119,23 +125,8 @@ cdk-diff:
119125
--context commitId=$$COMMIT_ID \
120126
--context logRetentionInDays=$$LOG_RETENTION_IN_DAYS
121127

122-
cdk-watch: guard-STACK_NAME
123-
REQUIRE_APPROVAL="$${REQUIRE_APPROVAL:-any-change}" && \
124-
VERSION_NUMBER="$${VERSION_NUMBER:-undefined}" && \
125-
COMMIT_ID="$${COMMIT_ID:-undefined}" && \
126-
npx cdk deploy \
127-
--app "npx ts-node --prefer-ts-exts packages/cdk/bin/EpsAssistMeApp.ts" \
128-
--watch \
129-
--all \
130-
--ci true \
131-
--require-approval $${REQUIRE_APPROVAL} \
132-
--context accountId=$$ACCOUNT_ID \
133-
--context stackName=$$STACK_NAME \
134-
--context versionNumber=$$VERSION_NUMBER \
135-
--context commitId=$$COMMIT_ID \
136-
--context logRetentionInDays=$$LOG_RETENTION_IN_DAYS \
137-
--context slackBotToken=$$SLACK_BOT_TOKEN \
138-
--context slackSigningSecret=$$SLACK_SIGNING_SECRET
128+
cdk-watch:
129+
./scripts/run_sync.sh
139130

140131
sync-docs:
141132
./scripts/sync_docs.sh

cdk.context.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"acknowledged-issue-numbers": [
3+
34892
4+
]
5+
}

packages/cdk/bin/EpsAssistMeApp.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,18 @@ import {App, Aspects, Tags} from "aws-cdk-lib"
33
import {AwsSolutionsChecks} from "cdk-nag"
44
import {EpsAssistMeStack} from "../stacks/EpsAssistMeStack"
55
import {applyCfnGuardSuppressions} from "./utils/appUtils"
6+
import fs from "fs"
67

7-
const app = new App()
8+
// read the config in
9+
const configFileName = process.env["CONFIG_FILE_NAME"]
10+
if (configFileName === undefined) {
11+
throw new Error("Can not read config file")
12+
}
13+
14+
const configDetails = JSON.parse(fs.readFileSync(configFileName, "utf-8"))
15+
16+
// create the app using the config details
17+
const app = new App({context: configDetails})
818

919
/* Required Context:
1020
- accountId
@@ -13,7 +23,6 @@ const app = new App()
1323
- commit
1424
*/
1525

16-
const accountId = app.node.tryGetContext("accountId")
1726
const stackName = app.node.tryGetContext("stackName")
1827
const version = app.node.tryGetContext("versionNumber")
1928
const commit = app.node.tryGetContext("commitId")
@@ -22,16 +31,14 @@ const cfnDriftDetectionGroup = app.node.tryGetContext("cfnDriftDetectionGroup")
2231
Aspects.of(app).add(new AwsSolutionsChecks({verbose: true}))
2332

2433
Tags.of(app).add("cdkApp", "EpsAssistMe")
25-
Tags.of(app).add("accountId", accountId)
2634
Tags.of(app).add("stackName", stackName)
2735
Tags.of(app).add("version", version)
2836
Tags.of(app).add("commit", commit)
2937
Tags.of(app).add("cfnDriftDetectionGroup", cfnDriftDetectionGroup)
3038

3139
const EpsAssistMe = new EpsAssistMeStack(app, "EpsAssistMeStack", {
3240
env: {
33-
region: "eu-west-2",
34-
account: accountId
41+
region: "eu-west-2"
3542
},
3643
stackName: stackName,
3744
version: version,

packages/cdk/constructs/LambdaFunction.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export interface LambdaFunctionProps {
2828
readonly additionalPolicies?: Array<IManagedPolicy>
2929
readonly logRetentionInDays: number
3030
readonly logLevel: string
31+
readonly dependencyLocation?: string
3132
}
3233

3334
// Lambda Insights layer for enhanced monitoring
@@ -115,6 +116,16 @@ export class LambdaFunction extends Construct {
115116
managedPolicies: requiredPolicies
116117
})
117118

119+
const layers = [insightsLambdaLayer]
120+
if (props.dependencyLocation) {
121+
const dependencyLayer = new LayerVersion(this, "DependencyLayer", {
122+
removalPolicy: RemovalPolicy.DESTROY,
123+
code: Code.fromAsset(props.dependencyLocation),
124+
compatibleArchitectures: [Architecture.X86_64]
125+
})
126+
layers.push(dependencyLayer)
127+
}
128+
118129
// Create Lambda function with Python runtime and monitoring
119130
const lambdaFunction = new LambdaFunctionResource(this, props.functionName, {
120131
runtime: Runtime.PYTHON_3_13,
@@ -129,7 +140,7 @@ export class LambdaFunction extends Construct {
129140
POWERTOOLS_LOG_LEVEL: props.logLevel
130141
},
131142
logGroup,
132-
layers: [insightsLambdaLayer]
143+
layers: layers
133144
})
134145

135146
// Suppress CFN guard rules for Lambda function

packages/cdk/resources/Functions.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export class Functions extends Construct {
5252
logRetentionInDays: props.logRetentionInDays,
5353
logLevel: props.logLevel,
5454
additionalPolicies: [props.slackBotManagedPolicy],
55+
dependencyLocation: ".dependencies/slackBotFunction",
5556
environmentVariables: {
5657
"RAG_MODEL_ID": RAG_MODEL_ID,
5758
"QUERY_REFORMULATION_MODEL_ID": QUERY_REFORMULATION_MODEL_ID,
@@ -80,6 +81,7 @@ export class Functions extends Construct {
8081
handler: "app.handler.handler",
8182
logRetentionInDays: props.logRetentionInDays,
8283
logLevel: props.logLevel,
84+
dependencyLocation: ".dependencies/syncKnowledgeBaseFunction",
8385
environmentVariables: {
8486
"KNOWLEDGEBASE_ID": props.knowledgeBaseId,
8587
"DATA_SOURCE_ID": props.dataSourceId

0 commit comments

Comments
 (0)