skip to content
Alvin Lucillo

Assume a role in AWS with Go

/ 1 min read

💻 Tech

If you’re using AWS in Go, you have need to assume a role for the user to perform actions. Here’s how you can do it:

// create a session first; assuming the environment variables for AWS credentials are set
// replace the region with the actual region
session, _ := session.NewSession(&aws.Config{
    Region: aws.String("us-west-2"),
})

// create credentials from the session with a given role
// role below is not real; replace it with the actual role ARN
creds := stscreds.NewCredentials(session, "arn:aws:iam::123456789012:role/role-name")

// use the credentials; for example, to create an uploader
uploader := s3manager.NewUploader(session, func(u *s3manager.Uploader) {
    u.S3 = s3.New(session, &aws.Config{
        Credentials: creds,
    })
})