@@ -52,8 +52,11 @@ import { DockerImage, Duration } from "aws-cdk-lib";
52
52
import { Code , Function , Runtime } from " aws-cdk-lib/aws-lambda" ;
53
53
import { execSync } from " child_process" ;
54
54
import * as path from " path" ;
55
+ import { fileURLToPath } from " url" ;
55
56
56
- export const sayHello = defineFunction (
57
+ const functionDir = path .dirname (fileURLToPath (import .meta .url ));
58
+
59
+ export const sayHelloFunctionHandler = defineFunction (
57
60
(scope ) =>
58
61
new Function (scope , " say-hello" , {
59
62
handler: " index.handler" ,
@@ -77,7 +80,7 @@ export const sayHello = defineFunction(
77
80
);
78
81
```
79
82
80
- Next, create the corresponding handler file at ` amplify/functions/say-hello/handler.ts ` . This is where your function code will go.
83
+ Next, create the corresponding handler file at ` amplify/functions/say-hello/index.py ` . This is where your function code will go.
81
84
82
85
``` ts title="amplify/functions/say-hello/index.py"
83
86
import json
@@ -111,21 +114,36 @@ To get started, install go package from npm. go to your backend directory, you s
111
114
112
115
` ` ` ts title =" amplify/functions/say-hello/resource.ts"
113
116
import { defineFunction } from " @aws-amplify/backend" ;
114
- import { GoFunction } from " @aws-cdk/aws-lambda-go-alpha" ;
115
- import { Runtime } from " aws-cdk-lib/aws-lambda" ;
116
-
117
- export const sendSmsGoFunctionHandler = defineFunction ((scope ) => {
118
- return new GoFunction (scope , " say-hello" , {
119
- entry: " ./amplify/function/say-hello/" ,
120
- runtime: Runtime .PROVIDED_AL2023 ,
121
- environment: {
122
- GOARCH: " amd64" ,
123
- },
124
- bundling: {
125
- goBuildFlags: [" -tags lambda.norpc" ],
126
- },
127
- });
128
- });
117
+ import { DockerImage , Duration } from " aws-cdk-lib" ;
118
+ import { Code , Function , Runtime } from " aws-cdk-lib/aws-lambda" ;
119
+ import { execSync } from " child_process" ;
120
+ import * as path from " path" ;
121
+ import { fileURLToPath } from " url" ;
122
+
123
+ const functionDir = path .dirname (fileURLToPath (import .meta .url ));
124
+
125
+ export const sayHelloFunctionHandler = defineFunction (
126
+ (scope ) =>
127
+ new Function (scope , " say-hello" , {
128
+ handler: " bootstrap" ,
129
+ runtime: Runtime .PROVIDED_AL2023 , // or any other python version
130
+ timeout: Duration .seconds (3 ), // default is 3 seconds
131
+ code: Code .fromAsset (functionDir , {
132
+ bundling: {
133
+ image: DockerImage .fromRegistry (" dummy" ),
134
+ local: {
135
+ tryBundle(outputDir : string ) {
136
+ execSync (` rsync -rLv ${functionDir }/* ${path .join (outputDir )} ` );
137
+ execSync (
138
+ ` cd ${path .join (outputDir )} && GOARCH=amd64 GOOS=linux go build -tags lambda.norpc -o ${path .join (outputDir )}/bootstrap ${functionDir }/main.go `
139
+ );
140
+ return true ;
141
+ },
142
+ },
143
+ },
144
+ }),
145
+ }),
146
+ );
129
147
```
130
148
131
149
Next, create the corresponding handler file at ` amplify/functions/say-hello/main.go ` . This is where your function code will go.
@@ -181,11 +199,11 @@ Lastly, this function needs to be added to your backend.
181
199
``` ts title="amplify/backend.ts"
182
200
import { defineBackend } from ' @aws-amplify/backend' ;
183
201
// highlight-next-line
184
- import { sayHello } from ' ./functions/say-hello/resource' ;
202
+ import { sayHelloFunctionHandler } from ' ./functions/say-hello/resource' ;
185
203
186
204
defineBackend ({
187
205
// highlight-next-line
188
- sayHello
206
+ sayHelloFunctionHandler ,
189
207
});
190
208
```
191
209
@@ -195,7 +213,7 @@ To invoke your Function, we recommend adding your [Function as a handler for a c
195
213
196
214
``` ts title="amplify/data/resource.ts"
197
215
import { type ClientSchema , a , defineData } from " @aws-amplify/backend"
198
- import { sayHello } from " ../functions/say-hello/resource"
216
+ import { sayHelloFunctionHandler } from " ../functions/say-hello/resource"
199
217
200
218
const schema = a .schema ({
201
219
// highlight-start
@@ -205,7 +223,7 @@ const schema = a.schema({
205
223
name: a .string (),
206
224
})
207
225
.returns (a .string ())
208
- .handler (a .handler .function (sayHello )),
226
+ .handler (a .handler .function (sayHelloFunctionHandler )),
209
227
// highlight-end
210
228
})
211
229
0 commit comments