MakeFunction.go - AWS Code Sample

MakeFunction.go

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: MIT-0 package main import ( "flag" "fmt" "io/ioutil" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/lambda" "github.com/aws/aws-sdk-go/service/lambda/lambdaiface" ) // MakeFunction creates an AWS Lambda function // Inputs: // svc is a Lambda service client // zipFile is the name of the .zip file // bucket is the name of the bucket // function is the name of the Lambda function // handler is the name of the function that is called in the Lambda function // resource is the Amazon Resource Name (ARN) of the function's execution role // runtime is the compute runtime environment that runs the Lambda function // Output: // If success, information about the Lambda function and nil // Otherwise, nil and an error from the call to ReadFile or CreateFunction func MakeFunction(svc lambdaiface.LambdaAPI, zipFile, bucket, function, handler, resource, runtime *string) (*lambda.FunctionConfiguration, error) { contents, err := ioutil.ReadFile(*zipFile + ".zip") if err != nil { return nil, err } createCode := &lambda.FunctionCode{ S3Bucket: bucket, S3Key: zipFile, S3ObjectVersion: aws.String(""), ZipFile: contents, } createArgs := &lambda.CreateFunctionInput{ Code: createCode, FunctionName: function, Handler: handler, Role: resource, Runtime: runtime, } result, err := svc.CreateFunction(createArgs) return result, err } func main() { zipFile := flag.String("z", "", "The name of the ZIP file (without the .zip extension)") bucket := flag.String("b", "", "the name of bucket to which the ZIP file is uploaded") function := flag.String("f", "", "The name of the Lambda function") handler := flag.String("h", "", "The name of the package.class handling the call") resource := flag.String("a", "", "The ARN of the role that calls the function") runtime := flag.String("r", "", "The runtime for the function") flag.Parse() if *zipFile == "" || *bucket == "" || *function == "" || *handler == "" || *resource == "" || *runtime == "" { fmt.Println("You must supply a zip file name, bucket name, function name, handler, Amazon Resource Name (ARN), and runtime.") fmt.Println("-z ZIP-FILE -b BUCKET -f FUNCTION-NAME -h HANDLER -a ROLE-ARN -r RUNTIME") return } sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) svc := lambda.New(sess) result, err := MakeFunction(svc, zipFile, bucket, function, handler, resource, runtime) if err != nil { fmt.Println("Got error creating function:") fmt.Println(err) return } fmt.Println("Function ARN: " + *result.FunctionArn) }