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 ¶
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 ¶
Provider is a ssm configuration provider.
func (*Provider) Process ¶
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: