-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_util.go
More file actions
157 lines (137 loc) · 3.54 KB
/
Copy pathlambda_util.go
File metadata and controls
157 lines (137 loc) · 3.54 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"archive/zip"
"encoding/json"
"io"
"os"
"path/filepath"
"github.com/gabriel-vasile/mimetype"
"github.com/pulumi/pulumi-aws/sdk/v4/go/aws/iam"
"github.com/pulumi/pulumi-aws/sdk/v4/go/aws/s3"
"github.com/pulumi/pulumi-aws/sdk/v5/go/aws/lambda"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
func zipSource(source, target string) error {
// 1. Create a ZIP file and zip.Writer
f, err := os.Create(target)
if err != nil {
return err
}
defer f.Close()
writer := zip.NewWriter(f)
defer writer.Close()
// 2. Go through all the files of the source
return filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// 3. Create a local file header
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
// set compression
header.Method = zip.Deflate
// 4. Set relative path of a file as the header name
header.Name, err = filepath.Rel(filepath.Dir(source), path)
if err != nil {
return err
}
if info.IsDir() {
header.Name += "/"
}
// 5. Create writer for the file header and save content of the file
headerWriter, err := writer.CreateHeader(header)
if err != nil {
return err
}
if info.IsDir() {
return nil
}
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(headerWriter, f)
return err
})
}
func CreateLambdaRole(ctx *pulumi.Context) (*iam.Role, error) {
tmpJSON0, err := json.Marshal(map[string]interface{}{
"Version": "2012-10-17",
"Statement": []map[string]interface{}{
map[string]interface{}{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Sid": "",
"Principal": map[string]interface{}{
"Service": "lambda.amazonaws.com",
},
},
},
})
if err != nil {
return nil, err
}
json0 := string(tmpJSON0)
lambdaRole, err := iam.NewRole(ctx, "lambdaRole", &iam.RoleArgs{
AssumeRolePolicy: pulumi.String(json0),
})
if err != nil {
return nil, err
}
_, err = iam.NewRolePolicyAttachment(ctx, "lambdaRoleAttach", &iam.RolePolicyAttachmentArgs{
Role: lambdaRole.Name,
PolicyArn: iam.ManagedPolicyIAMReadOnlyAccess,
})
if err != nil {
return nil, err
}
return lambdaRole, nil
}
func CreateGoLambda(ctx *pulumi.Context) (*lambda.Function, error) {
// Package lambda function.
err := zipSource("handler/handler", "handler.zip")
if err != nil {
return nil, err
}
// Allow API Gateway to invoke Lambda functions.
role, err := CreateLambdaRole(ctx)
if err != nil {
return nil, err
}
// Provision bucket for uploading Lambda handler.
bucket, err := s3.NewBucket(ctx, "ts-test-bucket-go", nil)
if err != nil {
return nil, err
}
// Upload handler to S3 bucket.
mtype, err := mimetype.DetectFile("./handler.zip")
if err != nil {
return nil, err
}
obj, err := s3.NewBucketObject(ctx, "1.0.0/handler.zip", &s3.BucketObjectArgs{
Bucket: bucket.ID(),
Source: pulumi.NewFileAsset("./handler.zip"),
ContentType: pulumi.String(mtype.String()),
})
// Create and return lambda function
function, err := lambda.NewFunction(
ctx, "lambdaAPIGatewayFunction", &lambda.FunctionArgs{
S3Bucket: bucket.ID(),
S3Key: obj.Key,
Runtime: pulumi.String("go1.x"),
Handler: pulumi.String("handler"),
Role: role.Arn,
},
nil,
)
lambda.NewPermission(ctx, "lambdaPermission", &lambda.PermissionArgs{
Action: pulumi.String("lambda:InvokeFunction"),
Principal: pulumi.String("apigateway.amazonaws.com"),
Function: function,
})
// Enable API Gateway to invoke the Lambda
return function, err
}