I answered my own question a while ago. So decided to share here.
<?php
//debug off by default
error_reporting(0);
//cdn fallback
if (!isset($_GET['aws_cdn'])) {
$cdn_fallback = curl_init('http://123.456.789/'); //Set to CDN IP or URL
curl_setopt($cdn_fallback, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cdn_fallback, CURLOPT_NOSIGNAL, 1);
curl_setopt($cdn_fallback, CURLOPT_TIMEOUT_MS, 400); //How many milliseconds before fallback to local storage
$data = curl_exec($cdn_fallback);
$curl_errno = curl_errno($cdn_fallback);
$curl_error = curl_error($cdn_fallback);
curl_close($cdn_fallback);
if ($curl_errno > 0) { ?>
<script type="text/javascript" src="http://example.com/js/jquery.js"></script>
<?php } else { ?>
<script type="text/javascript" src="http://123.456.789/cdn/js/jquery.js"></script>
<?php }} ?>