AWS Code Sample


Creates a Lambda function.

/* Copyright 2010-2018, 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 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 ( "" "" "" "flag" "fmt" "io/ioutil" "os" ) 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 { fmt.Println("Could not read " + zipFileName + ".zip") 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), Role: aws.String(resourceArn), Runtime: aws.String(runtime), } result, err := svc.CreateFunction(createArgs) if err != nil { fmt.Println("Cannot create function: " + err.Error()) } else { fmt.Println(result) } } func main() { flag.String(&zipFile, "z", "", "The name of the ZIP file (without the .zip extension)") flag.String(&bucketName, "b", "", "the name of bucket to which the ZIP file is uploaded") flag.String(&functionName, "f", "", "The name of the Lambda function") flag.String(&handler, "h", "", "The name of the package.class handling the call") flag.String(&resourceArn, "a", "", "The ARN of the role that calls the function") flag.String(&runtime, "r", "", "The runtime for the function.") flag.Parse() if zipFile == "" || bucketName == "" || functionName == "" || handler == "" || resourceArn == "" || runtime == "" { fmt.Println("You must supply a zip file name, bucket name, function name, handler, ARN, and runtime.") os.Exit(0) } createFunction(zipFile, bucketName, functionName, handler, resourceArn, runtime) }

Sample Details

Service: lambda

Last tested: 2018-03-16

Author: Doug-AWS

Type: full-example

On this page: