Skip to content

Commit 03e2733

Browse files
committed
chore(function): rename function handler and update handler file path, remove go lambda alpha package
1 parent 63f71b5 commit 03e2733

File tree

1 file changed

+39
-21
lines changed
  • src/pages/[platform]/build-a-backend/functions/custom-functions

1 file changed

+39
-21
lines changed

src/pages/[platform]/build-a-backend/functions/custom-functions/index.mdx

+39-21
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,11 @@ import { DockerImage, Duration } from "aws-cdk-lib";
5252
import { Code, Function, Runtime } from "aws-cdk-lib/aws-lambda";
5353
import { execSync } from "child_process";
5454
import * as path from "path";
55+
import { fileURLToPath } from "url";
5556

56-
export const sayHello = defineFunction(
57+
const functionDir = path.dirname(fileURLToPath(import.meta.url));
58+
59+
export const sayHelloFunctionHandler = defineFunction(
5760
(scope) =>
5861
new Function(scope, "say-hello", {
5962
handler: "index.handler",
@@ -77,7 +80,7 @@ export const sayHello = defineFunction(
7780
);
7881
```
7982

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.
8184

8285
```ts title="amplify/functions/say-hello/index.py"
8386
import json
@@ -111,21 +114,36 @@ To get started, install go package from npm. go to your backend directory, you s
111114
112115
```ts title="amplify/functions/say-hello/resource.ts"
113116
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+
);
129147
```
130148

131149
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.
181199
```ts title="amplify/backend.ts"
182200
import { defineBackend } from '@aws-amplify/backend';
183201
// highlight-next-line
184-
import { sayHello } from './functions/say-hello/resource';
202+
import { sayHelloFunctionHandler } from './functions/say-hello/resource';
185203

186204
defineBackend({
187205
// highlight-next-line
188-
sayHello
206+
sayHelloFunctionHandler,
189207
});
190208
```
191209

@@ -195,7 +213,7 @@ To invoke your Function, we recommend adding your [Function as a handler for a c
195213

196214
```ts title="amplify/data/resource.ts"
197215
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"
199217

200218
const schema = a.schema({
201219
// highlight-start
@@ -205,7 +223,7 @@ const schema = a.schema({
205223
name: a.string(),
206224
})
207225
.returns(a.string())
208-
.handler(a.handler.function(sayHello)),
226+
.handler(a.handler.function(sayHelloFunctionHandler)),
209227
// highlight-end
210228
})
211229

0 commit comments

Comments
 (0)