Skip to content

Commit

Permalink
Add boto3 script to fetch environment variables for development
Browse files Browse the repository at this point in the history
  • Loading branch information
rkilpadi committed Jul 27, 2023
1 parent 0b34e01 commit 836d2dc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ infrastructure/cognitolambda/node_modules
public/*
/frontend/tsconfig.tsbuildinfo
.idea/
.env
5 changes: 5 additions & 0 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export API_BASE_URL=https://[API_ID].execute-api.us-east-2.amazonaws.com/prod #
export ENV=dev
```

If you have boto3 installed and have set your AWS credentials, you can run this script to fetch the necessary environment variables:
```bash
python3 scripts/generate_env.py $PCUI_STACK_NAME && source .env
```

If you don't have a virtual environment setup already, you can run from the base dir of the project:
```bash
python3 -m venv venv
Expand Down
35 changes: 35 additions & 0 deletions scripts/generate_env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env python3
import boto3
import sys
from os import path

try:
pcui_stack_name = sys.argv[1]
except:
raise TypeError("Please provide a base PCUI stack name")

client = boto3.client('cloudformation')

def get_nested_stack_name(logical_id):
nested_stack = client.describe_stack_resource(StackName=pcui_stack_name, LogicalResourceId=logical_id)
return nested_stack['StackResourceDetail']['PhysicalResourceId']

def get_output(stack_name, output_key):
pcui_stack = client.describe_stacks(StackName=stack_name)
for output in pcui_stack['Stacks'][0]['Outputs']:
if output['OutputKey'] == output_key:
return output['OutputValue']

pc_api_stack_name = get_nested_stack_name('ParallelClusterApi')
pcui_cognito_stack_name = get_nested_stack_name('Cognito')
outpath = f"{path.dirname(path.dirname(__file__))}/.env"

with open(outpath, 'w') as file:
file.write(f"export API_BASE_URL={get_output(pc_api_stack_name, 'ParallelClusterApiInvokeUrl')}\n")
file.write("export ENV=dev\n")
file.write(f"export SECRET_ID={get_output(pcui_stack_name, 'UserPoolClientSecretName')}\n")
file.write("export SITE_URL=http://localhost:5001\n")
file.write(f"export AUDIENCE={get_output(pcui_stack_name, 'AppClientId')}\n")
file.write(f"export AUTH_PATH={get_output(pcui_cognito_stack_name, 'UserPoolAuthDomain')}")

print(f"Wrote to {outpath}")

0 comments on commit 836d2dc

Please sign in to comment.