在 AWS 上管理 SSL/TLS 憑證是確保應用程式安全通訊的關鍵環節。本文將深入探討如何使用 AWS Certificate Manager (ACM) 匯入憑證,並詳細說明不同 AWS 服務對憑證地區的特殊要求。
為什麼需要在 AWS 中正確管理 SSL/TLS 憑證
1. 確保資料傳輸安全
SSL/TLS 憑證是網路安全的基礎設施,提供以下關鍵保護:
- 加密通訊:防止中間人攻擊(MITM),保護敏感資料在傳輸過程中不被竊取
- 身份驗證:確認伺服器身份,防止使用者連接到偽造的網站
- 資料完整性:確保資料在傳輸過程中未被篡改
2. 符合法規與合規要求
多項法規要求企業必須使用 SSL/TLS 加密:
- PCI DSS:處理信用卡交易的網站必須使用 TLS 1.2 或更高版本
- GDPR:歐盟一般資料保護規範要求傳輸個人資料時使用加密
- HIPAA:美國醫療保險可攜性及責任法案要求保護健康資訊
3. AWS 服務的地區特殊性
AWS 不同服務對憑證地區有特定要求,原因包括:
- CloudFront 全球分發架構:CloudFront 是全球 CDN 服務,其控制平面位於 us-east-1,因此所有 CloudFront 憑證必須匯入該地區
- 地區性服務:ELB、API Gateway 等服務是地區性的,憑證需與服務部署於同一地區以降低延遲
- 災難復原:正確的憑證地區配置是多地區災難復原策略的基礎
一、匯入憑證的基本步驟
前置準備
在匯入憑證之前,需確保你有以下文件:
- 憑證主體(Certificate body):你的網域憑證,PEM 格式
- 憑證私鑰(Certificate private key):與憑證配對的私鑰,必須保密
- 憑證鏈(Certificate chain):中繼憑證和根憑證,建議提供以確保完整的信任鏈
步驟 1:確認目錄位置
確保在正確的目錄下工作,避免找不到檔案:
cd /Users/username/Downloads/ssl
ls -la
步驟 2:轉換憑證格式(如果需要)
AWS ACM 僅接受 PEM 格式憑證。如果你的憑證是 DER 格式(.cer 或 .der 副檔名),需要轉換:
# 轉換 DER 格式憑證為 PEM 格式
openssl x509 -inform der -in certfile.cer -out certfile.pem
# 驗證憑證內容
openssl x509 -in certfile.pem -text -noout
常見格式說明:
| 格式 | 副檔名 | 特性 | 轉換方式 |
|---|---|---|---|
| PEM | .pem, .crt, .key | Base64 編碼,包含 BEGIN/END 標記 | AWS 直接支援 |
| DER | .cer, .der | 二進位格式 | 需使用 openssl 轉換 |
| PKCS#7 | .p7b, .p7c | 包含憑證鏈 | openssl pkcs7 指令 |
| PKCS#12 | .pfx, .p12 | 包含憑證和私鑰 | openssl pkcs12 指令 |
步驟 3:建立完整憑證鏈
憑證鏈應包含中繼憑證和根憑證,順序很重要:
# 正確的順序:中繼憑證在前,根憑證在後
cat intermediate.crt root.crt > chain.pem
# 驗證憑證鏈完整性
openssl verify -CAfile chain.pem certfile.pem
憑證鏈驗證要點:
- 確保中繼憑證和根憑證來自同一 CA
- 檢查憑證有效期限(
notBefore和notAfter) - 確認憑證鏈中沒有過期的憑證
步驟 4:使用 AWS CLI 匯入憑證
方法一:使用 AWS 管理控制台
- 登入 AWS Console,選擇正確的地區
- 進入 AWS Certificate Manager (ACM)
- 點選「匯入憑證」
- 貼上憑證內容:
- Certificate body:複製
certfile.pem全部內容 - Certificate private key:複製私鑰全部內容
- Certificate chain:複製
chain.pem全部內容
- Certificate body:複製
- 點選「下一步」並檢閱
方法二:使用 AWS CLI(推薦,可自動化)
# 匯入憑證到 us-east-1(CloudFront 使用)
aws acm import-certificate
--certificate fileb://certfile.pem
--private-key fileb://private.key
--certificate-chain fileb://chain.pem
--region us-east-1
# 匯入憑證到其他地區(ELB 使用)
aws acm import-certificate
--certificate fileb://certfile.pem
--private-key fileb://private.key
--certificate-chain fileb://chain.pem
--region ap-northeast-1
自動化腳本範例:
#!/bin/bash
# 批次匯入憑證到多個地區
REGIONS=("us-east-1" "ap-northeast-1" "eu-west-1")
CERT_FILE="certfile.pem"
KEY_FILE="private.key"
CHAIN_FILE="chain.pem"
for region in "${REGIONS[@]}"; do
echo "Importing certificate to $region..."
aws acm import-certificate
--certificate fileb://$CERT_FILE
--private-key fileb://$KEY_FILE
--certificate-chain fileb://$CHAIN_FILE
--region $region
--tags Key=Environment,Value=Production
done
二、不同 AWS 服務對憑證的需求(完整版)
必須在 us-east-1 地區匯入憑證的服務
1. Amazon CloudFront
服務說明:
- CloudFront 是 AWS 的全球內容分發網絡 (CDN) 服務
- 用於分發靜態和動態內容到全球用戶,降低延遲
憑證要求:
- ⚠️ 所有用於 CloudFront 的自訂 SSL 憑證必須匯入到
us-east-1地區 - 這是 AWS 的固定要求,無法變更
- 即使你的 CloudFront distribution 服務全球用戶,憑證仍必須在 us-east-1
CloudFront SSL 配置範例:
# 1. 匯入憑證到 us-east-1
ACM_ARN=$(aws acm import-certificate
--certificate fileb://cert.pem
--private-key fileb://key.pem
--certificate-chain fileb://chain.pem
--region us-east-1
--query 'CertificateArn'
--output text)
# 2. 更新 CloudFront distribution 使用新憑證
aws cloudfront update-distribution
--id E1234EXAMPLE
--viewer-certificate
ACMCertificateArn=$ACM_ARN,SSLSupportMethod=sni-only,MinimumProtocolVersion=TLSv1.2_2021
2. Amazon API Gateway(Edge-Optimized 邊緣優化模式)
服務說明:
- API Gateway 的 Edge-Optimized 端點類型透過 CloudFront 分發 API 請求
- 適合服務全球用戶的 API
- 底層依賴 CloudFront 的邊緣節點
憑證要求:
- ⚠️ Edge-Optimized 自訂網域的憑證必須在
us-east-1地區 - 原因:Edge-Optimized API 使用 CloudFront 作為前端,繼承了 CloudFront 的憑證要求
- 重要:即使你的 API Gateway 建立在
ap-northeast-1,自訂網域憑證也必須在 us-east-1
配置範例:
# 1. 在 us-east-1 匯入憑證(Edge-Optimized 專用)
aws acm import-certificate
--certificate fileb://cert.pem
--private-key fileb://key.pem
--certificate-chain fileb://chain.pem
--region us-east-1
# 2. 在任意地區建立 API Gateway(例如 ap-northeast-1)
aws apigateway create-rest-api
--name "My API"
--endpoint-configuration types=EDGE
--region ap-northeast-1
# 3. 設定自訂網域(使用 us-east-1 的憑證)
aws apigateway create-domain-name
--domain-name api.example.com
--endpoint-configuration types=EDGE
--certificate-arn arn:aws:acm:us-east-1:123456789012:certificate/12345678-1234-1234-1234-123456789012
--region ap-northeast-1
如何判斷 API Gateway 類型:
# 檢查 API Gateway 端點類型
aws apigateway get-rest-api
--rest-api-id abc123def4
--region ap-northeast-1
--query 'endpointConfiguration.types'
--output text
# 輸出:EDGE(需要 us-east-1 憑證)
# 輸出:REGIONAL(需要本地區憑證)
3. AWS Amplify Console
服務說明:
- 用於建置和部署靜態網站和單頁應用程式(SPA)
- 底層使用 CloudFront 進行全球分發
憑證要求:
- ⚠️ 自訂網域憑證必須在
us-east-1地區 - Amplify Console 會自動整合 CloudFront,因此憑證要求與 CloudFront 相同
可在任意地區匯入憑證的服務
1. Elastic Load Balancing (ELB)
服務類型:
- Application Load Balancer (ALB):HTTP/HTTPS 流量,支援進階路由
- Network Load Balancer (NLB):TCP/UDP 流量,超高效能
- Classic Load Balancer (CLB):舊版負載均衡器,不建議新專案使用
憑證要求:
- 憑證必須匯入到負載均衡器所在的地區
- 範例:若 ALB 在
ap-northeast-1(東京),憑證也要在東京地區
# 匯入憑證到 ALB 所在地區
aws acm import-certificate
--certificate fileb://cert.pem
--private-key fileb://key.pem
--certificate-chain fileb://chain.pem
--region ap-northeast-1
# 將憑證附加到 ALB
aws elbv2 add-listener-certificates
--listener-arn arn:aws:elasticloadbalancing:ap-northeast-1:123456789012:listener/app/my-alb/50dc6c495c0c9188/f2f7dc8efc522ab2
--certificates CertificateArn=arn:aws:acm:ap-northeast-1:123456789012:certificate/12345678-1234-1234-1234-123456789012
2. Amazon API Gateway(Regional 區域性模式)
服務說明:
- API Gateway 的 Regional 端點類型僅在單一 AWS 地區提供服務
- 適合服務特定地區用戶的 API,或搭配自己的 CDN 使用
- 不透過 CloudFront,直接連接到地區性端點
憑證要求:
- ✅ Regional 自訂網域憑證必須與 API Gateway 在同一地區
- 範例:若 API 在
eu-west-1(愛爾蘭),憑證也要在 eu-west-1
配置範例:
# 1. 在 API 所在地區匯入憑證(例如 eu-west-1)
aws acm import-certificate
--certificate fileb://cert.pem
--private-key fileb://key.pem
--certificate-chain fileb://chain.pem
--region eu-west-1
# 2. 建立 Regional API Gateway
aws apigateway create-rest-api
--name "My Regional API"
--endpoint-configuration types=REGIONAL
--region eu-west-1
# 3. 設定自訂網域(使用同地區憑證)
aws apigateway create-domain-name
--domain-name api.example.com
--endpoint-configuration types=REGIONAL
--regional-certificate-arn arn:aws:acm:eu-west-1:123456789012:certificate/12345678-1234-1234-1234-123456789012
--region eu-west-1
⚠️ Edge-Optimized vs Regional 快速比較:
| 項目 | Edge-Optimized | Regional |
|---|---|---|
| 憑證地區 | 必須 us-east-1 |
API 所在地區 |
| 底層架構 | 使用 CloudFront 分發 | 直接連接地區端點 |
| 適用場景 | 全球用戶 | 特定地區用戶 |
| 延遲 | 全球低延遲 | 地區內低延遲 |
| 憑證檢查指令 | certificateArn |
regionalCertificateArn |
3. AWS Elastic Beanstalk
服務說明:
- 快速部署和擴展 Web 應用程式和服務
- 自動處理容量配置、負載平衡、自動擴展
憑證要求:
- 憑證應匯入到 Elastic Beanstalk 環境所在的地區
- Elastic Beanstalk 底層使用 ELB,因此憑證要求與 ELB 相同
4. Amazon EC2 實例(直接使用)
使用場景:
- 在 EC2 上執行 NGINX、Apache 等 Web 伺服器
- 直接在應用程式層處理 SSL/TLS
憑證處理:
- 不使用 ACM,憑證直接安裝在 EC2 實例上
- 使用傳統的 PEM 檔案配置到 Web 伺服器
- 無地區限制,憑證是檔案系統層級管理
# NGINX 範例配置
server {
listen 443 ssl;
server_name www.example.com;
ssl_certificate /etc/nginx/ssl/cert.pem;
ssl_certificate_key /etc/nginx/ssl/private.key;
ssl_trusted_certificate /etc/nginx/ssl/chain.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
}
5. AWS App Runner
服務說明:
- 全託管的容器化應用程式服務
- 自動處理負載平衡、擴展和 HTTPS
憑證要求:
- 自訂網域憑證應匯入到 App Runner 服務所在地區
- 支援的地區:us-east-1, us-west-2, eu-west-1, ap-northeast-1 等
6. AWS Transfer Family
服務說明:
- 全託管的 SFTP、FTPS、FTP 檔案傳輸服務
- 支援自訂網域名稱
憑證要求:
- 憑證必須匯入到 Transfer Family 伺服器所在的地區
混合架構的憑證配置
1. CloudFront + ELB(常見架構)
這是最常見的高可用架構,需要在兩個地區匯入憑證:
# 1. 匯入憑證到 us-east-1(CloudFront 使用)
aws acm import-certificate
--certificate fileb://cert.pem
--private-key fileb://key.pem
--certificate-chain fileb://chain.pem
--region us-east-1
# 2. 匯入相同憑證到 ELB 所在地區(例如 ap-northeast-1)
aws acm import-certificate
--certificate fileb://cert.pem
--private-key fileb://key.pem
--certificate-chain fileb://chain.pem
--region ap-northeast-1
架構說明:
- CloudFront 作為全球 CDN,使用 us-east-1 的憑證
- CloudFront 回源(origin)到 ELB,ELB 使用本地區憑證
- 終端用戶只看到 CloudFront 的憑證
- CloudFront 與 origin 之間也可以使用 HTTPS 加密(建議)
2. CloudFront + Regional API Gateway
這種架構也需要在兩個地區匯入憑證:
# 1. 匯入憑證到 us-east-1(CloudFront 使用)
aws acm import-certificate
--certificate fileb://cert.pem
--private-key fileb://key.pem
--certificate-chain fileb://chain.pem
--region us-east-1
# 2. 匯入憑證到 API Gateway 所在地區(例如 eu-west-1)
aws acm import-certificate
--certificate fileb://cert.pem
--private-key fileb://key.pem
--certificate-chain fileb://chain.pem
--region eu-west-1
為什麼需要兩個憑證:
- CloudFront 需要 us-east-1 的憑證用於自訂網域
- Regional API Gateway 需要本地區憑證用於自訂網域
- CloudFront 分發層使用一個憑證,API 端點層使用另一個憑證
3. 多地區災難復原架構
在多地區部署時,需要在每個地區匯入憑證:
#!/bin/bash
# 多地區憑證部署腳本
REGIONS=("us-east-1" "eu-west-1" "ap-northeast-1" "ap-southeast-1")
for region in "${REGIONS[@]}"; do
echo "Deploying certificate to $region..."
aws acm import-certificate
--certificate fileb://cert.pem
--private-key fileb://private.key
--certificate-chain fileb://chain.pem
--region $region
--tags Key=Environment,Value=Production Key=Region,Value=$region
echo "Certificate deployed to $region successfully"
done
echo "All certificates deployed. Summary:"
for region in "${REGIONS[@]}"; do
echo "$region: $(aws acm list-certificates --region $region --query 'CertificateSummaryList[0].CertificateArn' --output text)"
done
快速參考表:AWS 服務憑證地區要求
| AWS 服務 | 憑證地區要求 | 原因 |
|---|---|---|
| CloudFront | 必須 us-east-1 |
全球服務,控制平面在 us-east-1 |
| API Gateway (Edge-Optimized) | 必須 us-east-1 |
底層使用 CloudFront |
| Amplify Console | 必須 us-east-1 |
底層使用 CloudFront |
| API Gateway (Regional) | API 所在地區 | 地區性服務 |
| Application Load Balancer | ALB 所在地區 | 地區性服務 |
| Network Load Balancer | NLB 所在地區 | 地區性服務 |
| Elastic Beanstalk | 環境所在地區 | 使用地區性 ELB |
| App Runner | 服務所在地區 | 地區性服務 |
| Transfer Family | 伺服器所在地區 | 地區性服務 |
| EC2(直接使用) | 不使用 ACM | 憑證存於檔案系統 |
常見錯誤配置與解決方案
錯誤 1:API Gateway 類型搞混
錯誤範例:
# 建立 Edge-Optimized API 但使用 eu-west-1 憑證
aws apigateway create-domain-name
--domain-name api.example.com
--endpoint-configuration types=EDGE
--certificate-arn arn:aws:acm:eu-west-1:123456789012:certificate/xxx # ❌ 錯誤!
--region eu-west-1
錯誤訊息:
An error occurred (BadRequestException) when calling the CreateDomainName operation:
The certificate must be in us-east-1 Region.
正確做法:
# 選項 1:改用 Regional API
aws apigateway create-domain-name
--domain-name api.example.com
--endpoint-configuration types=REGIONAL # ✅ 改為 REGIONAL
--regional-certificate-arn arn:aws:acm:eu-west-1:123456789012:certificate/xxx
--region eu-west-1
# 選項 2:使用 us-east-1 憑證
aws apigateway create-domain-name
--domain-name api.example.com
--endpoint-configuration types=EDGE
--certificate-arn arn:aws:acm:us-east-1:123456789012:certificate/xxx # ✅ 使用 us-east-1
--region eu-west-1
錯誤 2:多地區部署時遺漏憑證
問題: 在 ap-northeast-1 建立 ALB,但憑證只匯入到 us-east-1
檢查方法:
# 列出所有地區的憑證
for region in us-east-1 eu-west-1 ap-northeast-1; do
echo "=== $region ==="
aws acm list-certificates --region $region --query 'CertificateSummaryList[].DomainName' --output table
done
解決方案:
# 補充匯入到缺少的地區
aws acm import-certificate
--certificate fileb://cert.pem
--private-key fileb://private.key
--certificate-chain fileb://chain.pem
--region ap-northeast-1
三、生成新的私鑰和 CSR(私鑰遺失處理)
什麼情況下需要重新生成私鑰
- 私鑰遺失:無法找到原始私鑰檔案
- 私鑰外洩:懷疑私鑰已被未授權人員取得
- 憑證過期:需要申請新憑證
- 安全性升級:從 RSA 2048 升級到 RSA 4096 或 ECC
步驟 1:生成新的私鑰
RSA 私鑰(傳統,相容性最佳):
# 生成 2048 位元 RSA 私鑰(最低建議)
openssl genpkey -algorithm RSA -out private.key -pkeyopt rsa_keygen_bits:2048
# 生成 4096 位元 RSA 私鑰(更高安全性,但效能稍差)
openssl genpkey -algorithm RSA -out private.key -pkeyopt rsa_keygen_bits:4096
# 為私鑰設定密碼保護(可選,建議)
openssl rsa -aes256 -in private.key -out private_encrypted.key
ECC 私鑰(現代,效能更佳):
# 生成 P-256 曲線私鑰
openssl ecparam -name prime256v1 -genkey -noout -out private_ecc.key
# 生成 P-384 曲線私鑰(更高安全性)
openssl ecparam -name secp384r1 -genkey -noout -out private_ecc.key
步驟 2:生成憑證簽名請求 (CSR)
# 生成 CSR
openssl req -new -key private.key -out request.csr
# 互動式填寫資訊:
# Country Name (2 letter code) [AU]: TW
# State or Province Name (full name) [Some-State]: Taipei
# Locality Name (eg, city) []: Taipei
# Organization Name (eg, company) [Internet Widgits Pty Ltd]: My Company Ltd
# Organizational Unit Name (eg, section) []: IT Department
# Common Name (e.g. server FQDN or YOUR name) []: www.example.com
# Email Address []: admin@example.com
使用配置檔自動化 CSR 生成:
# 建立 CSR 配置檔 (csr.conf)
cat > csr.conf <<EOF
[req]
default_bits = 2048
prompt = no
default_md = sha256
distinguished_name = dn
req_extensions = v3_req
[dn]
C = TW
ST = Taipei
L = Taipei
O = My Company Ltd
OU = IT Department
CN = www.example.com
[v3_req]
subjectAltName = @alt_names
[alt_names]
DNS.1 = www.example.com
DNS.2 = example.com
DNS.3 = api.example.com
EOF
# 使用配置檔生成 CSR
openssl req -new -key private.key -out request.csr -config csr.conf
步驟 3:驗證 CSR 內容
# 查看 CSR 詳細資訊
openssl req -text -noout -verify -in request.csr
# 確認 Common Name 和 Subject Alternative Names 正確
步驟 4:提交 CSR 給 CA 簽發新憑證
將生成的 request.csr 提交給你的憑證發行機構(CA):
- 商業 CA:DigiCert, GlobalSign, Sectigo 等
- 免費 CA:Let’s Encrypt(可使用 certbot 自動化)
- 企業內部 CA:公司自建的 PKI 系統
步驟 5:使用新憑證和私鑰匯入 AWS
新憑證簽發後,匯入 AWS:
aws acm import-certificate
--certificate fileb://new_certificate.pem
--private-key fileb://private.key
--certificate-chain fileb://chain.pem
--region us-east-1
安全性最佳實踐
1. 私鑰保護
- 嚴格限制存取權限:
# 設定私鑰檔案權限為僅所有者可讀
chmod 400 private.key
# 確認權限
ls -la private.key
# 輸出應為:-r-------- 1 user group 1675 Dec 10 10:00 private.key
- 加密儲存:私鑰應加密後儲存,使用 AWS Secrets Manager 或參數存放區
- 定期輪換:建議每 12-24 個月更新憑證和私鑰
- 絕不提交到版本控制:將
*.key、*.pem加入.gitignore
2. 使用 AWS Secrets Manager 管理私鑰
# 將私鑰儲存到 Secrets Manager
aws secretsmanager create-secret
--name production/ssl/private-key
--description "SSL private key for www.example.com"
--secret-string file://private.key
--region us-east-1
# 從 Secrets Manager 讀取私鑰
aws secretsmanager get-secret-value
--secret-id production/ssl/private-key
--query SecretString
--output text > private.key
3. 啟用 TLS 最佳實踐
- 使用 TLS 1.2 或更高版本:停用 TLS 1.0 和 1.1
- 選擇安全的加密套件:優先使用 ECDHE 和 AES-GCM
- 啟用 HSTS:強制瀏覽器使用 HTTPS
# CloudFront 設定範例
{
"MinimumProtocolVersion": "TLSv1.2_2021",
"SSLSupportMethod": "sni-only"
}
4. 監控憑證有效期限
# 檢查憑證到期日期
openssl x509 -in certificate.pem -noout -enddate
# 使用 AWS CLI 列出即將到期的憑證
aws acm list-certificates
--region us-east-1
--query 'CertificateSummaryList[?NotAfter<`2025-12-31`]'
常見問題 FAQ
1. 為什麼 CloudFront 必須使用 us-east-1 的憑證?
技術原因:
- CloudFront 是全球服務,其控制平面(control plane)位於 us-east-1
- 所有 CloudFront distribution 的配置都集中管理於 us-east-1
- 這是 AWS 的架構設計,確保全球一致性
實務影響:
- 即使你的用戶在亞洲,CloudFront 憑證仍必須在 us-east-1
- 如果誤將憑證匯入其他地區,CloudFront 將無法使用
2. 如何驗證憑證已正確匯入 AWS?
# 列出所有憑證
aws acm list-certificates --region us-east-1
# 查看特定憑證詳細資訊
aws acm describe-certificate
--certificate-arn arn:aws:acm:us-east-1:123456789012:certificate/12345678-1234-1234-1234-123456789012
--region us-east-1
# 檢查憑證驗證狀態
aws acm describe-certificate
--certificate-arn YOUR_CERTIFICATE_ARN
--query 'Certificate.Status'
--output text
3. 憑證匯入後可以刪除本地檔案嗎?
建議作法:
- ✅ 保留私鑰和憑證的加密備份,儲存在安全位置(如 AWS Secrets Manager)
- ✅ 可刪除工作目錄中的明文檔案,避免意外外洩
- ❌ 不要完全刪除所有備份,以防需要匯入到其他地區或服務
# 安全刪除本地私鑰(覆寫後刪除)
shred -u -n 3 private.key
# 或使用 srm(需安裝)
srm private.key
4. 如何處理憑證鏈不完整的錯誤?
錯誤訊息範例:
Certificate validation failed: The certificate chain is incomplete
解決方法:
- 確認憑證鏈順序:中繼憑證在前,根憑證在後
- 從 CA 下載完整鏈:某些 CA 會提供完整的 bundle 檔案
- 使用線上工具驗證:SSL Labs Server Test 可檢查憑證鏈
# 使用 OpenSSL 驗證憑證鏈
openssl verify -CAfile chain.pem -untrusted intermediate.crt certificate.pem
# 輸出應為:certificate.pem: OK
5. ACM 自動續期和匯入憑證有什麼差別?
| 項目 | ACM 簽發的憑證 | 匯入的憑證 |
|---|---|---|
| 費用 | 免費 | 需向 CA 購買 |
| 自動續期 | ✅ 自動 | ❌ 手動更新 |
| 私鑰匯出 | ❌ 無法匯出 | ✅ 可保留本地副本 |
| 使用範圍 | 僅限 AWS 服務 | 可用於 AWS 內外 |
| 網域驗證 | DNS 或 Email | CA 依其流程驗證 |
選擇建議:
- ✅ 優先使用 ACM 簽發:若所有服務都在 AWS 上,使用 ACM 最省力
- ✅ 匯入憑證適用情境:需要在 AWS 外部使用同一憑證、企業有統一 CA、需要 EV 憑證等
6. 如何在多個 AWS 帳戶間共用憑證?
問題: ACM 憑證無法直接在不同 AWS 帳戶間共用。
解決方案:
- 使用匯入憑證方式:在每個帳戶中匯入相同的憑證和私鑰
- 使用 AWS Secrets Manager 跨帳戶分享:
# 帳戶 A:建立 Secret 並授權帳戶 B 存取
aws secretsmanager put-resource-policy
--secret-id production/ssl/private-key
--resource-policy '{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {"AWS": "arn:aws:iam::ACCOUNT_B_ID:root"},
"Action": "secretsmanager:GetSecretValue",
"Resource": "*"
}]
}'
# 帳戶 B:讀取帳戶 A 的 Secret
aws secretsmanager get-secret-value
--secret-id arn:aws:secretsmanager:us-east-1:ACCOUNT_A_ID:secret:production/ssl/private-key
故障排查指南
問題 1:憑證匯入失敗 – 「格式錯誤」
可能原因:
- 憑證不是 PEM 格式
- 檔案包含額外的空白行或字元
- BEGIN/END 標記不完整
解決方法:
# 檢查憑證格式
cat certificate.pem
# 確保憑證以此格式呈現:
# -----BEGIN CERTIFICATE-----
# [Base64 編碼內容]
# -----END CERTIFICATE-----
# 清除多餘空白
sed '/^$/d' certificate.pem > certificate_clean.pem
問題 2:CloudFront 無法使用憑證 – 「憑證不在 us-east-1」
解決方法:
# 確認憑證所在地區
aws acm list-certificates --region us-east-1
aws acm list-certificates --region ap-northeast-1
# 如果憑證在錯誤地區,需重新匯入到 us-east-1
aws acm import-certificate
--certificate fileb://cert.pem
--private-key fileb://key.pem
--certificate-chain fileb://chain.pem
--region us-east-1
問題 3:瀏覽器顯示「憑證不受信任」
可能原因:
- 憑證鏈不完整(最常見)
- 憑證已過期
- Common Name 或 SAN 與網域不符
診斷方法:
# 使用 OpenSSL 測試 SSL 連線
openssl s_client -connect www.example.com:443 -servername www.example.com
# 檢查憑證鏈深度(應為 2 或 3)
echo | openssl s_client -connect www.example.com:443 -servername www.example.com 2>/dev/null | grep -A 2 "Certificate chain"
總結
在 AWS 中正確管理 SSL/TLS 憑證需要理解不同服務的地區要求:
- ✅ CloudFront:憑證必須匯入到
us-east-1 - ✅ ELB、API Gateway、Elastic Beanstalk:憑證匯入到服務所在地區
- ✅ 混合架構:可能需要在多個地區匯入相同憑證
關鍵要點:
- 使用正確的憑證格式(PEM)
- 確保憑證鏈完整
- 妥善保護私鑰,使用 Secrets Manager 儲存
- 定期監控憑證有效期限
- 遵循 TLS 安全最佳實踐
透過本文的詳細指引,你應該能夠順利在 AWS 中匯入和管理 SSL/TLS 憑證。如有任何問題,建議參考 AWS 官方文件或聯繫 AWS 支援團隊。