This project is an AWS Lambda function that captures a screenshot of a web page and uploads it to an AWS S3 bucket using Puppeteer and the Serverless Framework.
Before you begin, make sure you have the following prerequisites installed:
- Node.js (v14 or later)
- npm
- Serverless Framework (v3 or later)
- AWS CLI (configured with your AWS credentials)
-
Clone this repository to your local machine:
git clone <repository-url>
-
Change into the project directory:
cd lambda-screenshot
-
Install project dependencies:
npm install
-
Configure your AWS credentials locally by running:
aws configure
This command will prompt you to enter your AWS access key ID, secret access key, default region, and default output format.
-
Start the serverless-offline plugin for local testing:
serverless offline start
-
The HTTP endpoint for capturing screenshots will be available locally at
http://localhost:3000/captureScreenshot
. -
To test the Lambda function locally, you can use a tool like curl or Postman to make a POST request to
http://localhost:3000/captureScreenshot
with a JSON payload containing the URL to capture a screenshot:curl -X POST http://localhost:3000/captureScreenshot -d '{"url": "https://example.com"}'
Replace
"https://example.com"
with the URL you want to capture.
-
Deploy the function to AWS Lambda using the Serverless Framework:
serverless deploy
This command packages and deploys your Lambda function to your AWS account.
-
After the deployment is complete, you will receive the HTTP endpoint URL for your AWS Lambda function. You can find it in the output of the
serverless deploy
command. -
To capture a screenshot using the deployed Lambda function, make a POST request to the provided endpoint URL with a JSON payload containing the URL to capture.
To remove the deployed resources from AWS Lambda and S3, you can use the following command:
serverless remove
This will delete the AWS resources created by the Serverless Framework.
Certainly, here's a guide on how to set up an AWS account, obtain AWS access keys, and configure the required permissions to run the serverless.yml
for your Lambda function.
-
Create an AWS Account:
If you don't already have an AWS account, you can create one by going to the AWS Sign-Up Page.
-
Sign In to AWS Console:
After creating an account, sign in to the AWS Management Console.
To deploy and manage AWS resources using the Serverless Framework, you'll need AWS access keys, which consist of an Access Key ID and a Secret Access Key.
-
Open AWS IAM (Identity and Access Management):
In the AWS Management Console, search for "IAM" or navigate to "Services" > "IAM."
-
Create an IAM User:
- In the IAM dashboard, click "Users" from the left navigation pane.
- Click the "Add user" button.
-
Set User Details:
- Enter a username for the new user.
- Choose "Programmatic access" as the access type.
-
Set Permissions:
- Attach existing policies directly. You can attach policies like
AdministratorAccess
for full access or create a custom policy with the necessary permissions. For your Lambda function, you'll need permissions for Lambda, S3, and IAM.
- Attach existing policies directly. You can attach policies like
-
Add Tags (Optional):
You can add tags if needed for organizational purposes.
-
Review and Create User:
Review the user details and permissions, and click "Create user."
-
Access Key ID and Secret Access Key:
After creating the user, you will see a screen with the user's details. Here, you can download the user's credentials (Access Key ID and Secret Access Key). Keep these keys secure; they are needed to configure AWS CLI and the Serverless Framework.
-
Install AWS CLI:
If you haven't already installed the AWS CLI, you can download and install it from the official AWS CLI page.
-
Configure AWS CLI:
Open a terminal and run:
aws configure
- Enter the Access Key ID and Secret Access Key when prompted.
- Set your preferred default region (e.g.,
us-east-1
) and output format (e.g.,json
).
The AWS CLI is now configured with your credentials, and you can interact with AWS services using the CLI.
To deploy your Lambda function using the Serverless Framework, you need to configure the appropriate permissions. This usually involves creating an AWS IAM role and attaching policies that grant access to Lambda, S3, and other required services. Here's how:
-
Create an AWS IAM Role:
- In the AWS Management Console, go to the IAM dashboard.
- Click "Roles" from the left navigation pane.
- Click "Create role."
- Select "Lambda" as the trusted entity type.
- Attach policies that provide the necessary permissions. At a minimum, you will need
AWSLambda_FullAccess
for Lambda andAmazonS3FullAccess
for S3.
-
Attach the IAM Role to Your Lambda Function:
In your
serverless.yml
, you can specify theiamRoleStatements
to grant the necessary permissions to your Lambda function. For example:functions: captureScreenshot: handler: handler.captureScreenshot iamRoleStatements: - Effect: Allow Action: - s3:PutObject Resource: arn:aws:s3:::your-bucket-name/*
Ensure you replace
your-bucket-name
with your actual S3 bucket name and add other permissions as needed.
With these steps, your AWS account is set up with access keys, AWS CLI is configured, and your Serverless Framework permissions are defined. You can now deploy and manage your Lambda function with ease.