EC2完全ガイド
EC2 (Elastic Compute Cloud) 完全ガイド
Section titled “EC2 (Elastic Compute Cloud) 完全ガイド”EC2は、AWSの仮想サーバーサービスです。実務で使える実装例とベストプラクティスを詳しく解説します。
1. EC2とは
Section titled “1. EC2とは”EC2の役割
Section titled “EC2の役割”EC2は、クラウド上で仮想サーバーを起動・管理するサービスです。必要な時に必要な分だけサーバーを利用できます。
ユーザー ↓EC2インスタンス(仮想サーバー) ├─ OS(Amazon Linux、Ubuntu、Windowsなど) ├─ アプリケーション └─ データなぜEC2が必要か
Section titled “なぜEC2が必要か”問題のある構成(物理サーバー):
# 問題: 物理サーバーの運用# 1. サーバーの購入(数週間かかる)# 2. データセンターへの設置# 3. OSのインストールと設定# 4. ネットワークの設定# 5. 24時間の監視とメンテナンス
# 問題点:# 1. 初期コストが高い# 2. スケーリングが困難(物理的な制約)# 3. 運用コストが高い# 4. 柔軟性が低い解決: EC2による仮想サーバー
# 解決: EC2インスタンスの起動aws ec2 run-instances \ --image-id ami-0c55b159cbfafe1f0 \ --instance-type t3.micro \ --key-name my-key \ --security-group-ids sg-12345678 \ --subnet-id subnet-12345678
# メリット:# 1. 初期コストが低い(従量課金)# 2. 簡単にスケーリング(数分で起動・停止)# 3. 運用コストが低い(AWSが管理)# 4. 高い柔軟性(必要な時に必要な分だけ)2. EC2インスタンスタイプ
Section titled “2. EC2インスタンスタイプ”インスタンスファミリー
Section titled “インスタンスファミリー”汎用インスタンス
Section titled “汎用インスタンス”t3/t4g: バースト可能な汎用インスタンス- 用途: Webサーバー、開発環境- 特徴: CPUクレジットでバースト可能- 例: t3.micro, t3.small, t3.medium
m5/m6i: 汎用インスタンス- 用途: アプリケーションサーバー、データベース- 特徴: CPUとメモリのバランスが良い- 例: m5.large, m5.xlargeコンピューティング最適化
Section titled “コンピューティング最適化”c5/c6i: コンピューティング最適化- 用途: バッチ処理、ゲームサーバー- 特徴: 高いCPU性能- 例: c5.large, c5.xlargeメモリ最適化
Section titled “メモリ最適化”r5/r6i: メモリ最適化- 用途: インメモリデータベース、ビッグデータ処理- 特徴: 大量のメモリ- 例: r5.large, r5.xlargeストレージ最適化
Section titled “ストレージ最適化”i3/i4i: ストレージ最適化- 用途: NoSQLデータベース、データウェアハウス- 特徴: 高速なローカルストレージ- 例: i3.large, i3.xlargeインスタンスタイプの選択
Section titled “インスタンスタイプの選択”# 用途別の選択例Webサーバー: t3.micro, t3.smallアプリケーションサーバー: m5.large, m5.xlargeデータベース: r5.large, r5.xlargeバッチ処理: c5.large, c5.xlarge3. AMI(Amazon Machine Image)
Section titled “3. AMI(Amazon Machine Image)”AMIは、EC2インスタンスを起動するためのテンプレートです。OS、アプリケーション、設定が含まれます。
# パブリックAMIの使用aws ec2 run-instances \ --image-id ami-0c55b159cbfafe1f0 \ # Amazon Linux 2023 --instance-type t3.micro
# カスタムAMIの作成aws ec2 create-image \ --instance-id i-1234567890abcdef0 \ --name "my-custom-ami" \ --description "Custom AMI with application"カスタムAMIの作成
Section titled “カスタムAMIの作成”# 1. ベースインスタンスを起動aws ec2 run-instances \ --image-id ami-0c55b159cbfafe1f0 \ --instance-type t3.micro
# 2. アプリケーションをインストールssh -i my-key.pem ec2-user@<instance-ip>sudo yum install -y nginxsudo systemctl enable nginx
# 3. AMIを作成aws ec2 create-image \ --instance-id i-1234567890abcdef0 \ --name "nginx-server-ami" \ --description "AMI with Nginx pre-installed"4. セキュリティグループ
Section titled “4. セキュリティグループ”セキュリティグループとは
Section titled “セキュリティグループとは”セキュリティグループは、EC2インスタンスへのトラフィックを制御する仮想ファイアウォールです。
基本的な設定
Section titled “基本的な設定”{ "SecurityGroupRules": [ { "IpProtocol": "tcp", "FromPort": 80, "ToPort": 80, "CidrIpv4": "0.0.0.0/0", "Description": "HTTP access from internet" }, { "IpProtocol": "tcp", "FromPort": 443, "ToPort": 443, "CidrIpv4": "0.0.0.0/0", "Description": "HTTPS access from internet" }, { "IpProtocol": "tcp", "FromPort": 22, "ToPort": 22, "CidrIpv4": "10.0.0.0/16", "Description": "SSH access from VPC" }, { "IpProtocol": "tcp", "FromPort": 3306, "ToPort": 3306, "SourceSecurityGroupId": "sg-12345678", "Description": "MySQL access from application servers" } ]}Terraformでの設定
Section titled “Terraformでの設定”resource "aws_security_group" "web" { name = "web-sg" description = "Security group for web servers" vpc_id = aws_vpc.main.id
ingress { description = "HTTP" from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] }
ingress { description = "HTTPS" from_port = 443 to_port = 443 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] }
ingress { description = "SSH from VPC" from_port = 22 to_port = 22 protocol = "tcp" cidr_blocks = [aws_vpc.main.cidr_block] }
egress { description = "Allow all outbound" from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] }
tags = { Name = "web-sg" }}5. ユーザーデータ
Section titled “5. ユーザーデータ”ユーザーデータとは
Section titled “ユーザーデータとは”ユーザーデータは、EC2インスタンス起動時に自動実行されるスクリプトです。
基本的な使用例
Section titled “基本的な使用例”#!/bin/bash# ユーザーデータスクリプト
# システムの更新yum update -y
# Nginxのインストールyum install -y nginx
# Nginxの起動と有効化systemctl start nginxsystemctl enable nginx
# HTMLファイルの作成echo "<h1>Hello from EC2</h1>" > /var/www/html/index.html
# カスタム設定の適用cat > /etc/nginx/conf.d/custom.conf <<EOFserver { listen 80; server_name _; root /var/www/html; index index.html;}EOF
# Nginxの再起動systemctl restart nginxCloudFormationでの設定
Section titled “CloudFormationでの設定”Resources: WebServer: Type: AWS::EC2::Instance Properties: ImageId: ami-0c55b159cbfafe1f0 InstanceType: t3.micro SecurityGroupIds: - !Ref WebSecurityGroup UserData: Fn::Base64: | #!/bin/bash yum update -y yum install -y nginx systemctl start nginx systemctl enable nginx echo "<h1>Hello from EC2</h1>" > /var/www/html/index.html6. EBS(Elastic Block Store)
Section titled “6. EBS(Elastic Block Store)”EBSは、EC2インスタンスに接続する永続的なブロックストレージです。
EBSボリュームタイプ
Section titled “EBSボリュームタイプ”gp3: 汎用SSD(推奨)- 用途: ほとんどのワークロード- 特徴: バランスの取れた性能とコスト- 例: 100GB, 500GB, 1TB
gp2: 汎用SSD(旧タイプ)- 用途: 汎用ワークロード- 特徴: gp3より高コスト
io1/io2: プロビジョンドIOPS SSD- 用途: 高I/Oワークロード(データベースなど)- 特徴: 高いIOPS性能- 例: 100GB, 1000 IOPS
st1: スループット最適化HDD- 用途: ビッグデータ、データウェアハウス- 特徴: 高いスループット
sc1: コールドHDD- 用途: アクセス頻度の低いデータ- 特徴: 低コストEBSスナップショット
Section titled “EBSスナップショット”# スナップショットの作成aws ec2 create-snapshot \ --volume-id vol-1234567890abcdef0 \ --description "Backup before update"
# スナップショットからボリュームを作成aws ec2 create-volume \ --snapshot-id snap-1234567890abcdef0 \ --availability-zone us-east-1a \ --volume-type gp3 \ --size 1007. オートスケーリング
Section titled “7. オートスケーリング”オートスケーリングとは
Section titled “オートスケーリングとは”オートスケーリングは、負荷に応じてEC2インスタンスを自動的に追加・削除する機能です。
Launch Template
Section titled “Launch Template”Resources: LaunchTemplate: Type: AWS::EC2::LaunchTemplate Properties: LaunchTemplateName: web-server-template LaunchTemplateData: ImageId: ami-0c55b159cbfafe1f0 InstanceType: t3.micro SecurityGroupIds: - !Ref WebSecurityGroup UserData: Fn::Base64: | #!/bin/bash yum update -y yum install -y nginx systemctl start nginx systemctl enable nginx IamInstanceProfile: Arn: !GetAtt InstanceProfile.ArnAuto Scaling Group
Section titled “Auto Scaling Group”Resources: AutoScalingGroup: Type: AWS::AutoScaling::AutoScalingGroup Properties: AutoScalingGroupName: web-asg MinSize: 2 MaxSize: 10 DesiredCapacity: 2 VPCZoneIdentifier: - !Ref PublicSubnet1 - !Ref PublicSubnet2 LaunchTemplate: LaunchTemplateId: !Ref LaunchTemplate Version: !GetAtt LaunchTemplate.LatestVersionNumber TargetGroupARNs: - !Ref TargetGroup HealthCheckType: ELB HealthCheckGracePeriod: 300 Tags: - Key: Name Value: WebServer PropagateAtLaunch: trueスケーリングポリシー
Section titled “スケーリングポリシー”Resources: # CPU使用率ベースのスケーリング CPUScalingPolicy: Type: AWS::AutoScaling::ScalingPolicy Properties: AutoScalingGroupName: !Ref AutoScalingGroup PolicyType: TargetTrackingScaling TargetTrackingScalingPolicyConfiguration: TargetValue: 70.0 PredefinedMetricSpecification: PredefinedMetricType: ASGAverageCPUUtilization
# リクエスト数ベースのスケーリング RequestCountScalingPolicy: Type: AWS::AutoScaling::ScalingPolicy Properties: AutoScalingGroupName: !Ref AutoScalingGroup PolicyType: TargetTrackingScaling TargetTrackingScalingPolicyConfiguration: TargetValue: 1000.0 PredefinedMetricSpecification: PredefinedMetricType: ALBRequestCountPerTarget ResourceLabel: !Sub "${LoadBalancerFullName}/${TargetGroupFullName}"8. Elastic Load Balancer(ELB)
Section titled “8. Elastic Load Balancer(ELB)”ELBは、複数のEC2インスタンスにトラフィックを分散するロードバランサーです。
Application Load Balancer(ALB)
Section titled “Application Load Balancer(ALB)”Resources: ApplicationLoadBalancer: Type: AWS::ElasticLoadBalancingV2::LoadBalancer Properties: Name: web-alb Type: application Scheme: internet-facing Subnets: - !Ref PublicSubnet1 - !Ref PublicSubnet2 SecurityGroups: - !Ref ALBSecurityGroup Listeners: - Protocol: HTTP Port: 80 DefaultActions: - Type: forward TargetGroupArn: !Ref TargetGroup
TargetGroup: Type: AWS::ElasticLoadBalancingV2::TargetGroup Properties: Name: web-targets Port: 80 Protocol: HTTP VpcId: !Ref VPC HealthCheckPath: /health HealthCheckIntervalSeconds: 30 HealthCheckTimeoutSeconds: 5 HealthyThresholdCount: 2 UnhealthyThresholdCount: 39. 実務でのベストプラクティス
Section titled “9. 実務でのベストプラクティス”パターン1: 高可用性構成
Section titled “パターン1: 高可用性構成”# マルチAZ構成AutoScalingGroup: Properties: VPCZoneIdentifier: - !Ref PublicSubnet1 # us-east-1a - !Ref PublicSubnet2 # us-east-1b - !Ref PublicSubnet3 # us-east-1c MinSize: 3 # 各AZに1つずつ MaxSize: 9 # 各AZに最大3つパターン2: コスト最適化
Section titled “パターン2: コスト最適化”# Spotインスタンスの使用LaunchTemplate: Properties: LaunchTemplateData: InstanceMarketOptions: MarketType: spot SpotOptions: MaxPrice: "0.05" SpotInstanceType: persistent InstanceInterruptionBehavior: stopパターン3: セキュリティ強化
Section titled “パターン3: セキュリティ強化”# IAMロールの使用(パスワードなし)LaunchTemplate: Properties: LaunchTemplateData: IamInstanceProfile: Arn: !GetAtt InstanceProfile.Arn SecurityGroupIds: - !Ref WebSecurityGroup10. よくある問題と解決策
Section titled “10. よくある問題と解決策”問題1: インスタンスに接続できない
Section titled “問題1: インスタンスに接続できない”原因:
- セキュリティグループでポートが開いていない
- キーペアが正しく設定されていない
- パブリックIPが割り当てられていない
解決策:
# セキュリティグループの確認aws ec2 describe-security-groups --group-ids sg-12345678
# インスタンスの状態確認aws ec2 describe-instances --instance-ids i-1234567890abcdef0
# パブリックIPの確認aws ec2 describe-instances --instance-ids i-1234567890abcdef0 \ --query 'Reservations[0].Instances[0].PublicIpAddress'問題2: オートスケーリングが動作しない
Section titled “問題2: オートスケーリングが動作しない”原因:
- スケーリングポリシーが正しく設定されていない
- CloudWatchアラームが発火していない
- インスタンスがヘルスチェックに失敗している
解決策:
# オートスケーリンググループの状態確認aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names web-asg
# スケーリングアクティビティの確認aws autoscaling describe-scaling-activities --auto-scaling-group-name web-asg
# CloudWatchメトリクスの確認aws cloudwatch get-metric-statistics \ --namespace AWS/EC2 \ --metric-name CPUUtilization \ --dimensions Name=AutoScalingGroupName,Value=web-asg \ --start-time 2024-01-01T00:00:00Z \ --end-time 2024-01-01T23:59:59Z \ --period 300 \ --statistics Averageこれで、EC2の基礎知識と実務での使い方を理解できるようになりました。