title: DNS magic (AWS Route53) to expose S3 and EBeanStalk as single domain (to avoid CORS)
body: <p>We have static html pages hosted on S3, which talk to REST services hosted on EC2 (managed by Elastic Beanstalk). Because S3 and EB are different hosts, these are CORS requests. The backend server is setting the required headers to allow CORS:</p>
  
  <pre><code>    if(allowOrigin){
          response.setHeader("Access-Control-Allow-Origin", clientOrigin);
          response.setHeader("Access-Control-Allow-Credentials", "true");
      }
  </code></pre>
  
  <p>The problem is that the REST services use sessions, which means they send a session cookie back to the browser. But most browsers have "don't accept 3rd party cookies" by default, so this doesn't actually  work. We could pass the value of the session cookie as a POST parameter, but then we'd have to re-implement session management that JEE does for us a with a simple call to <code>request.getSession(true)</code>.</p>
  
  <p>As a solution, we are hoping for some DNS wizardry that would allow us to present both S3 and EB services as if they are the same domain. So for example, do something like this:</p>
  
  <pre><code>Request: mydomain.com/somePath/to/page --&gt; redirect to S3 bucket/somePath/to/page
  Request: mydomain.com/services/path/to/service --&gt; redirect to EB/path/to/service
  </code></pre>
  
  <p>So the redirect is conditional to /services/ being present as the root folder</p>
  
  <p>We'd prefer to do this at the DNS level rather than from within an S3 redirect, because Amazon charges fees for every request that hits S3 (so we'd be paying twice for all hits to /services/</p>
  
  <p>How can we achieve something like that? Other ideas welcome.</p>