AWS SDK for Go
Developer Guide

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.

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 import the packages we use in the example.

"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/lambda" "flag" "fmt" "io/ioutil" "os" )

Next, create the session and Lambda client.

SharedConfigState: session.SharedConfigEnable, })) // Create Lambda service client

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

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.

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

See the complete example on GitHub.