We announced
Managing IAM Account Aliases
This Go example shows you how to create, list, and delete IAM account aliases. You can download complete versions of these example files from the aws-doc-sdk-examples
Scenario
You can use a series of Go routines to manage aliases in IAM. The routines use the AWS SDK for Go IAM client methods that follow:
Prerequisites
-
You have set up and configured the AWS SDK for Go.
-
You are familiar with IAM account aliases. To learn more, see Your AWS account ID and Its Alias in the IAM User Guide.
Create a New IAM Account Alias
This code creates a new IAM user.
Create a new Go file named iam_createaccountalias.go
. You must import the relevant Go and AWS SDK for Go packages by adding the following lines.
package main import ( "fmt" "os" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/iam" )
Set up a session and an IAM client.
func main() { sess, err := session.NewSession(&aws.Config{ Region: aws.String("us-west-2")}, ) // Create a IAM service client. svc := iam.New(sess)
The code takes the new alias as an argument, and then calls CreateAccountAlias
with the alias name.
_, err = svc.CreateAccountAlias(&iam.CreateAccountAliasInput{ AccountAlias: &os.Args[1], }) if err != nil { fmt.Println("Error", err) return } fmt.Printf("Account alias %s has been created\n", os.Args[1])
See the complete example
List IAM Account Aliases
This code lists the aliases for your IAM account.
Create a new Go file named iam_listaccountaliases.go
. You must import the relevant Go and AWS SDK for Go packages by adding the following lines.
package main import ( "fmt" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/iam" )
Set up a session and an IAM client.
func main() { sess, err := session.NewSession(&aws.Config{ Region: aws.String("us-west-2")}, ) // Create a IAM service client. svc := iam.New(sess)
The code calls ListAccountAliases
, specifying to return a maximum of 10 items.
result, err := svc.ListAccountAliases(&iam.ListAccountAliasesInput{ MaxItems: aws.Int64(10), }) if err != nil { fmt.Println("Error", err) return } for i, alias := range result.AccountAliases { if alias == nil { continue } fmt.Printf("Alias %d: %s\n", i, *alias) } }
See the complete example
Delete an IAM Account Alias
This code deletes a specified IAM account alias.
Create a new Go file with the name iam_deleteaccountalias.go
. You must import the relevant Go and AWS SDK for Go packages by adding the following lines.
package main import ( "fmt" "os" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/iam" )
Set up a session and an IAM client.
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")}, ) // Create a IAM service client. svc := iam.New(sess)
The code calls ListAccountAliases
, specifying to return a maximum of 10 items.
_, err = svc.DeleteAccountAlias(&iam.DeleteAccountAliasInput{ AccountAlias: &os.Args[1], }) if err != nil { fmt.Println("Error", err) return } fmt.Printf("Alias %s has been deleted\n", os.Args[1])
See the complete example