Skip to main content

CLI for rendering configuration templates with SSM Parameter Store as a data source

Project description

SSM Parameter Store Template

Command line application to render templates with data from SSM Parameter Store

Installation

The ssm-ps-template application is available via the Python Package Index and can be installed with pip:

pip install ssm-ps-template

Templating

The application uses Jinja2 for the templating engine. All functionality available to Jinja2 templates by default are exposed in the application.

Getting Parameter Store Values

The application exposes get_parameter(name: str, default: typing.Optional[str] = None) in templates to access the values in SSM Parameter Store.

In the following example we assume there are Parameter Store values for the keys /my-application/foo and /my-application/bar and that the application is called with a prefix of /my-appliction:

foo: {{ get_parameter('/my-application/foo'}}
bar: {{ get_parameter('/my-application/bar'}}

Will render as:

foo: bar
baz: qux

Additionally, there is another function exposed get_parameters_by_path(path: str, default: typing.Optional[dict] = None) which will return a dictionary for the specified path.

The following example will iterate over the results:

{% for key, value in get_parameters_by_path('settings/', {}).items() %}
  {{ key }}: {{ value }}
{% endfor %}

Or you can use a Jinja filter to convert them to YAML:

{{ get_parameters_by_path('settings/') | toyaml | indent(2, first=True) }}

For values in ParameterStore that are stored as StringList, they are automatically transformed as a list of strings. Given the following value:

Key Value
/my-application/connections amqp://server1, amqp://server2

And the following template:

Connections:
{% for connection in get_parameter('/my-application/connections', []) %}
  - {{ connection }}

The following would be rendered:

Connections:
  - amqp://server1
  - amqp://server2

Performance Considerations

The parameter names are gathered in a pre-processing step to minimize calls to SSM Parameter Store.

Configuration

The configuration file provides the ability to specify multiple templates, override AWS configuration, and change logging levels:

Top-Level Configuration Directives

Directive Description
templates An array of template directives as detailed in the next table.
endpoint_url Specify an endpoint URL to use to override the default URL used to contact SSM Parameter Store
profile Specify the AWS profile to use. If unspecified will default to the AWS_DEFAULT_PROFILE environment variable or is unspecified
region Specify the AWS region to use. If unspecified it will default to the AWS_DEFAULT_REGION environment variable or is unspecified
replace_underscores Replace underscores with dashes when asking for values from SSM Parameter Store
verbose Turn debug logging on. Possible values are true and false

Template Configuration Directives

The templates directive in the configuration is an array of objects, defined by a source and destination.

Directive Description
source The source file of the template
destination The destination path to write the rendered template to
prefix The prefix to prepend variables with if they do not start with a forward-slash (/)

Extended Templating Functionality

In addition to the base functionality exposed by Jinja2, the following Python functions have been added:

Function Definition
get_parameter Get a string value from SSM Parameter Store
get_parameters_by_path Get a dictionary value from SSM Parameter Store
urlparse The urllib.parse.urlparse function from the Python standard library.
parse_qs The urllib.parse.parse_qs function from the Python standard library.
unquote The urllib.parse.unquote function from the Python standard library.

The following filters are added:

Filter Description
toyaml Converts a dictionary value to YAML

The following variables are exposed:

Variable Definition
environ The os.environ dictionary for accessing environment variables

Configuration File Format

The application supports JSON, TOML, or YAML for configuration. The following example is in YAML:

Example Configuration File

templates:
  - source: /etc/ssm-templates/nginx-example
    destination: /etc/nginx/sites-available/example
    prefix: /namespaced/application/nginx/
  - source: /etc/ssm-templates/postgres-example
    destination: /etc/postgresql/14/main/postgresql.conf
    prefix: /namespaced/application/postgres/
profile: default
region: us-east-1
verbose: false

Command Line Usage

usage: ssm-ps-template [-h] [--aws-profile AWS_PROFILE] [--aws-region AWS_REGION] [--prefix PREFIX] [--verbose] config

Templating for SSM Parameter Store

positional arguments:
  config

optional arguments:
  -h, --help            show this help message and exit
  --aws-profile AWS_PROFILE
                        AWS Profile
  --aws-region AWS_REGION
                        AWS Region
  --endpoint-url SSM_ENDPOINT_URL
                        Specify an endpoint URL to use when contacting SSM Parameter Store.
  --prefix PREFIX       Default SSM Key Prefix
  --replace-underscores
                        Replace underscores in variable names to dashes when looking for values in SSM
  --verbose

Note that the default SSM prefix can also be set with the PARAMS_PREFIX environment variable and the endpoint URL setting cn be set with the SSM_ENDPOINT_URL environment variable.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ssm-ps-template-2.0.0.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

ssm_ps_template-2.0.0-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file ssm-ps-template-2.0.0.tar.gz.

File metadata

  • Download URL: ssm-ps-template-2.0.0.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for ssm-ps-template-2.0.0.tar.gz
Algorithm Hash digest
SHA256 cd76f2d10dd69e48eae101177c619922918c0f9b1b60ec2693cce1958d60bc43
MD5 77c349c8a4762b2c016f809530d667de
BLAKE2b-256 3d3a554154f5d4db70a694b4ef6925095107ed82203cdf6eba7e27191c0bea01

See more details on using hashes here.

File details

Details for the file ssm_ps_template-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for ssm_ps_template-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1ec8879fb125ae8536b853005a22653891b070158bf0677d88e85e76efd9a86b
MD5 842cecb03b4bbdfc60a581a7f1706b23
BLAKE2b-256 83fc8668686475e57cc4f3aa0592fdd94925b45649bf6a2b8ba811ef18786565

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page