AWS SDK for Go
Developer Guide

Creating a Lambda Function

The following example creates the Lambda function functionName in the us-west-2 region using the following values:

  • Role ARN: resourceArn. In most cases, you need to attach only the AWSLambdaExecute managed policy to the policy for this role.

  • Function entry point: handler

  • Runtime: runtime

  • Zip file: zipFileName + .zip

  • Bucket: bucketName

  • Key: zipFileName

The first step is to create the session and Lambda client.

func createFunction(zipFileName string, bucketName string, functionName string, handler string, resourceArn string, runtime string) { // Initialize a session sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) // Create Lambda service client svc := lambda.New(sess, &aws.Config{Region: aws.String("us-west-2")}) contents, err := ioutil.ReadFile(zipFileName + ".zip") if err != nil {

Next, create the structures for the input argument to the CreateFunction function.

os.Exit(0) } createCode := &lambda.FunctionCode{ S3Bucket: aws.String(bucketName), S3Key: aws.String(zipFileName), S3ObjectVersion: aws.String(""), ZipFile: contents, } createArgs := &lambda.CreateFunctionInput{ Code: createCode, FunctionName: aws.String(functionName), Handler: aws.String(handler),

Finally, call CreateFunction and display a message with the result of the call.

Runtime: aws.String(runtime), } result, err := svc.CreateFunction(createArgs) if err != nil { fmt.Println("Cannot create function: " + err.Error())

See the complete example on GitHub.