Skip to content

Commit

Permalink
build(ci): create .env files in EB with Param Store
Browse files Browse the repository at this point in the history
- use an .ebextensions config to create an .env from Parameter Store
  - determine the environment name by looking at container config files
    directly [1]
- mount the .env into /opt/formsg/.env

References:
[1]: https://aws.amazon.com/premiumsupport/knowledge-center/view-environment-properties-elastic-beanstalk/
  • Loading branch information
LoneRifle committed Jun 23, 2021
1 parent 9fc8c9e commit ac27242
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
41 changes: 41 additions & 0 deletions .ebextensions/env-file-creation.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Creates an .env file from AWS SSM Parameter Store

commands:
01-create-env:
command: "/tmp/create-env.sh"

files:
"/tmp/create-env.sh":
mode: "000755"
content : |
#!/bin/bash
# Reach into the undocumented container config
AWS_REGION='`{"Ref": "AWS::Region"}`'
ENV_NAME=$(jq .system.environment_name /opt/elasticbeanstalk/deploy/configuration/containerconfiguration | sed 's/"//g')
TARGET_DIR=/etc/formsg

echo "Checking if ${TARGET_DIR} exists..."
if [ ! -d ${TARGET_DIR} ]; then
echo "Creating directory ${TARGET_DIR} ..."
mkdir -p ${TARGET_DIR}
if [ $? -ne 0 ]; then
echo 'ERROR: Directory creation failed!'
exit 1
fi
else
echo "Directory ${TARGET_DIR} already exists!"
fi
echo "Creating config for ${ENV_NAME} in ${AWS_REGION}"
aws ssm get-parameter --name "${ENV_NAME}-general" --with-decryption --region $AWS_REGION | jq -r '.Parameter.Value' > $TARGET_DIR/.env
aws ssm get-parameter --name "${ENV_NAME}-captcha" --with-decryption --region $AWS_REGION | jq -r '.Parameter.Value' >> $TARGET_DIR/.env
aws ssm get-parameter --name "${ENV_NAME}-ga" --with-decryption --region $AWS_REGION | jq -r '.Parameter.Value' >> $TARGET_DIR/.env
aws ssm get-parameter --name "${ENV_NAME}-intranet" --with-decryption --region $AWS_REGION | jq -r '.Parameter.Value' >> $TARGET_DIR/.env
aws ssm get-parameter --name "${ENV_NAME}-sentry" --with-decryption --region $AWS_REGION | jq -r '.Parameter.Value' >> $TARGET_DIR/.env
aws ssm get-parameter --name "${ENV_NAME}-sms" --with-decryption --region $AWS_REGION | jq -r '.Parameter.Value' >> $TARGET_DIR/.env
aws ssm get-parameter --name "${ENV_NAME}-ndi" --with-decryption --region $AWS_REGION | jq -r '.Parameter.Value' >> $TARGET_DIR/.env
aws ssm get-parameter --name "${ENV_NAME}-verified-fields" --with-decryption --region $AWS_REGION | jq -r '.Parameter.Value' >> $TARGET_DIR/.env
aws ssm get-parameter --name "${ENV_NAME}-webhook-verified-content" --with-decryption --region $AWS_REGION | jq -r '.Parameter.Value' >> $TARGET_DIR/.env

packages:
yum:
jq: []
6 changes: 5 additions & 1 deletion Dockerrun.aws.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
{
"HostDirectory": "/certs",
"ContainerDirectory": "/certs"
},
{
"HostDirectory": "/etc/formsg/.env",
"ContainerDirectory": "/opt/formsg/.env"
}
]
}
}

0 comments on commit ac27242

Please sign in to comment.