Blog

Create a Simple Microservice in Minutes with Akamai EdgeWorkers

February 25, 2020 · by Ted Shuter ·

Serverless functions are an ideal alternative to VMs or containers for microservices, as they are easy to create, less risky than VMs, and faster than containers.

With Akamai’s EdgeWorkers, serverless functions run directly on the Edge, where users enter the Internet. This co-locates serverless computing with the web content consumers need, which enables massive scale and unparalleled performance. 

EdgeWorkers is available today in our beta program. We are already seeing a number of serverless microservice examples, from building geolocation services (returning customized user locations) to meeting data services (returning meeting data from key identifiers).

This quick how-to guide lists the steps needed to quickly build a microservice using EdgeWorkers serverless computing, and demonstrates the simplicity and value for developers.

Serverless functions on the Akamai Edge

Without Edge computing, web developers can run logic as a service on their infrastructure of choice or push it down to clients as javascript on the page. Both have their drawbacks when it comes to performance and security.

By moving the logic out of the client and to the edge, you can provide a consistent experience across the vast set of devices and hide both the logic and data from users.

With cloud services, there will need to be another hop regardless of the architecture employed. Even serverless solutions, which are located strategically in large cloud centers, can introduce significant round-trip latency depending on the location of the user. 

graphic showing edge network and microservice replicated to thousands of servers

*assumes 20ms to Edge from client and 120ms across the internet; 95% of EdgeWorkers to execute in less than 50ms, with most less than 10ms

Edge Data

EdgeWorkers provides a number of data layer options. For simple services, you have full access to the request and delivery insights; for more complex microservices, you can leverage data embedded in the function or remote data solutions (which should also be deployed to the Edge).

Within the request, functions can access all of the data from the http request, including cookies, headers, URL, query parameters. Additionally, you can get more insight from the delivery layer which adds location and device information. For example, you could create a service which determines the importance of a customer, whether a request is considered a VIP based on country, device type, or cookie values.

Additional data can also be embedded in the function or included as part of the uploaded bundle. Later this year, EdgeWorkers will support data fetched from remote systems. Ideally the data is also co-located or deployed in edge data solution to realize the performance improvements. A few considerations worth mentioning, there is a limit to the amount of data each function can include; see the limits sections in the user guide, for memory and bundle limitations. 

Steps

So, you are ready to create your first edge microservice! Here are the steps to create an EdgeWorker. If you aren’t already in the beta program, head over to developer.akamai.com/edgeworkers and sign up.

  1. Create the code bundle: Provide the code for the microservice in the main.js and build the bundle for upload.

  2. Activate the code on Edge: Once you've created the code bundle you can use the EdgeWorkers Management application to upload your Hello World code sample on the Edge.

  3. Add EdgeWorkers to your delivery configuration: Your Akamai administrator can enable the EdgeWorkers behavior in Property Manager, and define which requests invoke the EdgeWorker.

  4. Test your new Microservice: Make a request to test the EdgeWorker.

For more details on these steps, see the getting started with code examples guide.

Create the code bundle

  1. Go to the Akamai EdgeWorkers GitHub repository and download the main.js and bundle.json files from the Hello World project or create them using the values below.

  2. Use the Hello World code sample to create the JavaScript source in a file called main.js.

// Hello World Example

 

export function onClientRequest(request) {

  request.respondWith(

      200, {},

      '<html><body><h1>Hello World From Akamai EdgeWorkers</h1></body></html>');

}

  1. Set the edgeworker-version in the bundle.json file.

  2. Compress the files into a code bundle.

Activate the code on Edge

  1. Log in to Control Center and go to Edge functions app.

  2. Click Create EdgeWorker ID

    Create Edgeworker ID

  3. Enter a name for the EdgeWorker ID.

  4. Click Create EdgeWorker ID.

  5. Select your new EdgeWorkers and click the Create version button.

    Edgeworkers Create Version

  6. Drag and drop the Hello World code bundle 

  7. Select your new EdgeWorker and click the Activate version button.

  8. Select the Staging network to test the Hello World code sample.

Add EdgeWorkers to your delivery configuration

  1. In Control Center Edit your Property

  2. Create or Modify an existing rule to choose a match criteria and scope.

    Edgeworkers use case

     

  3. Add EdgeWorkers in available behaviors.

    Add behaviors

  4. Change the setting to On.

  5. Select an EdgeWorker identifier from the list.

  6. Click Insert Behavior.

  7. Click the Activate tab.

  8. Click the Activate v<#> on Staging button.

Test your new microservice

Use the following curl request, resolved to staging, to test your new microservice.

For instructions on how to resolve an IP address refer to the Getting Started for HTTPS Properties documentation.

curl http://[your website name]/helloworld --resolve [your website name]:80:[your staging ip address]

The curl request should produce the following HTML output:

HTTP/1.1 200 OK

Content-Type: text/html

Content-Length: 70

Date: Wed, 20 Nov 2019 17:09:17 GMT

Connection: keep-alive

X-Hello-World: From Akamai EdgeWorkers

 

<html><body><h1>Hello World From Akamai EdgeWorkers</h1></body></html>

Next Steps

If you haven’t already started to use EdgeWorkers, head to https://developer.akamai.com/edgeworkers for more details and access to the beta program. This is just one area EdgeWorkers can help improve development velocity and performance. 

Stay tuned for our next post which will provide insight on how to optimally integrate serverless functions into your development workflows and optimize for success.