-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssm.go
50 lines (46 loc) · 1.15 KB
/
ssm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package ssm
import (
"os"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ssm"
)
// Parse sets the environment using AWS SSM. All parameters are fetched from
// SSM using the provided path, and used to set the current environment.
// It is assumed that all parameters are of type "SecureString".
func Parse(path string) error {
if path == "" {
return nil
}
if !strings.HasSuffix(path, "/") {
path += "/"
}
sess, err := session.NewSession()
if err != nil {
return err
}
svc := ssm.New(sess, aws.NewConfig())
input := ssm.GetParametersByPathInput{
Path: aws.String(path),
WithDecryption: aws.Bool(true),
}
var internalErr error
err = svc.GetParametersByPathPages(&input, func(out *ssm.GetParametersByPathOutput, lastPage bool) bool {
for _, param := range out.Parameters {
name := strings.TrimPrefix(aws.StringValue(param.Name), path)
internalErr = os.Setenv(name, aws.StringValue(param.Value))
if internalErr != nil {
return false
}
}
return true
})
if err != nil {
return err
}
if internalErr != nil {
return internalErr
}
return nil
}