Skip to content

Commit

Permalink
feat: add script to parse and upload env variables from .env (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
FanchenBao authored Nov 23, 2023
1 parent cc7aaf3 commit c88f528
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,5 @@ pyrightconfig.json
.ionide

# End of https://www.toptal.com/developers/gitignore/api/macos,visualstudiocode,python

.env.*
4 changes: 3 additions & 1 deletion app/api/api_v0/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
@router.get("")
async def root():
return {
"ENV": os.getenv("ENV", default='dev'),
"ENV": os.getenv("ENV", default="dev"),
"message": "Hello World!",
"SOME_ENV": os.getenv("SOME_ENV", default=""),
"OTHER_ENV": os.getenv("OTHER_ENV", default=""),
}


Expand Down
4 changes: 2 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
allow_headers=["*"],
)

# Add or comment out the following lines of code to include a new version of API or
# deprecate an old version
# Add or comment out the following lines of code to include a new version of
# API or deprecate an old version
app.include_router(api_v0_router, prefix="/api/v0")

# The magic that allows the integration with AWS Lambda
Expand Down
19 changes: 17 additions & 2 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,25 @@ config_lambda() {


# Update ENV variables for the lambda function
echo -e "${BWhite}Update environment variables for Lambda function $AWS_LAMBDA_FUNC_NAME...${ColorOff}\n"
comment_re="^#.*"
VARIABLES="Variables={"
# Handle a last line that may not be followed by a newline
# https://unix.stackexchange.com/a/418067
while IFS= read -r line || [ -n "$line" ]; do
# Trime leading white space: https://stackoverflow.com/a/3232433/9723036
trimmed="$(echo $line | sed -e 's/^[[:space:]]*//')"
# ignore commented out env and empty lines
if [[ ! $trimmed =~ $comment_re ]] && [ "$trimmed" != "" ];
then
VARIABLES+="$trimmed,"
fi
done < .env.$ENV
VARIABLES+="ENV=$ENV}"

echo -e "${BWhite}Update environment variables...${ColorOff}\n"
until aws lambda update-function-configuration \
--function-name $AWS_LAMBDA_FUNC_NAME \
--environment "Variables={ENV=$ENV}" 2> /dev/null
--environment $VARIABLES 2> /dev/null
do
sleep 2
done
Expand Down

0 comments on commit c88f528

Please sign in to comment.