Skip to content

Solution for listening for Snippet updates via a Lambda webhook and pushing new snippet JS to S3 origin

Notifications You must be signed in to change notification settings

optimizely-solutions/lambda-webhook-cloudfront

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 

Repository files navigation

UPDATE: This is no longer the preferred way to self-host with AWS

CloudFront supports custom origins, which will allow you to create a Distribution that points directly to cdn.optimizely.com. Guide coming soon.


Lambda Webhook for syncing Optimizely Snippet to S3

Solution for listening for Snippet updates via a Lambda webhook and pushing the updated Optimizely snippet to an S3 bucket. This facilitates the ability to self-host the Optimizely snippet in your CloudFront instance. The workflow is as follows:

The sample code uses Python, but can easily be ported over any language supported by AWS Lambda

  • An Optimizely user makes an update which triggers a Snippet update
  • Optimizely webhook hits the Lambda function's API endpoint URL
  • Lambda function is triggered, which:
    • Downloads JS snippet contents
    • Add/replaces the JS snippet in the specified S3 Bucket + path (Key)
  • The S3 bucket is fronted by CloudFront

Create new Lambda function

  • Visit Lambda console
  • Create from Blueprint
  • From list of Blueprints, choose microservice-http-endpoint (nodejs or python 3.6)
    • This will automatically set up an API gateway & set it as a trigger to your function
  • Name your function, e.g. "optimizelywebhook"
  • Assign to a (new) IAM role that has access to:
    • Amazon S3 (policy: AmazonS3FullAccess)
    • Amazon CloudWatch Logs (policy: AWSLambdaBasicExecutionRole or CloudWatchLogsFullAccess)

policies

  • In the Lambda Inline Code Editor, replace with this code. Notes on code:
    • You must replace "YOUR-AWS-S3-BUCKET-NAME" with your S3 bucket name
    • The second argument in s3.Object is the S3 Key. This is the path that the file will be created in your bucket.
    • There is no security behind this Webhook

Create Optimizely Webhook

  • Visit the Settings // Webhooks page in Optimizely
  • Create a new Webhook
  • Supply the URL that was generated automatically when the API Gateway trigger was created
  • The Lambda API endpoint URL can be found by clicking the button on the resource tree and scrolling towards the bottom

gateway button

  • You'll see a module that shows the API endpoint URL:

tree

Validating your setup

  • In the Lambda console, you'll see the following associations if IAM role is properly configured
    • Triggers (left side): API Gateway
    • Resources (right side): Amazon CloudWatch Logs, Amazon S3

tree

  • Test function within Lambda console
    • Create a test function that will allow you to manually trigger the Lambda function from within the console.
    • Build a request payload that looks identical to Optimizely's webhook requests.
    • Modify the following entities within your payload:
      • "body": "{\"timestamp\":1000000000,\"project_id\":PROJECTID,\"data\":{\"cdn_url\":\"https://cdn.optimizely.com/js/PROJECTID.js\",\"origin_url\":\"https://optimizely.s3.amazonaws.com/js/0PROJECTID.js\",\"revision\":1},\"event\":\"project.snippet_updated\"}"
      • "httpMethod": "POST"
    • After running the Test trigger, you'll see a verbose output that shows both (a) http response & (b) output to CloudWatchLog

success

Troubleshooting

  • CloudWatch Logs - Monitor the CloudWatch Logs for Lambda function calls. The log group will automatically be generated for your function, as long as it has the proper IAM role. This is located in the CloudWatch service
  • Manually dispatch Webhook using cURL on CLI
    • curl --data "{\"timestamp\":1000000000,\"project_id\":PROJECTID,\"data\":{\"cdn_url\":\"https://cdn.optimizely.com/js/PROJECTID.js\",\"origin_url\":\"https://optimizely.s3.amazonaws.com/js/0PROJECTID.js\",\"revision\":1},\"event\":\"project.snippet_updated\"}" https://UNIQUE.execute-api.us-east-1.amazonaws.com/default/FUNCTIONNAME
    • The payload should be the same as your Test payload in the Lambda console
    • You should see both
      1. A log entry for this function call
      2. The snippet showing up in your S3 destination

About

Solution for listening for Snippet updates via a Lambda webhook and pushing new snippet JS to S3 origin

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published