AWS Application Load Balancer (ALB) の固定レスポンス機能を使用することで、メンテナンス時のSorryページを簡単に実装できます。
CloudFormationを使った実装例をご紹介します。
実装のポイント
ALBのListenerRuleで固定レスポンス(fixed-response)を設定することで、バックエンドサーバーを経由せずにHTMLレスポンスを返すことができます。
CloudFormation実装例
1. Sorryページ用のListenerRule
ListenerRuleSorryPage:
Type: 'AWS::ElasticLoadBalancingV2::ListenerRule'
Properties:
Actions:
- Type: fixed-response
FixedResponseConfig:
ContentType: text/html
StatusCode: 503
MessageBody: '<html lang="ja">...'
Conditions:
- Field: source-ip
SourceIpConfig:
Values:
- '0.0.0.0/0'
ListenerArn: !Ref Listener
Priority: 99
2. CloudWatch AlarmとEventBridgeによる自動化
ターゲットグループのヘルスチェック状態を監視し、アラーム状態に応じて自動切り替え:
UnhealthyHostsAlarm:
Type: 'AWS::CloudWatch::Alarm'
Properties:
AlarmName: 'ALB-TargetGroup-UnhealthyHosts'
MetricName: UnHealthyHostCount
Namespace: AWS/ApplicationELB
Statistic: Average
Period: 60
EvaluationPeriods: 2
Threshold: 1
ComparisonOperator: GreaterThanOrEqualToThreshold
Dimensions:
- Name: TargetGroup
Value: !GetAtt TargetGroupPrimary.TargetGroupFullName
- Name: LoadBalancer
Value: !GetAtt ALB.LoadBalancerFullName
# EventBridge用IAMロール
EventBridgeRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: events.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: StepFunctionsExecutionPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- states:StartExecution
Resource:
- !GetAtt SorryPageStateMachine.Arn
- !GetAtt NormalOperationStateMachine.Arn
# アラーム状態がALARMの時(Sorryページ有効化)
SorryPageEventRule:
Type: 'AWS::Events::Rule'
Properties:
Name: 'sorry-page-automation'
EventPattern:
source: ["aws.cloudwatch"]
detail-type: ["CloudWatch Alarm State Change"]
detail:
state:
value: ["ALARM"]
alarmName: ["ALB-TargetGroup-UnhealthyHosts"]
Targets:
- Arn: !GetAtt SorryPageStateMachine.Arn
Id: "SorryPageTarget"
RoleArn: !GetAtt EventBridgeRole.Arn
# アラーム状態がOKの時(通常運用復帰)
NormalOperationEventRule:
Type: 'AWS::Events::Rule'
Properties:
Name: 'normal-operation-automation'
EventPattern:
source: ["aws.cloudwatch"]
detail-type: ["CloudWatch Alarm State Change"]
detail:
state:
value: ["OK"]
alarmName: ["ALB-TargetGroup-UnhealthyHosts"]
Targets:
- Arn: !GetAtt NormalOperationStateMachine.Arn
Id: "NormalOperationTarget"
RoleArn: !GetAtt EventBridgeRole.Arn
3. Step Functionsによる動的制御
Sorryページの有効/無効を動的に切り替えるためのStep Functions:
メンテナンス開始用(Sorryページ有効化)
SorryPageStateMachine:
Type: 'AWS::StepFunctions::StateMachine'
Properties:
StateMachineName: 'sorry-page-handler'
DefinitionString: !Sub |
{
"StartAt": "SetRulePriorities HTTPS:443",
"States": {
"SetRulePriorities HTTPS:443": {
"Type": "Task",
"Parameters": {
"RulePriorities": [
{
"RuleArn": "${ListenerRuleSorryPage.RuleArn}",
"Priority": 1
}
]
},
"Resource": "arn:aws:states:::aws-sdk:elasticloadbalancingv2:setRulePriorities",
"End": true
}
}
}
通常運用復帰用(Sorryページ無効化)
NormalOperationStateMachine:
Type: 'AWS::StepFunctions::StateMachine'
Properties:
StateMachineName: 'normal-operation-handler'
DefinitionString: !Sub |
{
"StartAt": "SetRulePriorities HTTPS:443",
"States": {
"SetRulePriorities HTTPS:443": {
"Type": "Task",
"Parameters": {
"RulePriorities": [
{
"RuleArn": "${ListenerRuleSorryPage.RuleArn}",
"Priority": 99
}
]
},
"Resource": "arn:aws:states:::aws-sdk:elasticloadbalancingv2:setRulePriorities",
"End": true
}
}
}
メリット
- シンプル:バックエンドサーバー不要でHTMLレスポンスを返せる
- 高可用性: ALB自体の可用性に依存するため、安定したSorryページ表示が可能
- 動的制御:Step Functionsでルールの優先度を変更し、メンテナンスモードの切り替えが可能
- コスト効率:追加のEC2インスタンスやS3ホスティング不要
デメリット
- 文字数制限:1,024文字までの制限により、リッチなデザインや詳細な情報を含められない
- 静的コンテンツのみ: JavaScript等の動的な機能は使用できない
- 画像・CSS制限:外部リソースの読み込みに制約があり、インライン記述が必要
- カスタマイズ性:複雑なレイアウトやブランディング要素の実装が困難
- SEO対応:メタタグやOGPタグなどの詳細なSEO設定が制限される
運用パターン
手動運用
2つのStep Functionsを使い分けて運用します:
- メンテナンス開始時:SorryPageStateMachineを実行してPriority 1(高優先度)に変更
- メンテナンス終了時:NormalOperationStateMachineを実行してPriority 99(低優先度)に戻す
自動運用
1つのCloudWatch Alarmで完全自動化:
- 障害検知:アラーム状態が"ALARM"になったらSorryページを自動有効化
- 復旧検知:アラーム状態が"OK"になったら通常運用に自動復帰
- シンプル構成:1つのアラームで障害発生から復旧まで対応
- 即座対応:サーバー障害時に数分以内でSorryページを表示
この仕組みにより、手動メンテナンスと緊急時の自動対応の両方に対応できます。
制限事項
文字数制限
ALBの固定レスポンスは最大1,024文字までのため、シンプルなSorryページのみ作成可能です。
複雑なデザインや大量のコンテンツには向きません。
注意
MessageBodyは1行で記述する必要があります。
改行やインデントを含む場合は適切にエスケープしてください。

