AWS Code Sample
Catalog

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.

CognitoCreateUser.go

CognitoCreateUser.go creates a user in a Amazon Cognito user pool.

/* Copyright 2010-2019 Amazon.com, 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 http://aws.amazon.com/apache2.0/ 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 ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/cognitoidentityprovider" "flag" "fmt" "os" ) func main() { emailIDptr := flag.String("e", "", "The email address of the user") userPoolIdptr := flag.String("p", "", "The ID of the user pool") userNameptr := flag.String("n", "", "The name of the user") flag.Parse() emailID := *emailIDptr userPoolID := *userPoolIdptr userName := *userNameptr if emailID == "" || userPoolID == "" || userName == "" { fmt.Println("You must supply an email address, user pool ID, and user name") fmt.Println("Usage: go run CreateUser.go -e EMAIL-ADDRESS -p USER-POOL-ID -n USER-NAME") os.Exit(1) } // 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("Got error creating session:", err) os.Exit(1) } // Create Cognito service client cognitoClient := cognitoidentityprovider.New(sess) newUserData := &cognitoidentityprovider.AdminCreateUserInput{ DesiredDeliveryMediums: []*string{ aws.String("EMAIL"), }, UserAttributes: []*cognitoidentityprovider.AttributeType{ { Name: aws.String("email"), Value: aws.String(emailID), }, }, } newUserData.SetUserPoolId(userPoolID) newUserData.SetUsername(userName) _, err = cognitoClient.AdminCreateUser(newUserData) if err != nil { fmt.Println("Got error creating user:", err) } }

Sample Details

Service: cognito

Last tested: 2019-02-12

Author: Doug-AWS

Type: full-example

On this page: