第12回 JAWS-UG札幌 勉強会 2014/01/17(金)

CloudFront
最近の事例と間違った使い方
株式会社データクラフト
大内 寛和
自己紹介
大内 寛和

株式会社データクラフト
・ソフトウェアエンジニア
・写真素材販売サイトの開発・保守
・社内インフラ担当
・受託開発(AWSを利用)
自社サービスにおけるAWSの利用状況
最近の事例
写真素材販売サイト imagenavi
サムネイルの表示が遅い・・・
オンプレミスで運用

DB1

Web1

DB2

Web2

File

サムネイル

Corporate Data center
で、CloudFront

DB1

Web1

DB2

Web2

File

サムネイル

Corporate Data center

CloudFront
サムネイルの表示が早い!
もちろん、費用はかかる
でも、メリット大
・オンプレサーバのメンテナンス、 チューニング

のことを考えなくてよい
・急激なトラフィック増に耐えられる

コスト削減、リスク軽減
間違った使い方
事象
・CloudFront(CF)がたまにエラーを返す・・・
ブラウザにはこんな表示が
The request could not be satisfied
・オリジンに接続できない (HTTP 504)
→ アクセス元のIPアドレス、ポートが制限されている
(オリジンのIPが引けない場合は、別のエラー)

・オリジンの応答が遅い (HTTP 504)
→ 応答に10秒以上かかっている(CFは3回リトライする)

・CNAMEsを設定していない (HTTP 403)
→ 独自ドメインでアクセスする場合、CFのディストリビューションに設定が必要

・ディストリビューションが非アクティブ (HTTP 403)
→ CFのディストリビューションが無効状態
サーバ構成

Web
Server
ユーザー

http://mysite.com/

CloudFront

http://org.mysite.com/

EC2
Subnet

ネットワークフィルタが正し
く設定されていない?
ネットワークフィルタのイメージ
Network ACL

IN:
httpを許可

Security Group

Web
Server

CloudFront

OUT:
49152-65535
を許可
接続元ポート
(Ephemeral port)

OUT:
全て許可
Subnet
VPC Network ACL 設定例
VPC Network ACL 設定例
Ephemeralポート
多くのLinuxカーネル(Amazon Linuxを含む):32768-61000
Windows XP、Windows Server 2003:1025-5000

Windows Vista、Windows 7、Windows Server 2008:49152-65535
Mac OS X、Mac OS X Server:49152-65535
FreeBSD 5.0:49152-65535
AIX、HP-UX:32768 - 65535
CFのEphemeralポートは?
多くのLinuxカーネル(Amazon Linuxを含む):32768-61000
Windows XP、Windows Server 2003:1025-5000

Windows Vista、Windows 7、Windows Server 2008:49152-65535
Mac OS X、Mac OS X Server:49152-65535
FreeBSD 5.0:49152-65535
AIX、HP-UX:32768 – 65535

32768以降に変更すれば・・・?
だめでした
デフォルトのNetwork ACLで、
CFのEphemeral portを確認
# netstat -al | grep serv

tcp

0

0 10.0.1.6:http

server-54-240-146-207:38071 TIME_WAIT

tcp

0

0 10.0.1.6:http

server-54-240-146-207:36867 TIME_WAIT

tcp

0

0 10.0.1.6:http

server-54-240-146-207:37525 TIME_WAIT

tcp

0

0 10.0.1.6:http

server-54-240-146-207:37024 TIME_WAIT

tcp

0

0 10.0.1.6:http

server-54-240-146-207:37711 TIME_WAIT

tcp

0

0 10.0.1.6:http

server-204-246-186-69.:

tcp

0

0 10.0.1.6:http

server-54-240-146-207:37914 TIME_WAIT

tcp

0

0 10.0.1.6:http

server-54-240-146-207:36712 TIME_WAIT

6730

TIME_WAIT
AWSのドキュメントをよく読む
Ephemeral Ports
The example network ACL in the preceding section uses an ephemeral port range of
49152-65535. However, you might want to use a different range for your network
ACLs. This section explains why.
The client that initiates the request chooses the ephemeral port range. The range
varies depending on the client's operating system. Many Linux kernels (including
the Amazon Linux kernel) use ports 32768-61000. Requests originating from Elastic

1024-65535

Load Balancing use ports
. Windows operating systems through
Windows Server 2003 use ports 1025-5000. Windows Server 2008 uses ports 4915265535. Therefore, if a request comes in to a web server in your VPC from a Windows
XP client on the Internet, your network ACL must have an outbound rule to enable
traffic destined for ports 1025-5000.
If an EC2 instance in your VPC is the client initiating a request, your network
ACL must have an inbound rule to enable traffic destined for the ephemeral ports
specific to the type of instance (Amazon Linux, Windows Server 2008, and so on.).
In practice, to cover the different types of clients that might initiate traffic
to public-facing instances in your VPC, you need to open ephemeral ports

1024-65535. However, you can also add rules to the ACL to deny traffic on
any malicious ports within that range. Make sure to place the DENY rules earlier
in the table than the rule that opens the wide range of ephemeral ports.
VPC Network ACLを変更

102465535
できた!
VPC Network ACL設定画面
Note: Network ACLs are stateless, which means
for any given request you want to handle, you
must create rules in both directions. For
example, to handle inbound traffic to a web
server in your VPC, you must allow both
inbound TCP port 80, and outbound TCP ports

1024-65535.
すいませんでした
エラーをカスタマイズ
CloudFrontは、エラーもキャッシュするので注意。
エラーのキャッシュ期間は、デフォルト300sec

カスタムエラーの設定で変更可。
様々なWebシステムと簡単に連携でき、高負荷に耐えられ
るCloudFrontは最高です。みなさんもすぐに利用しましょ
う!

AWSの間違った使い方、ハマった経験いろいろあると思い
ます。みなさんのそういった事例紹介を待ってます!

CloudFront最近の事例と間違った使い方