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

Adding example for AWS SAM #64

Closed
wants to merge 1 commit into from

Conversation

Enzodtz
Copy link

@Enzodtz Enzodtz commented Feb 22, 2023

Hi,

I noticed that there wasn't an example on how to use it with AWS SAM, and there are some details that were painful to find out (#41 etc), so I created a basic reproducible example using the framework.

Unfortunately, I wasn't able to find a contributing guideline for this repo, so I'm just creating this PR with the necessary code. Feel free to edit, add to other branch, or anything.

You can test it following the readme of the example.

Thanks!

@Enzodtz
Copy link
Author

Enzodtz commented Feb 22, 2023

Also, from the main README:

This package works with all the currently supported AWS Lambda Node.js runtimes out of the box.

I was not able to use it with SAM without a layer, I'm not sure if this is intended.

Browsing through the generated container filesystem, I wasn't able to find the binary for chromium, it needed a layer.

@Sparticuz
Copy link
Owner

Awesome! I appreciate it! I'll probably strip it down to just very basic javascript. I want the examples to be as simple as possible.

Thanks for the PR!

@ericArbour
Copy link

@Sparticuz Thank you so much for this amazing package, and @Enzodtz thank you for this SAM example! I was able to get puppeteer-core v20.1 working on lambda using SAM by following this PR 😄. The one thing I had to research further was how to include @sparticuz/chromium and puppeteer-core in a lambda layer that is defined in the SAM template.yaml. I imagine this will be common for people using SAM so I suggest including how to do it in the example.

Something like this in the README.md showing how to prepare the packages for the lambda layer:

mkdir puppeteer-layer
cd puppeteer-layer
npm init -y
# Puppeteer or Playwright is a production dependency
npm install --save puppeteer-core@$PUPPETEER_VERSION
# @sparticuz/chromium can be a DEV dependency IF YOU ARE USING A LAYER, if you are not using a layer, use as a production dependency!
npm install --save-dev @sparticuz/chromium@$CHROMIUM_VERSION
mkdir nodejs
mv node_modules nodejs/

Something like this in the template.yaml:

Resources:
  PuppeteerLayer:
    Type: AWS::Serverless::LayerVersion
    Properties:
      LayerName: puppeteer-layer
      Description: Puppeteer layer
      ContentUri: puppeteer-layer/
      CompatibleRuntimes:
        - nodejs16.x
  HelloWorldFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      Layers:
        - !Ref PuppeteerLayer
      CodeUri: hello-world/
      Handler: app.lambdaHandler
      Runtime: nodejs16.x
      Architectures:
        - x86_64
      Events:
        HelloWorld:
          Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            Path: /hello
            Method: get
    Metadata: # Manage esbuild properties
      BuildMethod: esbuild
      BuildProperties:
        Minify: true
        Target: "es2020"
        # Sourcemap: true # Enabling source maps will create the required NODE_OPTIONS environment variables on your lambda function during sam build
        EntryPoints:
          - app.ts

@Sparticuz
Copy link
Owner

Sparticuz commented Nov 13, 2023

superseded by #178

@Sparticuz Sparticuz closed this Nov 13, 2023
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