简体   繁体   English

如何将 IAM 用户凭证与 Go AWS SDK v2 一起使用

[英]How to use IAM User credentials with Go AWS SDK v2

I'm getting IAM User credentials dynamically and I want to use the credentials I generated using the program to create a session/client .我正在动态获取IAM User凭证,并且我想使用我使用该程序生成的凭证来创建 session/client Not the shared credentials methods.不是共享凭据方法。 Through code.通过代码。 I tried this and I know its not the right way, it doesn't work anyway.我试过这个,我知道它不是正确的方法,它无论如何都不起作用。 Anyone has any idea how to do this properly?任何人都知道如何正确地做到这一点?

    staticCred := aws.NewStaticCredentialsProvider(accessKey, secretKey, "")
    cfg, err := external.LoadDefaultAWSConfig()
    if err != nil {
        panic("unable to load SDK config, " + err.Error())
    }
    cfg.Credentials = staticCred
    cfg.Region = aws.String("ap-southeast-1")
    fmt.Println(cfg)

    svc := ssm.New(cfg)

    req := svc.StartSessionRequest(&ssm.StartSessionInput{
        Target: aws.String(instanceId),
    })
    fmt.Println(req)

    resp, _ := req.Send(context.Background())
    if err != nil {
        log.Fatalf("error sending ssm request : %v", err)
    }
    fmt.Println(resp)

You have to wait for sometime till the credentials actually work.您必须等待一段时间,直到凭据真正起作用。 I'm using Hashicorp Vault to make the credentials btw.我正在使用Hashicorp Vault来制作凭据。 I'm using我在用着

GetCallerIdentityRequest(&sts.GetCallerIdentityInput{}) GetCallerIdentityRequest(&sts.GetCallerIdentityInput{})

to check if they are alive yet.检查他们是否还活着。 If anyone has better solutions for both please feel free to comment如果有人对两者都有更好的解决方案,请随时发表评论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM