View a markdown version of this page

搭配原始伺服器交互 TLS 使用 CloudFront 函數 - Amazon CloudFront

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

搭配原始伺服器交互 TLS 使用 CloudFront 函數

CloudFront Functions 在邊緣提供輕量的無伺服器運算,以自訂內容交付。搭配 CloudFront Functions 使用原始伺服器交互 TLS 時,需要注意有關原始伺服器選取和操作的特定行為和限制。

支援的 CloudFront Functions 操作

CloudFront Functions 可以透過以下方式與啟用 mTLS 的原始伺服器互動:

updateRequestOrigin()

使用啟用 mTLS 的原始伺服器時,updateRequestOrigin() 函數支援有限的修改:

  • 在原始伺服器 mTLS 原始伺服器之間切換:您可以更新請求,以路由到使用原始伺服器 mTLS 的不同原始伺服器,前提是兩個原始伺服器都使用相同的用戶端憑證。這可讓您實作自訂路由邏輯,同時維持交互 TLS 身分驗證。透過 selectRequestOriginById()createRequestOriginGroup() APIs,支援切換使用不同憑證的原始伺服器。

  • 停用原始伺服器 mTLS:您可以透過在 函數mTLSConfig: 'off'中設定 ,從啟用 mTLS 的原始伺服器切換到非 mTLS 原始伺服器。這可讓您根據請求特性,有條件地停用交互 TLS 身分驗證。

範例:在具有相同憑證的原始伺服器 mTLS 原始伺服器之間切換

import cf from 'cloudfront'; function handler(event) { var request = event.request; // Route to different origin based on request path if (request.uri.startsWith('/api/v2')) { cf.updateRequestOrigin({ "domainName": "api-v2.example.com", "mTLSConfig": "inherit", // If no value is provided for mTLSConfig, it defaults to inherit // Both origins must use the same certificate }); } return request; }

範例:有條件停用原始伺服器 mTLS

import cf from 'cloudfront'; function handler(event) { var request = event.request; // Disable mTLS for specific paths if (request.uri.startsWith('/public')) { cf.updateRequestOrigin({ "domainName": "public-origin.example.com", "mTLSConfig": "off" }); } return request; }

selectRequestOriginById()

selectRequestOriginById() 函數支援選取已啟用交互 TLS (原始伺服器) 的原始伺服器。您可以使用此函數,將請求動態路由到分佈中設定的已啟用 mTLS 的原始伺服器。依 ID 選取啟用交互 TLS (原始伺服器) 的原始伺服器時,CloudFront 會使用分佈設定中針對該原始伺服器設定的用戶端憑證。

範例:依 ID 選取啟用交互 TLS (原始伺服器) 的原始伺服器

import cf from 'cloudfront'; function handler(event) { var request = event.request; // Select mTLS-enabled origin based on request characteristics if (request.uri.startsWith('/secure-api')) { cf.selectRequestOriginById("mtls-origin-1"); } return request; }

createRequestOriginGroup()

createRequestOriginGroup() 函數支援建立原始伺服器群組,其中包含啟用交互 TLS (原始伺服器) 的原始伺服器。您可以針對 CloudFront Functions 中的容錯移轉案例,使用啟用 mTLS 的原始伺服器動態建立原始伺服器群組。

範例:使用啟用交互 TLS (原始伺服器) 的原始伺服器建立原始伺服器群組

import cf from 'cloudfront'; function handler(event) { // Create origin group with mTLS-enabled primary and failover origins cf.createRequestOriginGroup({ "originIds": ["mtls-origin-primary", "mtls-origin-failover"], "failoverCriteria": { "statusCodes": [500, 502, 503, 504] } }); return event.request; }