Skip to content

Instantly share code, notes, and snippets.

@doi-t
Last active May 28, 2019 03:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save doi-t/565ecdd600a4acbad5c2fc14517a00bb to your computer and use it in GitHub Desktop.
Save doi-t/565ecdd600a4acbad5c2fc14517a00bb to your computer and use it in GitHub Desktop.
Simple example of envattrs for aws lambda function
# echo '{}' > event.json; mkdir -p libs; pip install attrs envattrs -t libs; \
# export YOUR_PREFIX_SLACK_CHANNEL=your_project-staging; \
# export YOUR_PREFIX_S3_BUCKET=pyconapac-bucket; \
# export YOUR_PREFIX_THRESHOLD=50; \
# python-lambda-local -l libs/ -f handler -t 5 lambda_pyconapac.py event.json
from typing import Dict
import attr
import envattrs
@attr.s
class LambdaConfig:
slack_channel = attr.ib(convert=str)
s3_bucket = attr.ib(convert=str)
threshold = attr.ib(convert=int)
def handler(event: Dict, context: Dict):
lambda_config = envattrs.load(LambdaConfig, 'YOUR_PREFIX')
print(f"{lambda_config.__dict__}")
print(f">>> LambdaConfig.slack_channel: {LambdaConfig.slack_channel}")
print(f">>> LambdaConfig.s3_bucket: {LambdaConfig.s3_bucket}")
print(f">>> LambdaConfig.threshold: {LambdaConfig.threshold}")
return lambda_config.__dict__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment