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.

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 { fmt.Println("Could not read " + zipFileName + ".zip") os.Exit(0) }

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

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), Role: aws.String(resourceArn), Runtime: aws.String(runtime), }

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

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

See the complete example on GitHub.