Skip to content

Commit c849582

Browse files
committed
Add boto3 script to fetch environment variables for development
1 parent 0b34e01 commit c849582

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ infrastructure/cognitolambda/node_modules
99
public/*
1010
/frontend/tsconfig.tsbuildinfo
1111
.idea/
12+
.env

DEVELOPMENT.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ export API_BASE_URL=https://[API_ID].execute-api.us-east-2.amazonaws.com/prod #
1010
export ENV=dev
1111
```
1212

13+
If you have boto3 installed and have set your AWS credentials, you can run this script to fetch the necessary environment variables:
14+
```bash
15+
python3 scripts/generate_env.py $PCUI_STACK_NAME && source .env
16+
```
17+
1318
If you don't have a virtual environment setup already, you can run from the base dir of the project:
1419
```bash
1520
python3 -m venv venv

scripts/generate_env.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python3
2+
import boto3
3+
import sys
4+
from os import path
5+
6+
try:
7+
pcui_stack_name = sys.argv[1]
8+
except:
9+
raise TypeError("Please provide a base PCUI stack name")
10+
11+
client = boto3.client('cloudformation')
12+
13+
def get_nested_stack_name(logical_id, stack_name=pcui_stack_name):
14+
nested_stack = client.describe_stack_resource(StackName=stack_name, LogicalResourceId=logical_id)
15+
return nested_stack['StackResourceDetail']['PhysicalResourceId']
16+
17+
def get_output(stack_name, output_key):
18+
stack = client.describe_stacks(StackName=stack_name)['Stacks'][0]
19+
return next((o.get("OutputValue") for o in stack.get('Outputs', []) if o.get("OutputKey") == output_key), None)
20+
21+
pc_api_stack_name = get_nested_stack_name('ParallelClusterApi')
22+
pcui_cognito_stack_name = get_nested_stack_name('Cognito')
23+
outpath = f"{path.dirname(path.dirname(__file__))}/.env"
24+
25+
with open(outpath, 'w') as file:
26+
file.write(f"export API_BASE_URL={get_output(pc_api_stack_name, 'ParallelClusterApiInvokeUrl')}\n")
27+
file.write("export ENV=dev\n")
28+
file.write(f"export SECRET_ID={get_output(pcui_stack_name, 'UserPoolClientSecretName')}\n")
29+
file.write("export SITE_URL=http://localhost:5001\n")
30+
file.write(f"export AUDIENCE={get_output(pcui_stack_name, 'AppClientId')}\n")
31+
file.write(f"export AUTH_PATH={get_output(pcui_cognito_stack_name, 'UserPoolAuthDomain')}")
32+
33+
print(f"Wrote to {outpath}")

0 commit comments

Comments
 (0)