-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature(function): add custom functions documentation for AWS Amplify-backend #8176
base: main
Are you sure you want to change the base?
feature(function): add custom functions documentation for AWS Amplify-backend #8176
Conversation
… Gen 2 @aws-amplify/amplify-backend/1602
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left two comments around docker and go. But I suggest to wait until @josefaidt gets a chance to review this before making changes.
<Callout warning> | ||
|
||
**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. | ||
|
||
</Callout> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<Callout warning> | |
**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. | |
</Callout> | |
<Callout warning> | |
**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. | |
</Callout> |
src/pages/[platform]/build-a-backend/functions/custom-functions/index.mdx
Show resolved
Hide resolved
src/pages/[platform]/build-a-backend/functions/custom-functions/index.mdx
Outdated
Show resolved
Hide resolved
…, remove go lambda alpha package
}; | ||
} | ||
|
||
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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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: | |
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). |
Technically, you can use any language supported by AWS Lambda. These are NodeJS, Python, Java, .NET, Ruby. | ||
To use other languages in Lambda, such as Go or Rust, use an OS-only runtime. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically, you can use any language supported by AWS Lambda. These are NodeJS, Python, Java, .NET, Ruby. | |
To use other languages in Lambda, such as Go or Rust, use an OS-only runtime. |
Technically, you can use any language supported by AWS Lambda. These are NodeJS, Python, Java, .NET, Ruby. | ||
To use other languages in Lambda, such as Go or Rust, use an OS-only runtime. | ||
|
||
In this guide, you will learn how to create python and golang functions in Amplify Functions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this guide, you will learn how to create python and golang functions in Amplify Functions. | |
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. |
separately we should include a troubleshooting guide for docker errors encountered when using Amplify Hosting to build and deploy backends, then link out from here
# Python functions | ||
To get started, create a new directory and a resource file, `amplify/functions/say-hello/resource.ts`. Then, define the Function with `defineFunction`: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# Python functions | |
To get started, create a new directory and a resource file, `amplify/functions/say-hello/resource.ts`. Then, define the Function with `defineFunction`: | |
## Python | |
To get started, create a new directory and a resource file, `amplify/functions/say-hello/resource.ts`. Then, define the function with `defineFunction`: |
import { defineFunction } from "@aws-amplify/backend"; | ||
import { DockerImage, Duration } from "aws-cdk-lib"; | ||
import { Code, Function, Runtime } from "aws-cdk-lib/aws-lambda"; | ||
import { execSync } from "child_process"; | ||
import * as path from "path"; | ||
import { fileURLToPath } from "url"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import { defineFunction } from "@aws-amplify/backend"; | |
import { DockerImage, Duration } from "aws-cdk-lib"; | |
import { Code, Function, Runtime } from "aws-cdk-lib/aws-lambda"; | |
import { execSync } from "child_process"; | |
import * as path from "path"; | |
import { fileURLToPath } from "url"; | |
import { execSync } from "node:child_process"; | |
import * as path from "node:path"; | |
import { fileURLToPath } from "node:url"; | |
import { defineFunction } from "@aws-amplify/backend"; | |
import { DockerImage, Duration } from "aws-cdk-lib"; | |
import { Code, Function, Runtime } from "aws-cdk-lib/aws-lambda"; |
ordering nit for node imports
} | ||
``` | ||
|
||
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). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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). | |
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). |
casing nit for proper noun
|
||
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). | ||
|
||
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. | |
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. |
|
||
# Go functions | ||
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: | ||
```npm i @aws-cdk/aws-lambda-go-alpha``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this dependency still relevant?
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: | ||
```npm i @aws-cdk/aws-lambda-go-alpha``` | ||
|
||
create a new directory and a resource file, `amplify/functions/say-hello/resource.ts`. Then, define the Function with `defineFunction`: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
create a new directory and a resource file, `amplify/functions/say-hello/resource.ts`. Then, define the Function with `defineFunction`: | |
Create a new directory and a resource file, `amplify/functions/say-hello/resource.ts`. Then, define the function with `defineFunction`: |
|
||
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. | ||
|
||
# Go functions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# Go functions | |
## Go |
import { defineFunction } from "@aws-amplify/backend"; | ||
import { DockerImage, Duration } from "aws-cdk-lib"; | ||
import { Code, Function, Runtime } from "aws-cdk-lib/aws-lambda"; | ||
import { execSync } from "child_process"; | ||
import * as path from "path"; | ||
import { fileURLToPath } from "url"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import { defineFunction } from "@aws-amplify/backend"; | |
import { DockerImage, Duration } from "aws-cdk-lib"; | |
import { Code, Function, Runtime } from "aws-cdk-lib/aws-lambda"; | |
import { execSync } from "child_process"; | |
import * as path from "path"; | |
import { fileURLToPath } from "url"; | |
import { execSync } from "node:child_process"; | |
import * as path from "node:path"; | |
import { fileURLToPath } from "node:url"; | |
import { defineFunction } from "@aws-amplify/backend"; | |
import { DockerImage, Duration } from "aws-cdk-lib"; | |
import { Code, Function, Runtime } from "aws-cdk-lib/aws-lambda"; |
``` | ||
|
||
Then you should run the following command to build the go function: | ||
```bash title="terminal" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
```bash title="terminal" | |
```bash title="Terminal" showLineNumbers={false} |
``` | ||
then run to install the dependencies. | ||
|
||
```bash title="terminal" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
```bash title="terminal" | |
```bash title="Terminal" showLineNumbers={false} |
# Common steps for all languages | ||
Lastly, this function needs to be added to your backend. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# Common steps for all languages | |
Lastly, this function needs to be added to your backend. | |
## Common steps for all languages | |
Regardless of the language used, your function needs to be added to your backend. |
}); | ||
``` | ||
|
||
Now when you run `npx ampx sandbox` or deploy your app on Amplify, it will include your Function. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now when you run `npx ampx sandbox` or deploy your app on Amplify, it will include your Function. | |
Now when you run `npx ampx sandbox` or deploy your app on Amplify, it will include your function. |
|
||
Now when you run `npx ampx sandbox` or deploy your app on Amplify, it will include your Function. | ||
|
||
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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we reword this to remove the language around strongly typed handlers? that will be a typescript-specific feature
export const meta = { | ||
title: 'Custom functions', | ||
description: | ||
'Use another AWS Lambda runtimes like python, golang to perform tasks and customize workflows.', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'Use another AWS Lambda runtimes like python, golang to perform tasks and customize workflows.', | |
'Use another AWS Lambda runtimes like Python, Golang to perform tasks and customize workflows.', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @MarlonJD thank you for taking the time to write this up!! I left a few comments to adjust some of the language used but overall this looks great!
… Gen 2 @aws-amplify/amplify-backend/1602
Description of changes:
Add custom runtime documents, it's required for #1602
Related GitHub issue
#1543, #1486
Instructions
Which product(s) are affected by this PR (if applicable)?
Which platform(s) are affected by this PR (if applicable)?
Please add the product(s)/platform(s) affected to the PR title
amplify-backend
Checks
When this PR is ready to merge, please check the box below
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.