Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 614
Yusuf Gören Avatar asked Oct 30 '25 10:10

Yusuf Gören


1 Answers

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.

like image 59
Khrees2412 Avatar answered Nov 02 '25 20:11

Khrees2412



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!