View on GitHub

Go Lang Notes

Compilations of Go Lang sample code along with AWS examples. Topped with notes on related topics.

Using Parameter Store with Go Lang

Visitors

Create a Parameter in Parameter Store

Open AWS Parameter Store: Link

Create Parameter that you require. Below names are used for creating parameters for secretkey String

/testapp/secretkey

Define The Same Parameter Name Defined above as Constants preferably in constant.go file

const (
  // SecretKey Parameter name
  SecretKeyParameterName = "/testapp/secretkey"
  // Region
  Region = "us-west-2"
)

Get the Values from Parameter Store

package main

import (
  "log"

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

const (
  // SecretKey Parameter name
  SecretKeyParameterName = "/testapp/secretkey"
  // Region
  Region = "us-west-2"
)

func main() {
  val, err := getValueFromParameterStore(SecretKeyParameterName)
  if err != nil {
    log.Println(err)
  }
  log.Println(val, err)
}

func getValueFromParameterStore(ParameterName string) (value string, err error) {
  sess, err := session.NewSession(&aws.Config{
    Region: aws.String(Region)},
  )
  if err != nil {
    return value, errors.Wrap(err, "not found: SecretKeyParameterName")
  }
  ssmsvc := ssm.New(sess, aws.NewConfig().WithRegion(Region))
  param, err := ssmsvc.GetParameter(&ssm.GetParameterInput{
    Name:           aws.String(ParameterName),
    WithDecryption: aws.Bool(false),
  })
  if err != nil {
    return value, errors.Wrap(err, "SecretKeyParameterName")
  }

  value = *param.Parameter.Value
  return value, err
}

Visitors