Documentation
    Amazon Web Services
    Microsoft Azure
    RapidAPI
    APILayer
Last Update: 2024-02-09

Puppeteer in Lambda


Table of Content


Launch Puppeteer in Lambda
Connect to your VM via SSH
Deploy files on your VM remotely
Set up IAM user
Deploy and Develop - Default NodeJS Runtime
Deploy and Develop - Custom Docker Image

Launch Puppeteer in Lambda

1. Go to the listings page Puppeteer in Lambda on AWS Marketplace.

2. Click on 'Continue to Subscribe', then 'Continue to Configuration' and finally launch it. Follow the instructions on the page to launch the Virtual Machine.

Connect to your VM via SSH


1.
 Please follow the official documentation how to install SSH on your Windows, Mac or Linux machine.'

2. Connect to your VM with user 'ec2-user' by

ssh -i keyfile.pem ec2-user@public-ip

Deploy files on your VM remotely

1. Download and install WinSCP or any other SCP client you want.

2. Choose SFTP as File protocol and enter the public IP address of your instance and 'ec2-user' as username.

3. Click on 'Advanced' and choose 'Authentification' on the left side panel and select your private key file for this instance.

4. Click on 'Login'.

Set up IAM user


1.
We need to create an IAM user we can use for deploying to AWS Lambda. Go to https://console.aws.amazon.com/iam/home, go to 'Users' and click on 'Add user'.
Give it a user name, let's say 'Lambda-Deploy-User' and check the box for 'Programmatic access' and click on Next.

Under 'Set permissions' select 'Attach existing policies directly', search and select the following policies:

AWSLambda_FullAccess
IAMFullAccess
AWSCloudFormationFullAccess
AmazonS3FullAccess
AmazonEC2ContainerRegistryFullAccess

Create user by click on 'Next', 'Review' and 'Create user'.
Copy and save Access key ID and Secret access key somewhere secure. Note: This is the only time you see the secret key.

2. Launch the VM and connect to the terminal via SSH and log into user 'ec2-user'.

3. Set up IAM user credentials by

aws configure

Enter the access and secret key from step 1 and keep region and output format empty.

Deploy and Develop - Default NodeJS Runtime


1.
Launch the VM and connect to the terminal via SSH and log into user 'ec2-user'.
Navigate into puppeteer-lambda directory by

cd /home/ec2-user/puppeteer-lambda

2. Build the Lambda function by

sam build --use-container

Deploy the Puppeteer Lambda Function by

sam deploy --guided

Enter a stack name you like, enter the AWS region your function should resist and hit 'y' when asked. SAM config remain at defaults.

3. The function code is in /home/ec2-user/puppeteer-lambda/code/app.js. You can implement your application there, save it and deploy it on Lambda again. Once deployed you can stop this instance, the code is saved in Lambda.

4. To test your function you can go to https://console.aws.amazon.com/lambda/home, select the previously deployed function, add a test event and click on 'Test'.

5. Some ways to call the function in a production environment:
API Gateway: https://docs.aws.amazon.com/apigateway/index.html
AWS SDK: https://aws.amazon.com/tools
SQS Trigger: https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/welcome.html

Deploy and Develop - Custom Docker Image


1.
 Launch the VM and connect to the terminal via SSH and log into user 'ec2-user'.

2. Get your AWS Account ID (12-digit number) by clicking on 'My Account' in the AWS Management Console to use it at the further steps.

3.  Navigate into puppeteer-docker -> code directory by

cd puppeteer-docker/code

4. The Lambda function code is located in app.js. Edit the script by

nano app.js

or deploy your scripts remotely.

5. Build the Docker image by

docker build -t puppeteer-docker .

6. Create a private repository in ECR by

aws --region us-east-1 ecr create-repository --repository-name puppeteer-docker --image-scanning-configuration scanOnPush=true

7. Tag the previous created Docker image by

docker tag puppeteer-docker:latest <Your-AWS-Account-Id>.dkr.ecr.us-east-1.amazonaws.com/puppeteer-docker:latest

8. Log into your account with Docker by

aws --region us-east-1 ecr get-login-password | docker login --username AWS --password-stdin <Your-AWS-Account-Id>.dkr.ecr.us-east-1.amazonaws.com

9. Push the Docker image to your repository by

docker push <Your-AWS-Account-Id>.dkr.ecr.us-east-1.amazonaws.com/puppeteer-docker:latest

10. Go to Lambda in the AWS Management Console: https://console.aws.amazon.com/lambda

11. Click on 'Create Function' and choose 'Container Image'. Enter a name for your function and click on 'Browse images'. Choose your image repository and select the previously pushed Docker image.

Afterwards create the function.

12. Go to Configuration and allocate at least 768 MB memory and set the timeout to over 1 minute.

13. Configure a test event and run the function by clicking on 'Test'. Yet, AWS does not support to edit the function code in the management console, so you need to repeat steps 4-12 every time you make a change in your code. Have fun developing your own scripts!

14. Additional information:
If your instance run out of storage, delete old Docker images by

docker rm $(docker ps -a -q)
docker rmi $(docker images -q)

More information on Custom Images:
https://docs.aws.amazon.com/lambda/latest/dg/images-create.html
https://aws.amazon.com/de/blogs/aws/new-for-aws-lambda-container-image-support

Ways to call the function in a production environment:
API Gateway: https://docs.aws.amazon.com/apigateway/index.html
AWS SDK: https://aws.amazon.com/tools
SQS Trigger: https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/welcome.html