PyMacaron

Star
Logo

A python microservice framework

Reference:

Overview
Get started
Write an API specification
Implement your API
Deploy to AWS
JWT authentication
Configuration
Error handling
Asynchronous execution
Database serialisation
Testing

Configuration

pym-config.yaml

All configuration settings of a PymMacaron microservice should be placed in the file ‘pym-config.yaml’ in the root of the microservice project.

As an example, here is the ‘pym-config.yaml’ from pymacaron-helloworld:

name: hello
docker_repo: <MYREPO>
docker_bucket: <DOCKER_CFG_BUCKET>
aws_user: <IAM_USER_NAME>
aws_keypair: aws-eb
aws_instance_type: t2.micro
aws_cert_arn: <ARN_OF_SSL_CERTIFICATE>

with_async: true

jwt_issuer: api.helloworld.com
jwt_audience: helloworldaudienceisgreat
jwt_secret: PYM_JWT_SECRET
live_host: helloworld.com

env_secrets:
  - PYM_JWT_SECRET

Accessing config parameters

The content of ‘pym-config.yaml’ is automatically loaded into a singleton object, accessible at any time by calling ‘ge_config()’:

from pymacaron.config import get_config

# To access the key-values defined in pym-config.yaml:

print get_config().live_host

conf = get_config()
print(conf.this)
print(conf.that)

Environment variables and secrets

You may not want to write all settings into the configuration file.

In particular, you should absolutely not commit secret keys into that file.

The proper way to add secrets to the configuration file is to set them from environment variables and tell PyMacaron to import those environment variables into the configuration singleton by listing their name under the ‘env_secrets’ yaml array in ‘pym-config.yaml’.

All config attributes whose value matches one of the names listed in ‘env_secrets’ will automatically have the content of the corresponding environment variable substituted to their value. This is very convenient when putting secrets in ‘pym-config.yaml’, as shown below:

# So, assuming you have set the environment variable MY_AWS_SECRET,
# your aws user configuration becomes as simple as:

aws_default_region: eu-central-1
aws_access_key_id: OTh0KhP89JKiudehIasd90blr
aws_secret_access_key: MY_AWS_SECRET

env_secrets:
  - MY_AWS_SECRET

# And 'aws_secret_access_key' will automagically have its value replaced with
# the value of the environment variable MY_AWS_SECRET

Access those environment variable in your code just like other settings:

from pymacaron.config import get_config

print(get_config().aws_secret_access_key)

When deploying your microservice, PyMacaron automatically imports all environment variables listed under ‘env_secrets’ into Elastic Beanstalk where they get loaded at runtime into the server’s Docker container.

PyMacaron settings

PyMacaron expects the following key-values to be set in ‘pym-config.yaml’:

The following variables are needed if you want to deploy to Elastic Beanstalk using pymacaron-aws: