Fast Purge Cloud Function for GCP to purge specific akamai cache by "cache-tags"
Prequisistes: have NPM (node package manager) and NVM (node version manager) installed
- Run in terminal at root:
nvm use v10
andnpm install
- Copy
.env.example.yml
to.env.yml
and replace the values from API keys you get from AKAMAI with purge and admin permission. Note the env is either stage or prod. The service key is sent when making a post request to prevent spam or unauthorized requests - Run in terminal at root:
npm run test
You should see out like this:
{"detail": "Request accepted", "estimatedSeconds": 5, "purgeId": "XXXXXXXX-9a53-XXXXX-aa24-498812ce210a", "supportId": "17PY15XXXXXXXX567174-24900000056", "httpStatus": 201}
If you do not get that output try to issue new API keys. Make sure they have purge and admin permission. Also check the Akamai envirnoment you are testing against.
- Install gcloud sdk cli tools https://cloud.google.com/sdk/docs#install_the_latest_cloud_tools_version_cloudsdk_current_version
- Authenticate with google https://cloud.google.com/sdk/gcloud/reference/auth
gcloud functions deploy akamaiFastPurge --env-vars-file .env.yml --runtime nodejs10 --trigger-http --project YOUR-PROJECT-NAME
- Setup a POST request to
https://us-central1-zesty-dev.cloudfunctions.net/akamaiFastPurge
noteus-central1
will be your deploy location andzesty-dev
will be your GCP environement name - In that post request include a header
'X-Auth' : SERVICE_KEY
and a raw body:
{
"objects": [
"CACHE-KEY",
"ZUID"
]
}
Examples
Curl
curl --location --request POST 'https://us-central1-PROJECT-NAME.cloudfunctions.net/akamaiFastPurge' \
--header 'X-Auth: xxxxx' \
--header 'Content-Type: text/plain' \
--data-raw '{
"objects": [
"Foo",
"Bar"
]
}'
Vanilla Javascript
var myHeaders = new Headers();
myHeaders.append("X-Auth", "xxxxx");
myHeaders.append("Content-Type", "text/plain");
var raw = "{\n \"objects\": [\n \"Foo\",\n \"Bar\"\n ]\n }";
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow'
};
fetch("https://us-central1-PROJECT-NAME.cloudfunctions.net/akamaiFastPurge", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
Note
The request returns an Estimated Time to Purge value (EstimatedTime) upon 201, it has the value of 5 seconds. We have witnessed seconds to up to 10 minutes.
From a Akamai Representative at 4:18:07 PM on 5.19.2020
The SLA for FastPurge might go to sometimes up to 7-8mins In best scenario within 1 mins. It totally depend on the Purge server traffic.
Documentation: https://developer.akamai.com/api/core_features/fast_purge/v3.html#tagrequest
estimatedSeconds Integer The estimated number of seconds before the purge is to complete.
Fast Purge is a web interface available on the Control Center that lets you refresh specific cached objects or remove all objects by URLs, content provider (CP) codes, cache tags, and ARLs across the Akamai edge network in just a few seconds. This is extremely useful, especially in situations when you need to quickly correct mistakes in your published content. You can automate your content purge requests via the Fast Purge API.
Read more https://learn.akamai.com/en-us/webhelp/fast-purge/fast-purge/GUID-3A497865-28DF-4CAB-A507-F588F21368F8.html
For example, the token bucket for cache tags holds 5,000 tokens and refills at a rate of 500 tokens per minute. You can submit a burst of 5,000 cache tags if the token bucket is full, but this completely empties the token bucket. Cache tag tokens are refilled at 500 tokens per minute, so after one minute, the bucket has 500 tokens in it and another request with up to 500 objects can be processed.
https://developer.akamai.com/api/core_features/fast_purge/v3.html#ratelimits