Skip to content

Commit 0e65746

Browse files
New: [AEA-6028] - Added cleanup scripts for old stacks and proxygen instances (#490)
## Summary - ✨ New Feature
1 parent 18db8c4 commit 0e65746

11 files changed

Lines changed: 1839 additions & 332 deletions

File tree

README.md

Lines changed: 100 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
![Build](https://github.com/NHSDigital/eps-cdk-utils/workflows/release/badge.svg?branch=main)
44

5-
This repository contains a docker image used to deploy CDK to our environments and a CDK constructs library for common EPS project patterns.
5+
This repository contains a docker image used to deploy CDK to our environments and a CDK constructs library for common EPS project patterns, plus shared deployment utilities.
66

77
- `docker/` Contains Dockerfile used to build image used fo for CDK deployments
8-
- `packages/cdkConstructs/` Contains common CDK constructs used in EPS projects
9-
- `packages/depoymentUtils/` Contains shared code for standardizing OAS files and performing proxygen deployments
8+
- `packages/cdkConstructs/` Contains common CDK constructs and CDK helpers used in EPS projects
9+
- `packages/deploymentUtils/` Contains shared code for standardising OpenAPI specifications and performing Proxygen-based deployments
1010
- `scripts/` Utilities helpful to developers of this specification
1111
- `.github/` Contains GitHub workflows that are used for building and deploying from pull requests and releases
1212

@@ -17,13 +17,89 @@ The release workflow does the following
1717
- creates a new version of the cdk construct library and publishes it to github
1818
- pushes the cdk-utils docker image to dev and all other environments (subject to manual release approval in github actions)
1919

20-
## CDK Constructs
20+
## CDK Constructs (`packages/cdkConstructs`)
2121

22-
This contains common CDK constructs used in EPS projects.
22+
This contains common CDK constructs and helpers used in EPS projects.
2323

24-
Available constructs are:
24+
Available constructs and helpers include:
2525

26-
- `TypescriptLambdaFunction` - A reusable construct for TypeScript Lambda functions
26+
- `TypescriptLambdaFunction` – A reusable construct for TypeScript Lambda functions
27+
- `createApp` – Helper for creating a CDK `App` pre-configured with standard EPS tags and stack props
28+
- `deleteUnusedStacks` – Helper functions for cleaning up superseded or PR-based CloudFormation stacks and their Route 53 records
29+
30+
### CDK app bootstrap (`createApp`)
31+
32+
The helper in [packages/cdkConstructs/src/apps/createApp.ts](packages/cdkConstructs/src/apps/createApp.ts) creates a CDK `App` and applies the standard NHS EPS tagging and configuration.
33+
34+
Usage example:
35+
36+
```ts
37+
import {createApp} from "@NHSDigital/eps-cdk-constructs"
38+
39+
const {app, props} = createApp({
40+
productName: "Electronic Prescription Service",
41+
appName: "eps-api",
42+
repoName: "eps-cdk-utils",
43+
driftDetectionGroup: "eps-api"
44+
})
45+
46+
// Use `app` and `props` when defining stacks
47+
```
48+
49+
`createApp` reads deployment metadata from environment variables such as `versionNumber`, `commitId`, `environment` and `isPullRequest`, and exposes them via the returned `props` for use when defining stacks.
50+
51+
### Stack cleanup helpers (`deleteUnusedStacks`)
52+
53+
The helpers in [packages/cdkConstructs/src/stacks/deleteUnusedStacks.ts](packages/cdkConstructs/src/stacks/deleteUnusedStacks.ts) are used to clean up old CloudFormation stacks and their DNS records:
54+
55+
- `deleteUnusedMainStacks(baseStackName, getActiveVersions, hostedZoneName?)` – deletes superseded main and sandbox stacks once the active version has been deployed for at least 24 hours, and removes matching CNAME records from Route 53.
56+
- `deleteUnusedPrStacks(baseStackName, repoName, hostedZoneName?)` – deletes stacks created for pull requests whose GitHub PRs have been closed, and cleans up their CNAME records.
57+
58+
These functions are designed to be invoked from scheduled jobs (for example, a nightly cleanup workflow) after deployment. They rely on:
59+
60+
- APIM status endpoints to determine the active API versions (via `getActiveApiVersions`).
61+
- GitHub’s API to determine whether PRs are closed.
62+
- Route 53 APIs to enumerate and delete CNAME records associated with the stacks.
63+
64+
Refer to [packages/cdkConstructs/tests/stacks/deleteUnusedStacks.test.ts](packages/cdkConstructs/tests/stacks/deleteUnusedStacks.test.ts) for example scenarios.
65+
66+
## Deployment utilities (`packages/deploymentUtils`)
67+
68+
The [packages/deploymentUtils](packages/deploymentUtils) package contains utilities for working with OpenAPI specifications and Proxygen-based API deployments.
69+
70+
It exposes the following main entry points via [packages/deploymentUtils/src/index.ts](packages/deploymentUtils/src/index.ts):
71+
72+
- `deployApi` – Normalises an OpenAPI specification and deploys it via Proxygen Lambda functions, optionally performing blue/green deployments and publishing documentation to the appropriate catalogue.
73+
- `writeSchemas` – Writes JSON Schemas to disk, collapsing `examples` arrays into a single `example` value to be compatible with OAS.
74+
- `deleteProxygenDeployments` – Removes Proxygen PTL instances that correspond to closed GitHub pull requests for a given API.
75+
- Config helpers from `config/index` – used to resolve configuration and CloudFormation export values.
76+
77+
Typical usage pattern (pseudo-code):
78+
79+
```ts
80+
import {deployApi} from "@NHSDigital/eps-deployment-utils"
81+
82+
await deployApi({
83+
spec,
84+
apiName: "eps-api",
85+
version: "v1.2.3",
86+
apigeeEnvironment: "int",
87+
isPullRequest: false,
88+
awsEnvironment: "dev",
89+
stackName: "eps-api-v1-2-3",
90+
mtlsSecretName: "eps-api-mtls",
91+
clientCertExportName: "ClientCertArn",
92+
clientPrivateKeyExportName: "ClientPrivateKeyArn",
93+
proxygenPrivateKeyExportName: "ProxygenPrivateKeyArn",
94+
proxygenKid: "kid-123",
95+
hiddenPaths: ["/internal-only"]
96+
},
97+
true, // blueGreen
98+
false // dryRun
99+
)
100+
```
101+
102+
See the source files under [packages/deploymentUtils/src/specifications](packages/deploymentUtils/src/specifications) and their tests in [packages/deploymentUtils/tests](packages/deploymentUtils/tests) for fuller examples and expected behaviours.
27103

28104
## Contributing
29105

@@ -49,25 +125,35 @@ See [https://code.visualstudio.com/docs/devcontainers/containers](https://code.v
49125
All commits must be made using [signed commits](https://docs.github.com/en/authentication/managing-commit-signature-verification/signing-commits).
50126

51127

52-
### Testing changes to construct library
53-
To test changes to the construct library, you need to package the library and install it on the project you want to test it on.
128+
### Testing changes to construct or deploymentUtils libraries
129+
To test changes to the construct library or the deploymentUtils package, you need to package the relevant library and install it into the project you want to test it in.
54130

55131
Either
56-
- run `make package` from this project and copy the .tgz file from lib folder to the project you want to test in
57-
- create a pull request and from the pull request workflow run, download nhsdigital-eps-cdk-constructs-1.0.0.tgz to the project you want to test in
132+
- run `make package` from this project and copy the generated `.tgz` file(s) from the lib folder to the project you want to test in
133+
- create a pull request and from the pull request workflow run, download the generated `.tgz` artifact(s) (for example `nhsdigital-eps-cdk-constructs-1.0.0.tgz` and/or `nhsdigital-eps-deployment-utils-1.0.0.tgz`) to the project you want to test in
58134

59-
In the project you want to test in, run the following
135+
In the project you want to test in, run the following as appropriate:
60136

61137
```bash
62-
npm install --save NHSDigital-eps-cdk-constructs-1.0.0.tgz --workspace packages/cdk/
138+
# Install the CDK constructs library
139+
npm install --save nhsdigital-eps-cdk-constructs-1.0.0.tgz --workspace packages/cdk/
140+
141+
# Install the deploymentUtils library
142+
npm install --save nhsdigital-eps-deployment-utils-1.0.0.tgz --workspace packages/specifications/
63143
```
64144

65-
You will then be able to use it - for example:
145+
You will then be able to use them - for example:
66146

67147
```typescript
68148
import {TypescriptLambdaFunction} from "@NHSDigital/eps-cdk-constructs"
69149
```
70150

151+
or
152+
153+
```typescript
154+
import {deployApi} from "@nhsdigital/eps-deployment-utils"
155+
```
156+
71157
### Make Commands
72158

73159
There are `make` commands that are run as part of the CI pipeline and help alias some

0 commit comments

Comments
 (0)