ssmconfig

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 8, 2020 License: MIT Imports: 8 Imported by: 0

README

ssmconfig GoDoc Report card Go Cover

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

SSMConfig is a utility for loading configuration parameters from AWS SSM (Parameter Store) directly into a struct. This package is largely inspired by kelseyhightower/envconfig.

Motivation

This package was created to reduce the boilerplate code required when using Parameter Store to provide configuration to AWS Lambda functions. It should be suitable for additional applications.

Usage

Set some parameters in AWS Parameter Store:

Name Value Type Key ID
/exmaple_service/prod/debug false String -
/exmaple_service/prod/port 8080 String -
/exmaple_service/prod/user Ian String -
/exmaple_service/prod/rate 0.5 String -
/exmaple_service/prod/secret zOcZkAGB6aEjN7SAoVBT SecureString alias/aws/ssm

Write some code:

package main

import (
    "fmt"
    "log"
    "time"

    ssmconfig "github.com/ianlopshire/go-ssm-config"
)

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"`
}

func main() {
    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())
    }
}

Result:

Debug: false
Port: 8080
User: Ian
Rate: 0.500000
Secret: zOcZkAGB6aEjN7SAoVBT

Additional examples can be found in godoc.

Struct Tag Support

ssmconfig supports the use of struct tags to specify parameter name, default value, and required parameters.

type Config struct {
    Param         string `ssm:"param"`
    RequiredParam string `ssm:"required_param" required:"true"`
    DefaultParam  string `ssm:"default_param" default:"foobar"`
}

The ssm tag is used to lookup the parameter in Parameter Store. It is joined to the base path passed into Process(). If the ssm tag is missing ssmconfig will ignore the struct field.

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, ssmconfig will return an error.

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

Supported Struct Field Types

ssmconfig supports these struct field types:

  • string
  • int, int8, int16, int32, int64
  • bool
  • float32, float64

More supported types may be added in the future.

Licence

MIT

Documentation

Overview

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

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Process

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

Process processes the config with a new default provider.

See Provider.Process() for full documentation.

Example
package main

import (
	"fmt"
	"log"

	ssmconfig "github.com/ianlopshire/go-ssm-config"
)

func main() {
	// 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())
	}
}
Output:

Example (MultipleEnvironments)

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

package main

import (
	"fmt"
	"log"
	"os"

	ssmconfig "github.com/ianlopshire/go-ssm-config"
)

func main() {
	// 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())
	}
}
Output:

Types

type Provider

type Provider struct {
	SSM ssmiface.SSMAPI
}

Provider is a ssm configuration provider.

func (*Provider) Process

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.

Example
package main

import (
	"fmt"
	"log"

	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/ssm"

	ssmconfig "github.com/ianlopshire/go-ssm-config"
)

func main() {
	// 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())
	}
}
Output:

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL