Search code examples
amazon-web-servicesgoamazon-rdsaws-secrets-manager

GetSecretValue, get identity: get credentials: failed to refresh cached credentials


I have a basic Go code like this. Gives me the error:

2024/02/21 21:49:15 operation error Secrets Manager: GetSecretValue,
  get identity: get credentials: failed to refresh cached credentials,
  no EC2 IMDS role found, operation error ec2imds: GetMetadata,
  request canceled, context deadline exceeded

I've used amazon secret manager for aws-rds, and I'm sure credentials are right

secretName := "key"
region := "eu-central-1"

config, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(region))
if err != nil {
    log.Fatal(err)
}

// Create Secrets Manager client
svc := secretsmanager.NewFromConfig(config)

input := &secretsmanager.GetSecretValueInput{
    SecretId:     aws.String(secretName),
    VersionStage: aws.String("AWSCURRENT"), 
}

result, err := svc.GetSecretValue(context.TODO(), input)
if err != nil {
    // For a list of exceptions thrown, see
    // https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
    log.Fatal(err.Error())
}

// Decrypts secret using the associated KMS key.
var secretMap map[string]string
json.Unmarshal([]byte(*result.SecretString), &secretMap)

return secretMap

Solution

  • The problem is you don't have AWS credentials saved on your local machine/environment. To do that follow any of these steps: https://wellarchitectedlabs.com/common/documentation/aws_credentials/ and just simply run the code again. I suggest using the CLI option as that worked easily for me.