Documentation

    Amazon Web Services

    Microsoft Azure

    RapidAPI

    APILayer

Last Update: 2024-11-18

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 to Lambda
Troubleshooting

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


First, create an IAM user to use it for deploying to AWS Lambda. Go to the AWS IAM console at https://console.aws.amazon.com/iam/home, navigate to 'Users', and click on 'Add user'.

Give it a username, for example, 'Lambda-Deploy-User', and click on 'Next: Permissions'.

Under 'Set permissions', choose 'Attach existing policies directly'. Then, search for and select 'AmazonEC2ContainerRegistryFullAccess'.

Proceed to create the user by clicking on 'Next: Tags', optionally add tags, then click on 'Next: Review' and finally, 'Create user'.

Select the newly created user and go to the 'Security credentials' tab. Under 'Access keys', create a new access key.

When prompted for the use case, select 'Command Line Interface (CLI)'.

Be sure to save the access key ID and the secret access key for later use in this tutorial. Note: This is the only time you will see the secret access key, so it's important to keep it secure.

Deploy and Develop


Obtain your AWS Account ID (a 12-digit number) by clicking on 'My Account' in the AWS Management Console. You will need this for subsequent steps.

Launch the Puppeteer in Lambda EC2 virtual machine, and connect to the terminal via SSH to log in as 'ec2-user'. Refer to the chapter 'Connect to your VM via SSH' for guidance.

Configure your AWS access and secret keys (refer to the chapter 'Set up IAM user') by executing:

aws configure

Navigate to the puppeteer-docker-nodejs directory with the command:

cd puppeteer-docker-nodejs

The Lambda function code is located in app.js. Edit the script using:

nano app.js

Alternatively, you can deploy your scripts remotely (see the chapter 'Deploy files on your VM remotely').

Build the preconfigured Docker image with the following command:

docker build -t puppeteer-docker-nodejs .

Tag the Docker image you previously created. Replace <Your-AWS-Account-Id> with your actual AWS Account ID (refer to the first step):

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

Create a private Docker image repository in Amazon ECR by running:

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

Log in to your AWS account with Docker. Again, replace <Your-AWS-Account-Id> with your actual AWS Account ID:

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

Upload the Docker image to the ECR repository you created. Ensure to replace <Your-AWS-Account-Id> with your actual AWS Account ID:

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

You can now find the repository holding the Docker image at: https://us-east-1.console.aws.amazon.com/ecr/repositories

To deploy to AWS Lambda, visit https://us-east-1.console.aws.amazon.com/lambda/home and click on 'Create function'.

Select 'Container image' as the source, give your function a name, and proceed.

Click on 'Browse images', select the uploaded image, and then click on 'Create function'.

Once the function is created, navigate to 'Configuration', and adjust the memory to at least 1024 MB and the timeout to a minimum of 20 seconds.

The setup is now complete. You can test the function by selecting the 'Test' tab.

Troubleshooting

How can I cancel my subscription?

To cancel your subscription, follow these steps:

  1. Go to the AWS Marketplace Console at https://aws.amazon.com/marketplace/library. Ensure that you are logged into the account that is subscribed to the product you want to cancel.
  2. Find the product you want to cancel and click on 'Manage.'
  3. Click on 'Actions' and then select 'Cancel subscription.'

For more detailed instructions, visit the AWS Marketplace Buyer Guide at https://docs.aws.amazon.com/marketplace/latest/buyerguide/cancel-subscription.html.