AWS Code Sample

The AWS Documentation website is getting a new look!
Try it now and let us know what you think. Switch to the new look >>

You can return to the original look by selecting English in the language selector above.


sts_assume_role.go creates a set of temporary security credentials.

/* Copyright 2010-2019, Inc. or its affiliates. All Rights Reserved. This file is licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package main import ( "fmt" "" "" "" ) // Usage: // go run sts_assume_role.go func main() { // Initialize a session in us-west-2 that the SDK will use to load // credentials from the shared credentials file ~/.aws/credentials. sess, err := session.NewSession(&aws.Config{ Region: aws.String("us-west-2"), }) if err != nil { fmt.Println("NewSession Error", err) return } // Create a STS client svc := sts.New(sess) roleToAssumeArn := "arn:aws:iam::123456789012:role/roleName" sessionName := "test_session" result, err := svc.AssumeRole(&sts.AssumeRoleInput{ RoleArn: &roleToAssumeArn, RoleSessionName: &sessionName, }) if err != nil { fmt.Println("AssumeRole Error", err) return } fmt.Println(result.AssumedRoleUser) }

Sample Details

Service: sts

Last tested: 2019-01-30

Author: AWS

Type: full-example

On this page: