terraform-nodejs-screenshooter

A thing I made for a talk to teach people about Terraform and Infrastructure as Code.

Stars
5

ScreenShooter

This project exists due to a talk I'm giving (or gave, depending on the time of writing) at Node Congress. The talk is about Infrastructure as Code (IaC) and features a Node.js application that is deployed to Amazon Web Services without ever leaving the editor.

What It Is

This tool is a simple lambda function that takes a given url as input, and gives you a JPEG screenshot as output. It serves the image above, and can provide a 720p screenshot of any website of your preference at the following URL:

https://yjc4y405v8.execute-api.eu-central-1.amazonaws.com/prod?url={YOUR_URL_HERE}

How It Works

This project uses Terraform to declaratively describe the infrastructure we need. Terraform then makes this real by talking to AWS via its API and creating/modifying the resources we need. Terraform also has state management, so it can make cumulative and incremental updates.

The Node.js Part

The Node.js part uses chrome-aws-lambda and puppeteer-core to spin up a headless version of Chrome, visit a website, and take a screenshot. It then returns the image represented as a Base64 string. This is wrapped in an AWS Lambda Handler and then deployed to AWS using Terraform.

The Terraform Part

It's one file that outlines the infrastructure we need (think blueprint). It then magically creates/updates this when we run terraform apply locally.

How Can I Use It?

Prerequisites

  1. Ensure you have Terraform installed. If you don't, use this guide.
  2. Ensure you have an AWS account.
  3. Ensure you have the AWS cli.
  4. Ensure you've logged in on your system with the AWS cli and you have credentials under ~/.aws/credentials.

Getting Started

  1. Clone this repo
  2. cd into the cloned directory
  3. cd screenshooter to open the Node.js part
  4. Build the lambda function with yarn && yarn build
  5. cd .. to go back
  6. terraform init && terraform apply to put it online

Once it's online, you'll see URLs for dev and prod stages of your deployed function!

Run terraform destroy to tear everything down.

Keep In Touch

If you'd like to talk or be friends or whatever, let's do it on twitter