forked from awsdocs/aws-doc-sdk-examples
/
MakeFunction.go
99 lines (86 loc) · 3.78 KB
/
MakeFunction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
// snippet-start:[lambda.go.make_function]
package main
// snippet-start:[lambda.go.make_function.imports]
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"
)
// snippet-end:[lambda.go.make_function.imports]
// 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) {
// snippet-start:[lambda.go.make_function.read_file]
contents, err := ioutil.ReadFile(*zipFile + ".zip")
// snippet-end:[lambda.go.make_function.read_file]
if err != nil {
return nil, err
}
// snippet-start:[lambda.go.make_function.structs]
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,
}
// snippet-end:[lambda.go.make_function.structs]
// snippet-start:[lambda.go.make_function.call]
result, err := svc.CreateFunction(createArgs)
// snippet-end:[lambda.go.make_function.call]
return result, err
}
func main() {
// snippet-start:[lambda.go.make_function.args]
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
}
// snippet-end:[lambda.go.make_function.args]
// snippet-start:[lambda.go.make_function.session]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
svc := lambda.New(sess)
// snippet-end:[lambda.go.make_function.session]
result, err := MakeFunction(svc, zipFile, bucket, function, handler, resource, runtime)
if err != nil {
fmt.Println("Got error creating function:")
fmt.Println(err)
return
}
// snippet-start:[lambda.go.make_function.display]
fmt.Println("Function ARN: " + *result.FunctionArn)
// snippet-end:[lambda.go.make_function.display]
}
// snippet-end:[lambda.go.make_function]