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

docs: Add example showing how to include sharp in custom lambda #7522

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

mtliendo
Copy link

@mtliendo mtliendo commented May 7, 2024

Description of changes:

This PR creates a new lambda example showing how to use a lambda layer with a custom NodeJsFunction

Related GitHub issue #, if available:

Instructions

If this PR should not be merged upon approval for any reason, please submit as a DRAFT

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

  • amplify-cli
  • 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
function/examples

Checks

  • Does this PR conform to the styleguide?

  • [ x] Does this PR include filetypes other than markdown or images? Please add or update unit tests accordingly.

  • Are any files being deleted with this PR? If so, have the needed redirects been created?

  • [ x] Are all links in MDX files using the MDX link syntax rather than HTML link syntax?

    ref: MDX: [link](https://docs.amplify.aws/)
    HTML: <a href="https://docs.amplify.aws/">link</a>

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.


> Feel free to adjust the location of the `zip-file` as needed. Also, if using the `AmplifySet` role, you may need to adjust your permissions.

```sh title="In your terminal"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
```sh title="In your terminal"
```bash title="Terminal"


To get started, ensure you have a Sharp layer deployed to your AWS account.

## Sharp Layer Deployment
Copy link
Contributor

Choose a reason for hiding this comment

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

Step 1 - Deploy your Lambda layer


Amplify Functions are powered by [AWS Lambda](https://aws.amazon.com/lambda/), and allow you to perform a wide variety of customization through self-contained _functions_. However, there are times when a feature may not be directly supported through what Amplify exposes. Fortunately, AWS Amplify allows the use of [custom resources](https://docs.amplify.aws/react/build-a-backend/add-aws-services/custom-resources/) to be created. This enables the full flexibility of the [AWS CDK](https://docs.aws.amazon.com/cdk/api/v2/), while still providing a type-safe environment.

In this section, we'll explore this feature by creating a custom Lambda function that bundles the [Sharp nodejs package](https://www.npmjs.com/package/sharp).
Copy link
Contributor

Choose a reason for hiding this comment

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

cut from here and put it right below the next heading


Once deployed, you'll see the ARN of your Lambda layer similar to below:

```sh title="example lambda layer arn"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
```sh title="example lambda layer arn"
```console title="Terminal"

arn:aws:lambda:us-east-1:211125654548:layer:sharp
```

It's *very* important to note that we'll needs to specify the version in addition to this ARN. In this example, we'll be adding this layer to a `.env.local` file with the version appended as shown:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
It's *very* important to note that we'll needs to specify the version in addition to this ARN. In this example, we'll be adding this layer to a `.env.local` file with the version appended as shown:
It's **very** important to specify the version in addition to this ARN. In this example, we'll be adding this layer to a `.env.local` file with the version appended as shown:

SHARP_LAYER_ARN=arn:aws:lambda:us-east-1:211125654548:layer:sharp:1
```

## Creating a custom Lambda Function with a Lambda Layer
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
## Creating a custom Lambda Function with a Lambda Layer
## Step 2 - Create a custom Lambda function with a Lambda layer

)
```

Above you can see that we are giving our function a name, where it's handler function is located, the runtime, and that our sharp layer is to be used and considered as externally brought in as a dependency.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Above you can see that we are giving our function a name, where it's handler function is located, the runtime, and that our sharp layer is to be used and considered as externally brought in as a dependency.
Above you can see that we are giving our function a namewhere its handler function is located the runtime, and that our sharp layer is to be used and considered as externally brought in as a dependency.


Locally, you will likely want to test out the Sharp library. This can be done by simply installing it as a dev dependency:

```sh title="installing sharp"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
```sh title="installing sharp"
```sh title="Terminal"

renebrandel
renebrandel previously approved these changes May 7, 2024
@mtliendo mtliendo marked this pull request as ready for review May 8, 2024 20:45
@mtliendo mtliendo requested a review from a team as a code owner May 8, 2024 20:45
@josefaidt
Copy link
Contributor

ope @mtliendo did your page get deleted?

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.

None yet

3 participants