Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

MarlonJD
Copy link

… 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)?

  • amplify-backend
  • amplify-ui
  • amplify-studio
  • amplify-hosting
  • amplify-libraries

Which platform(s) are affected by this PR (if applicable)?

  • JS
  • Swift
  • Android
  • Flutter
  • React Native

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

  • Ready to merge

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Copy link
Member

@sobolk sobolk left a 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.

Comment on lines +35 to +39
<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>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<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>


# 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```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should refrain from using alpha packages in docs.

We should:

  • either convert this sample to use Code.from.. and plain Function (like in case of python).
  • or just remove Go sample.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is an example of how this can be implemented using Function:

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";

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

export const sendSmsGoFunctionHandler = defineFunction(
  (scope) =>
    new Function(scope, "SendSmsGo", {
      handler: "bootstrap",
      runtime: Runtime.PROVIDED_AL2023, // or any other python version
      timeout: Duration.seconds(3), //  default is 3 seconds
      code: Code.fromAsset(functionDir, {
        bundling: {
          image: DockerImage.fromRegistry("dummy"),
          local: {
            tryBundle(outputDir: string) {
              execSync(`rsync -rLv ${functionDir}/* ${path.join(outputDir)}`);
              execSync(
                `cd ${path.join(outputDir)} && GOARCH=amd64 GOOS=linux go build -tags lambda.norpc -o ${path.join(outputDir)}/bootstrap ${functionDir}/main.go`
              );
              return true;
            },
          },
        },
      }),
    }),
);

);
```

Next, create the corresponding handler file at `amplify/functions/say-hello/handler.ts`. This is where your function code will go.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be amplify/functions/say-hello/index.py instead of amplify/functions/say-hello/handler.ts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants