Skip to content

Instantly share code, notes, and snippets.

@n8sty
Created January 3, 2019 18:13
Show Gist options
  • Save n8sty/6c2b55fcf5293873fc5708a5459ecfdd to your computer and use it in GitHub Desktop.
Save n8sty/6c2b55fcf5293873fc5708a5459ecfdd to your computer and use it in GitHub Desktop.
Get AWS Lambda SSM variables in Zappa
import os
import boto3
class Environment(enum.Enum):
DEVELOPMENT = 'development'
TEST = 'test'
CI = 'ci'
STAGING = 'staging'
PRODUCTION = 'production'
def __str__(self) -> str:
return str(self.value)
def __eq__(self, value) -> bool:
return str(self) == value
# STAGE is a var provided by Zappa indicating an arbitrarily defined
# environment name as defined in zappa_settings.json
APP_ENV: str = os.getenv('STAGE', os.getenv('APP_ENV', str(Environment.DEVELOPMENT))
APP_NAME: str = os.getenv('APP_NAME')
APP_LOAD_SSM_VARS: bool = os.getenv('APP_LOAD_SSM_VARS')
AWS_SSM_VAR_PATH: str = f'/{APP_ENV}/{APP_NAME}/'
SERVER_TYPE: str = os.getenv('SERVERTYPE')
if APP_LOAD_SSM_VARS & SERVER_TYPE == 'AWS Lambda':
ssm = boto3.client('ssm')
ssm_vars = {
'Path': AWS_SSM_VAR_PATH,
'Recursive': True,
'WithDecryption': True,
}
while True:
response = ssm.get_parameters_by_path(**ssm_vars)
for param in response.get('Parameters'):
var_name = os.path.basename(param['Name'])
logger.info(f'Setting {var_name}')
os.environ[var_name] = param['Value']
next_token: typing.Optional[str] = response.get('NextToken')
if not next_token:
break
ssm_vars['NextToken'] = next_token
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment