Blog

Develop EdgeWorkers Code Using the Akamai Control Center

May 12, 2021 · by Javier Garza ·
Categories:

This short blog will guide you through the process required to write, deploy, and test EdgeWorkers code in the Akamai cloud platform using the Akamai Control Center. The EdgeWorkers service makes it possible for you to run serverless code closer to your end users and improve your application performance — all while accelerating deployment times by applying solutions on a massive scale.

Please note that, in order to deploy EdgeWorkers code, you have to perform a one-time setup that consists of creating an EdgeWorker ID and assigning that EdgeWorker ID to an existing property. See the How to Set Up EdgeWorkers blog and the EdgeWorkers User Guide for detailed instructions.

The JavaScript reference includes details about the functions, events, and objects required to write EdgeWorkers code to manipulate HTTP requests and responses. 

In a nutshell, the EdgeWorkers code development workflow consists of four easy steps:

  1. Creating and packaging the source code 

  2. Creating a new EdgeWorker version using the packaged source code 

  3. Activating the new EdgeWorker version 

  4. Testing 

Create and package the source code 

We’ll start our journey by copying the Hello World example of the edgeworker-examples public GitHub repository into our local machine.

github

Navigate to the local folder containing the sample code you just downloaded and open main.js in your favorite text editor.

/*

(c) Copyright 2020 Akamai Technologies, Inc. Licensed under Apache 2 license.

 

Version: 0.2

Purpose:  EdgeWorker that generates a simple html page at the Edge and adds a response header

Repo: https://github.com/akamai/edgeworkers-examples/tree/master/edgeworkers/examples/getting-started/hello-world

*/

 

// Import logging module

import { logger } from 'log';

 

export function onClientRequest (request) {

 // Outputs a message to the X-Akamai-EdgeWorker-onClientRequest-Log header.

 logger.log('Responding with hello world from the path: %s', request.path);

 request.respondWith(

   200, {},

   '<html><body><h1>Hello World From my EdgeWorker</h1></body></html>');

}

 

export function onClientResponse (request, response) {

 // Outputs a message to the X-Akamai-EdgeWorker-onClientResponse-Log header.

 logger.log('Adding a header in ClientResponse');

 

 response.setHeader('X-Hello-World', 'From Akamai EdgeWorkers');

}

Let’s update the code. Edit the HTML response text in line 16 from “Hello World From Akamai EdgeWorkers” to “Hello World from my EdgeWorker” and save the file.

Next, update the manifest file. Open bundle.json with a text editor.

{

   "edgeworker-version": "0.1",

   "description" : "My Hello World Example"

}

Update the version to “0.1,” the description to “My Hello World Example,” and save the file.

We’ll open a Terminal and package both files into a tarball archive with the following command: tar cvfz hello-world.tgz main.js bundle.json.

Next, we’re going to create a new EdgeWorker version using the tarball archive we just created.

Create a new version of EdgeWorkers

Log into the Akamai Control Center as an admin (or any other role that has access to EdgeWorkers), click the hamburger menu on the top left, and select CDN > EdgeWorkers (or just type “EdgeWorkers” into the search box).

chart

The EdgeWorkers management application will show all the different EdgeWorkers that have been created, along with their associated IDs. (IDs are just auto-generated numbers for each EdgeWorker). 

In my case, I already created an EdgeWorker with ID 5357 (see Creating an EdgeWorker ID in the EdgeWorkers User Guide).

chart

Click the EdgeWorker and then the Create Version button.

A new window will open where you can drag and drop the hello-world.tgz archive.

You should get a message with the version number you specified on the manifest file that indicates the archive is valid. Click the Create Version button.

In the version history, you should see that a new version was created and that it’s inactive.

Activate the new version of the EdgeWorker 

Click the Actions button and select “Activate version.” Indicate the version you want to activate (0.1 in my case) and the network (staging in my case), and click the Activate button.

chart

In a few seconds, you’ll see the Activation History screen showing the status as “Activating.”

Note: Only one version of each EdgeWorker ID can be activated at a time. You can, however, activate a previous version at any time.

Test the EdgeWorker 

Next, we’ll test the code we just activated by opening a Web browser with the URL associated with the EdgeWorker. In my case, the URL is: https://Edgeworkers-staging.akamaidevelopers.com/hello-world.

You should see a message that matches the code you have on your main.js file. In my case I see:

hello world

Once you’ve tested your EdgeWorker on staging, you can go ahead and activate it on production. 

Summary

Developing EdgeWorkers code is very similar to any other code development workflow and consists of:

  1. Editing the source code 

  2. Packaging and uploading the code 

  3. Activating the new code 

  4. Testing

Watch our video to run through each step, and refer back to it whenever you need a quick refresh.

Find more resources about EdgeWorkers by visiting developer.akamai.com/edgeworkers. And don’t forget to subscribe to the Akamai Developer YouTube channel for more developer videos!

Resources

About the author

JavierJavier Garza is a developer advocate at Akamai Technologies where he helps the largest companies on the internet run fast and secure apps by leveraging web performance, security, and DevOps best practices. Javier has written many articles on HTTP/2 and web performance, and is the co-author of the O’Reilly Book, Learning HTTP/2. In 2018, Javier spoke at more than 20 developer-related events around the world, including well-known conferences like Velocity, AWS Re:Invent, and PerfMatters. His life’s motto is: share what you learn, and learn what you don’t. In his free time he enjoys challenging workouts and volunteering for non-profits that support children and education.