Add EC2 Origin

Add EC2 Origin

In this step, you add the EC2 instance as a second origin and route /api* requests to it without caching.

  1. In the CloudFront console, open your distribution, select the Origins tab, then click Create origin.

Create CloudFront Distribution

  1. In Origin domain, enter the public DNS name of your EC2 instance. For Protocol, select HTTP only.

Create CloudFront Distribution

  1. Keep the other defaults and expand Additional settings.

Create CloudFront Distribution

  1. Set Keep-alive timeout to 60, then click Create origin.

Viewers still connect to CloudFront over HTTPS. Using HTTP between CloudFront and the origin avoids TLS overhead on that connection.

Create CloudFront Distribution

  1. The Origins list now shows the EC2 origin.

Create CloudFront Distribution

  1. Select the Behaviors tab and click Create behavior.

Create CloudFront Distribution

  1. Configure the behavior settings:
    • Path pattern: /api*
    • Origin and origin groups: select the EC2 origin you created
    • Viewer protocol policy: Redirect HTTP to HTTPS
    • Allowed HTTP methods: GET, HEAD, OPTIONS, PUT, POST, PATCH, DELETE

Create CloudFront Distribution

  1. Under Cache key and origin requests, keep Cache policy and origin request policy (recommended) and set:
    • Cache policy: CachingDisabled. CloudFront does not cache responses, which suits dynamic API content.
    • Origin request policy: AllViewer. CloudFront forwards all headers, cookies, and query strings from the viewer request to the origin.

Create CloudFront Distribution

  1. Leave Function associations empty and click Create behavior.

Create CloudFront Distribution

  1. The /api* behavior appears with precedence 0, above the Default (*) behavior. Wait until the distribution finishes deploying.

Create CloudFront Distribution