Authenticating Go AWS SDK using external id
2022-10-17
For self reference:
Sample code as to how to authenticate aws-sdk-go using external ids:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
... | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/aws/credentials" | |
"github.com/aws/aws-sdk-go/aws/credentials/stscreds" | |
awssession "github.com/aws/aws-sdk-go/aws/session" | |
... | |
) | |
// Create the session: | |
sess, _ := awssession.NewSession(&aws.Config{ | |
Region: aws.String("{region}"), | |
Credentials: credentials.NewStaticCredentials( | |
"{aws-assume-role-key}", | |
"{aws-assume-role-secret}", | |
"", | |
), | |
}) | |
// Create the config with external id from session: | |
cnf := &aws.Config{ | |
Credentials: stscreds.NewCredentials( | |
sess, | |
"{arn-to-assume}", | |
func(p *stscreds.AssumeRoleProvider) { | |
p.ExternalID = aws.String("{external-id}") | |
}, | |
), | |
} | |
// Now create the desired service from cnf: | |
svc := iam.New(sess, cnf) | |
out, err := svc.GetRolePolicy(&iam.GetRolePolicyInput{ | |
RoleName: aws.String("rolename"), | |
PolicyName: aws.String("policyname"), | |
}) | |
... |