Skip to content

Commit 7157281

Browse files
committed
Update README with comprehensive stack testing instructions
1 parent 2678c98 commit 7157281

File tree

4 files changed

+61
-24
lines changed

4 files changed

+61
-24
lines changed

cloud/README.md

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This project uses AWS CDK (with TypeScript) to build CloudFormation templates for deployment of all resources for the
44
SpyLogic application. The main stack defines a CodePipeline, configured with a single Stage to deploy the application
5-
stacks on merging into the repo main branch.
5+
stacks on merging into the repo `main` branch.
66

77
The API layer is a _fairly_ typical containerized Node Express server, managed by AWS Fargate with a load-balancer in
88
front. The UI is S3-hosted and served through CloudFront, and Cognito handles AuthN / AuthZ.
@@ -12,7 +12,8 @@ authorize users (via Cognito) when accessing the API. This seemingly simple task
1212
Application Load Balancer (ALB), which only allows _initiating_ authentication rather than verifying an existing access
1313
token. The solution is to re-use our CloudFront distribution for the UI to proxy API requests as well, with an Edge
1414
function to verify the token and, if verified, insert a custom header into the request before passing to the load
15-
balancer. We then filter requests at the load balancer, and reject any requests without the expected header / value.
15+
balancer. We then filter requests at the load balancer, and reject any requests without the expected header and value,
16+
to prevent attempts to bypass auth.
1617

1718
This should be much easier, as it is natively supported by API Gateway, but it seems ALB is yet to catch up.
1819

@@ -44,22 +45,57 @@ need to be deleted manually in the AWS Console.
4445
As the pipeline deploys the application stacks, it is wise to test any changes to those stacks before committing them.
4546
You can do this by synthesizing just the application stacks locally, and deploying to AWS as `dev` stage.
4647

