Skip to content

Instantly share code, notes, and snippets.

@xlyk
Created June 27, 2019 23:11
Show Gist options
  • Save xlyk/f2f2246ee259415c05f84eb21218ac73 to your computer and use it in GitHub Desktop.
Save xlyk/f2f2246ee259415c05f84eb21218ac73 to your computer and use it in GitHub Desktop.
golang and AWS secrets manager
package main
import (
"encoding/json"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/secretsmanager"
)
type SecretData struct {
MongoUser string `json:"mongo_user"`
MongoPass string `json:"mongo_pass"`
}
var (
secretName string = "name-goes-here"
region string = "us-west-2"
versionStage string = "AWSCURRENT"
)
func GetSecret() SecretData {
svc := secretsmanager.New(
session.New(),
aws.NewConfig().WithRegion(region),
)
input := &secretsmanager.GetSecretValueInput{
SecretId: aws.String(secretName),
VersionStage: aws.String(versionStage),
}
result, err := svc.GetSecretValue(input)
if err != nil {
panic(err.Error())
}
var secretString string
if result.SecretString != nil {
secretString = *result.SecretString
}
var secretData SecretData
err = json.Unmarshal([]byte(secretString), &secretData)
if err != nil {
panic(err.Error())
}
return secretData
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment