go-ssm-config: github.com/ianlopshire/go-ssm-config Index | Examples | Files

package ssmconfig

import "github.com/ianlopshire/go-ssm-config"

Package ssmconfig is a utility for loading configuration values from AWS SSM (Parameter Store) directly into a struct.

Index

Examples

Package Files

ssmconfig.go

func Process Uses

func Process(configPath string, c interface{}) error

Process processes the config with a new default provider.

See Provider.Process() for full documentation.

Code:

// Assuming the following Parameter Store parameters:
//
// | Name                         | Value                | Type         | Key ID        |
// | ---------------------------- | -------------------- | ------------ | ------------- |
// | /example_service/prod/debug  | false                | String       | -             |
// | /example_service/prod/port   | 8080                 | String       | -             |
// | /example_service/prod/user   | Ian                  | String       | -             |
// | /example_service/prod/rate   | 0.5                  | String       | -             |
// | /example_service/prod/secret | zOcZkAGB6aEjN7SAoVBT | SecureString | alias/aws/ssm |

type Config struct {
    Debug  bool    `smm:"debug" default:"true"`
    Port   int     `smm:"port"`
    User   string  `smm:"user"`
    Rate   float32 `smm:"rate"`
    Secret string  `smm:"secret" required:"true"`
}

var c Config
err := ssmconfig.Process("/example_service/prod/", &c)
if err != nil {
    log.Fatal(err.Error())
}

format := "Debug: %v\nPort: %d\nUser: %s\nRate: %f\nSecret: %s\n"
_, err = fmt.Printf(format, c.Debug, c.Port, c.User, c.Rate, c.Secret)
if err != nil {
    log.Fatal(err.Error())
}

ExampleProcess_multipleEnvironments demonstrates how ssmcofig can be used to configure multiple environments.

Code:

// Assuming the following Parameter Store parameters:
//
// | Name                         | Value                | Type         | Key ID        |
// | ---------------------------- | -------------------- | ------------ | ------------- |
// | /example_service/prod/debug  | false                | String       | -             |
// | /example_service/prod/port   | 8080                 | String       | -             |
// | /example_service/prod/user   | Ian                  | String       | -             |
// | /example_service/prod/rate   | 0.5                  | String       | -             |
// | /example_service/prod/secret | zOcZkAGB6aEjN7SAoVBT | SecureString | alias/aws/ssm |
// | ---------------------------- | -------------------- | ------------ | ------------- |
// | /example_service/test/debug  | true                 | String       | -             |
// | /example_service/test/port   | 8080                 | String       | -             |
// | /example_service/test/user   | Ian                  | String       | -             |
// | /example_service/test/rate   | 0.9                  | String       | -             |
// | /example_service/test/secret | TBVoAS7NjEa6BGAkZcOz | SecureString | alias/aws/ssm |

type Config struct {
    Debug  bool    `smm:"debug" default:"true"`
    Port   int     `smm:"port"`
    User   string  `smm:"user"`
    Rate   float32 `smm:"rate"`
    Secret string  `smm:"secret" required:"true"`
}

// An environment variable is used to set the config path. In this example it would be
// set to `/example_service/test/` for test and `/example_service/prod/` for
// production.
configPath := os.Getenv("CONFIG_PATH")

var c Config
err := ssmconfig.Process(configPath, &c)
if err != nil {
    log.Fatal(err.Error())
}

format := "Debug: %v\nPort: %d\nUser: %s\nRate: %f\nSecret: %s\n"
_, err = fmt.Printf(format, c.Debug, c.Port, c.User, c.Rate, c.Secret)
if err != nil {
    log.Fatal(err.Error())
}

type Provider Uses

type Provider struct {
    SSM ssmiface.SSMAPI
}

Provider is a ssm configuration provider.

func (*Provider) Process Uses

func (p *Provider) Process(configPath string, c interface{}) error

Process loads config values from smm (parameter store) into c. Encrypted parameters will automatically be decrypted. c must be a pointer to a struct.

The `ssm` tag is used to lookup the parameter in Parameter Store. It is joined to the provided base path. If the `ssm` tag is missing the struct field will be ignored.

The `default` tag is used to set the default value of a parameter. The default value will only be set if Parameter Store returns the parameter as invalid.

The `required` tag is used to mark a parameter as required. If Parameter Store returns a required parameter as invalid an error will be returned.

The behavior of using the `default` and `required` tags on the same struct field is currently undefined.

Code:

// Assuming the following Parameter Store parameters:
//
// | Name                         | Value                | Type         | Key ID        |
// | ---------------------------- | -------------------- | ------------ | ------------- |
// | /example_service/prod/debug  | false                | String       | -             |
// | /example_service/prod/port   | 8080                 | String       | -             |
// | /example_service/prod/user   | Ian                  | String       | -             |
// | /example_service/prod/rate   | 0.5                  | String       | -             |
// | /example_service/prod/secret | zOcZkAGB6aEjN7SAoVBT | SecureString | alias/aws/ssm |

type Config struct {
    Debug  bool    `smm:"debug" default:"true"`
    Port   int     `smm:"port"`
    User   string  `smm:"user"`
    Rate   float32 `smm:"rate"`
    Secret string  `smm:"secret" required:"true"`
}

sess, err := session.NewSession()
if err != nil {
    log.Fatal(err)
}

provider := &ssmconfig.Provider{
    SSM: ssm.New(sess),
}

var c Config
err = provider.Process("/example_service/prod/", &c)
if err != nil {
    log.Fatal(err.Error())
}

format := "Debug: %v\nPort: %d\nUser: %s\nRate: %f\nSecret: %s\n"
_, err = fmt.Printf(format, c.Debug, c.Port, c.User, c.Rate, c.Secret)
if err != nil {
    log.Fatal(err.Error())
}

Package ssmconfig imports 8 packages (graph). Updated 2020-05-13. Refresh now. Tools for package owners.