47-
There is one small task to complete before you begin. In AWS Secrets Manager, you will find a secret storing API key and
48-
secret values for the `prod` stage, which the server needs for successful startup. You must create a new secret for the
49-
dev stage, with the same OPENAI_API_KEY value and any random string for SESSION_SECRET. Once that is in place, you can
50-
synthesize and deploy the stacks for testing. Once you have finished, please delete the secret to avoid unnecessary
51-
costs.
48+
Before you begin, ensure you have added [your API key](#server-secrets) into AWS Secrets Manager, for the dev stage.
49+
Once you have finished testing, we recommend deleting the secret to avoid unnecessary costs.
5250

53-
`npm run cdk:test:synth` - synthesizes just the application stacks (i.e. not the pipeline)
51+
```shell
52+
# synthesize the application stacks (no pipeline)
53+
npm run cdk:test:synth
54+
55+
# deploy resources to AWS as "dev" stage
56+
npm run cdk:test:deploy
57+
```
58+
59+
Once this is complete, you will need to set some environment vars for the UI build. Copy the following stack outputs
60+
from the above deployment command into file `frontend/.env`, using [.env.example](../frontend/.env.example) as a
61+
template:
62+
- from dev-spylogic-auth-stack,
63+
copy UserPoolId, UserPoolClient and UserPoolDomain into corresponding properties
64+
- from dev-spylogic-hostedzone-stack,
65+
copy `HostUrl` into VITE_COGNITO_REDIRECT_URL, and `BackendUrl` into VITE_BACKEND_URL
66+
67+
Also uncomment VITE_AUTH_PROVIDER. Once that is done, run the following commands:
68+
69+
```shell
70+
# Build the UI
71+
cd ../frontend
72+
npm run build
73+
74+
# Deploy to S3
75+
aws s3 sync dist s3://dev-spylogic-host-bucket
76+
```
5477

55-
`npm run cdk:test:deploy` - deploys these stacks to AWS as "dev" stage
78+
All being successful, you should see the login screen when you navigate to the `HostUrl` (see above).
79+
Before you can sign in, you'll need to add a user to the dev userpool in Cognito, in AWS Console. After that, log into
80+
the application UI and check everything works as expected - including authenticated calls to the API, and session
81+
persistence via the SpyLogic.sid secure cookie.
5682

57-
All being successful, you should see the application login screen at `https://dev.spylogic.ai`. Log into the AWS Console
58-
to add a user to the dev Cognito userpool, then log into the UI to test app deployment was successful.
83+
Finally, remember to destroy the stacks after testing, else you will rack up costs:
84+
85+
```shell
86+
# Tear down all deployed resources
87+
npm run cdk:test:destroy
88+
```
5989

60-
`npm run cdk:test:destroy` - Remember to destroy the stacks after testing, else you will rack up costs!
90+
## Troubleshooting
6191

62-
---
92+
- `SSOTokenProviderFailure: SSO Token refresh failed. Please log in using "aws sso login"` - Go on, try it!
93+
- Deployment of the spylogic-api-stack fails with "docker authorization token expired" error. Try this:
94+
[docker login for AWS](https://stackoverflow.com/a/66919813)
95+
- AccessDenied page when accessing UI. If deploying manually for testing the stacks, did you forget to deploy the UI to
96+
the host bucket? If deployed via the pipeline, check CodePipeline in AWS Console to see if the task failed.
97+
- UI seems stuck on old version even after merging to main. Is the pipeline awaiting manual approval? This can happen
98+
if changes made to a stack broaden any required permissions.
6399

64100
## A note on costs
65101

cloud/lib/auth-stack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export class AuthStack extends Stack {
142142
this.userPoolId = new CfnOutput(this, 'UserPool.Id', {
143143
value: userPool.userPoolId,
144144
});
145-
this.userPoolClient = new CfnOutput(this, 'UserPoolClient.Id', {
145+
this.userPoolClient = new CfnOutput(this, 'UserPool.Client', {
146146
value: userPoolClient.userPoolClientId,
147147
});
148148
this.userPoolDomain = new CfnOutput(this, 'UserPool.Domain', {

cloud/package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@
1919
"cdk:test:destroy": "cdk destroy --app cdk.test.out",
2020
"cdk:test:destroy:all": "cdk destroy --app cdk.test.out --all",
2121
"cdk:test:clean": "rimraf cdk.test.out",
22-
"cdk:dev:synth": "cdk synth -o cdk.dev.out --context STAGE=dev",
23-
"cdk:dev:deploy": "cdk deploy --app cdk.dev.out --all",
24-
"cdk:dev:destroy": "cdk destroy --app cdk.dev.out --all",
25-
"cdk:dev:clean": "rimraf cdk.dev.out",
2622
"codecheck": "concurrently \"npm run lint:check\" \"npm run format:check\"",
2723
"format": "prettier . --write",
2824
"format:check": "prettier . --check",

frontend/.env.example

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
# For running locally:
12
VITE_BACKEND_URL=http://localhost:3000/api
3+
# For building the UI for remote testing:
4+
#VITE_BACKEND_URL=https://[YOUR_UI_DOMAIN]/api
5+
6+
# Currently only AWS Cognito is supported for AuthN/AuthZ.
7+
# Cognito can integrate with external identity providers, such as Azure.
28

3-
# Currently only AWS Cognito is supported for remote authn/authz
4-
# Note that Cognito can integrate with external identity providers
59
#VITE_AUTH_PROVIDER=cognito
6-
#VITE_COGNITO_REDIRECT_URL=https://YOUR_DOMAIN
7-
#VITE_COGNITO_USERPOOL_ID=YOUR_USERPOOL_ID
8-
#VITE_COGNITO_USERPOOL_CLIENT=YOUR_CLIENT_ID
9-
#VITE_COGNITO_USERPOOL_DOMAIN=YOUR_AUTH_DOMAIN
10+
# Replace these with your actual values:
11+
#VITE_COGNITO_REDIRECT_URL=https://[YOUR_UI_DOMAIN]
12+
#VITE_COGNITO_USERPOOL_ID=[AWS_REGION]_abcdefghi
13+
#VITE_COGNITO_USERPOOL_CLIENT=abcdefghijklmnopqrstuvwxyz
14+
#VITE_COGNITO_USERPOOL_DOMAIN=dev-spylogic.auth.[AWS_REGION].amazoncognito.com

0 commit comments

Comments
 (0)