Skip to content

Commit 4b0242f

Browse files
committedJan 7, 2025
docs(functions): improve documentation for Python and Go functions, correcting capitalization and enhancing clarity
1 parent 03e2733 commit 4b0242f

File tree

1 file changed

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

1 file changed

+24
-29
lines changed
 

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

+24-29
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { getCustomStaticPath } from '@/utils/getCustomStaticPath';
33
export const meta = {
44
title: 'Custom functions',
55
description:
6-
'Use another AWS Lambda runtimes like python, golang to perform tasks and customize workflows.',
6+
'Use another AWS Lambda runtimes like Python, Golang to perform tasks and customize workflows.',
77
platforms: [
88
'android',
99
'angular',
@@ -30,29 +30,29 @@ export function getStaticProps(context) {
3030
};
3131
}
3232

33-
AWS Amplify Gen 2 Functions are AWS Lambda functions that can be used to perform tasks and customize workflows in your Amplify app.Functions can be written in Node.js, Python, Go, or any other language supported by AWS Lambda:
33+
AWS Amplify Gen 2 functions are AWS Lambda functions that can be used to perform tasks and customize workflows in your Amplify app. Functions can be written in Node.js, Python, Go, or any [other language supported by AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html).
3434

3535
<Callout warning>
3636

37-
**Note:** Custom runtimes are not supported in Amplify Functions directly. If you need docker support to use a custom runtime for example in Python, Docker is not supported in Amplify Hosting and Amplify backend auto build. You shouldn't use docker build in your function.There is an example of how to use python in a lambda function in the without Docker section below.
37+
**Note:** [Fullstack Git-based environments](https://docs.amplify.aws/react/how-amplify-works/concepts/#fullstack-git-based-environments) do not support Docker for functions bundling out of the box.
3838

3939
</Callout>
4040

4141
Technically, you can use any language supported by AWS Lambda. These are NodeJS, Python, Java, .NET, Ruby.
4242
To use other languages in Lambda, such as Go or Rust, use an OS-only runtime.
4343

44-
In this guide, you will learn how to create python and golang functions in Amplify Functions.
44+
In this guide, you will learn how to create Python and Go functions with Amplify functions. The examples shown in this guide do not use Docker to build functions. Instead, the examples use commands that run on your host system to build, and as such require the necessary tooling for the language you are using for your functions.
4545

46-
# Python functions
47-
To get started, create a new directory and a resource file, `amplify/functions/say-hello/resource.ts`. Then, define the Function with `defineFunction`:
46+
## Python
4847

48+
To get started, create a new directory and a resource file, `amplify/functions/say-hello/resource.ts`. Then, define the function with `defineFunction`:
4949
```ts title="amplify/functions/say-hello/resource.ts"
50+
import { execSync } from "node:child_process";
51+
import * as path from "node:path";
52+
import { fileURLToPath } from "node:url";
5053
import { defineFunction } from "@aws-amplify/backend";
5154
import { DockerImage, Duration } from "aws-cdk-lib";
5255
import { Code, Function, Runtime } from "aws-cdk-lib/aws-lambda";
53-
import { execSync } from "child_process";
54-
import * as path from "path";
55-
import { fileURLToPath } from "url";
5656

5757
const functionDir = path.dirname(fileURLToPath(import.meta.url));
5858

@@ -94,9 +94,9 @@ def handler(event, context):
9494
}
9595
```
9696
97-
The handler file _must_ export a function named "handler". This is the entry point to your function. For more information on writing functions, refer to the [AWS documentation for Lambda function handlers using python](https://docs.aws.amazon.com/lambda/latest/dg/python-handler.html).
97+
The handler file _must_ export a function named "handler". This is the entry point to your function. For more information on writing functions, refer to the [AWS documentation for Lambda function handlers using Python](https://docs.aws.amazon.com/lambda/latest/dg/python-handler.html).
9898
99-
If you need python packages, you can add them to a `requirements.txt` file in the same directory as your handler file. The `bundling` option in the `Code.fromAsset` method will install these packages for you.
99+
If you need Python packages, you can add them to a `requirements.txt` file in the same directory as your handler file. The `bundling` option in the `Code.fromAsset` method will install these packages for you.
100100
Create a `requirements.txt` file in the same directory as your handler file. This file should contain the names of the packages you want to install. For example:
101101
102102
```txt title="amplify/functions/say-hello/requirements.txt"
@@ -106,22 +106,19 @@ some-other-package>=1.0.0
106106
107107
You're now ready to deploy your python function. Next is the same process as the Node.js/TypeScript function. Go to [Common steps for all languages](#common-steps-for-all-languages) to continue.
108108
109-
# Go functions
110-
To get started, install go package from npm. go to your backend directory, you should see amplify folder inside it. and run the following command:
111-
```npm i @aws-cdk/aws-lambda-go-alpha```
112-
113-
create a new directory and a resource file, `amplify/functions/say-hello/resource.ts`. Then, define the Function with `defineFunction`:
109+
## Go
110+
To get started, Create a new directory and a resource file, `amplify/functions/say-hello/resource.ts`. Then, define the function with `defineFunction`:
114111
115112
```ts title="amplify/functions/say-hello/resource.ts"
116113
import { defineFunction } from "@aws-amplify/backend";
117114
import { DockerImage, Duration } from "aws-cdk-lib";
118115
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-
116+
import { execSync } from "node:child_process";
117+
import * as path from "node:path";
118+
import { fileURLToPath } from "node:url";
119+
import { defineFunction } from "@aws-amplify/backend";
120+
import { DockerImage, Duration } from "aws-cdk-lib";
121+
import { Code, Function, Runtime } from "aws-cdk-lib/aws-lambda";
125122
export const sayHelloFunctionHandler = defineFunction(
126123
(scope) =>
127124
new Function(scope, "say-hello", {
@@ -182,22 +179,20 @@ func main() {
182179
```
183180

184181
Then you should run the following command to build the go function:
185-
```bash title="terminal"
182+
```bash title="terminal" showLineNumbers={false}
186183
go mod init lambda
187184
```
188185
then run to install the dependencies.
189186

190-
```bash title="terminal"
187+
```bash title="terminal" showLineNumbers={false}
191188
go mod tidy
192189
```
193190

194191
You're now ready to deploy your golang function. Next is the same process as the Node.js/TypeScript function.
195192

196-
# Common steps for all languages
197-
Lastly, this function needs to be added to your backend.
193+
## Common steps for all languages
198194

199-
```ts title="amplify/backend.ts"
200-
import { defineBackend } from '@aws-amplify/backend';
195+
Regardless of the language used, your function needs to be added to your backend.
201196
// highlight-next-line
202197
import { sayHelloFunctionHandler } from './functions/say-hello/resource';
203198

@@ -212,7 +207,7 @@ Now when you run `npx ampx sandbox` or deploy your app on Amplify, it will inclu
212207
To invoke your Function, we recommend adding your [Function as a handler for a custom query with your Amplify Data resource](/[platform]/build-a-backend/data/custom-business-logic/). This will enable you to strongly type Function arguments and the return statement, and use this to author your Function's business logic. To get started, open your `amplify/data/resource.ts` file and specify a new query in your schema:
213208
214209
```ts title="amplify/data/resource.ts"
215-
import { type ClientSchema, a, defineData } from "@aws-amplify/backend"
210+
Now when you run `npx ampx sandbox` or deploy your app on Amplify, it will include your function.
216211
import { sayHelloFunctionHandler } from "../functions/say-hello/resource"
217212
218213
const schema = a.schema({

0 commit comments

Comments
 (0)