Welcome to Tech Athletes | テック・アスリート   Click to listen highlighted text! Welcome to Tech Athletes | テック・アスリート

【2026年最新】AWS完全入門ガイド|EC2・S3・RDS・Lambda・ECSを実践コードで徹底解説

AWS(Amazon Web Services)は世界最大のクラウドプラットフォームです。2026年現在、市場シェア約33%を占め、エンジニアにとって必須スキルとなっています。本記事では、AWSの主要サービスを実践的なコードと共に解説します。

AWSアカウント作成と初期設定

セキュリティの鉄則:rootアカウントを使わない

AWSアカウント作成後は、必ずIAMユーザーを作成してrootアカウントでの通常作業を避けてください。rootアカウントは「鍵のかかった金庫」として保管するイメージです。

# AWS CLIのインストール(macOS)
brew install awscli

# AWS CLIの設定
aws configure
# AWS Access Key ID: [IAMユーザーのアクセスキー]
# AWS Secret Access Key: [シークレットキー]
# Default region name: ap-northeast-1  # 東京リージョン
# Default output format: json

# 設定確認
aws sts get-caller-identity

EC2(Elastic Compute Cloud)完全ガイド

EC2はAWSの仮想サーバーサービスです。数分でLinuxサーバーを立ち上げられます。

# AWS CLIでEC2インスタンスを起動
aws ec2 run-instances   --image-id ami-0d52744d6551d851e   # Amazon Linux 2023
  --instance-type t3.micro   --key-name my-key-pair   --security-group-ids sg-xxxxxxxx   --subnet-id subnet-xxxxxxxx   --count 1   --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=WebServer}]'

# インスタンス一覧表示
aws ec2 describe-instances   --query 'Reservations[].Instances[].[InstanceId,State.Name,PublicIpAddress,Tags[?Key==`Name`].Value|[0]]'   --output table

# SSHで接続
ssh -i ~/.ssh/my-key-pair.pem ec2-user@[public-ip]

# インスタンスの停止・終了
aws ec2 stop-instances --instance-ids i-xxxxxxxxxxxxxxxxx
aws ec2 terminate-instances --instance-ids i-xxxxxxxxxxxxxxxxx

S3(Simple Storage Service)完全ガイド

S3はスケーラブルなオブジェクトストレージです。静的ウェブサイトホスティング・バックアップ・データレイクなど多様な用途で使われます。

# バケット作成
aws s3 mb s3://my-tech-athletes-bucket-2026 --region ap-northeast-1

# ファイルのアップロード
aws s3 cp ./index.html s3://my-tech-athletes-bucket-2026/
aws s3 sync ./build/ s3://my-tech-athletes-bucket-2026/ --delete

# バケットの内容一覧
aws s3 ls s3://my-tech-athletes-bucket-2026/

# 静的ウェブサイトホスティング設定
aws s3 website s3://my-tech-athletes-bucket-2026/   --index-document index.html   --error-document error.html

# Node.jsからS3を操作(AWS SDK v3)
import { S3Client, PutObjectCommand, GetObjectCommand } from "@aws-sdk/client-s3";

const client = new S3Client({ region: "ap-northeast-1" });

// ファイルアップロード
const uploadFile = async (key: string, body: Buffer | string) => {
  const command = new PutObjectCommand({
    Bucket: "my-tech-athletes-bucket-2026",
    Key: key,
    Body: body,
    ContentType: "image/jpeg"
  });
  return await client.send(command);
};

Lambda(サーバーレス)完全ガイド

AWS Lambdaはサーバーレスコンピューティングサービスです。コードを実行した時間分だけ課金される、非常にコスト効率が高いサービスです。

// Lambda関数のサンプル(Node.js 20.x)
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';

export const handler = async (
  event: APIGatewayProxyEvent
): Promise<APIGatewayProxyResult> => {
  const { httpMethod, path, body } = event;
  
  console.log(`${httpMethod} ${path}`);
  
  try {
    // ビジネスロジック
    const data = body ? JSON.parse(body) : {};
    
    return {
      statusCode: 200,
      headers: {
        'Content-Type': 'application/json',
        'Access-Control-Allow-Origin': '*'
      },
      body: JSON.stringify({
        message: 'Success',
        data,
        timestamp: new Date().toISOString()
      })
    };
  } catch (error) {
    return {
      statusCode: 500,
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ message: 'Internal Server Error' })
    };
  }
};
# Lambdaのデプロイ(SAMを使用)
# template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Globals:
  Function:
    Timeout: 30
    MemorySize: 512
    Runtime: nodejs20.x
    Architectures:
      - arm64  # Graviton2で約20%コスト削減

Resources:
  ApiFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: dist/index.handler
      Events:
        Api:
          Type: Api
          Properties:
            Path: /{proxy+}
            Method: ANY
      Environment:
        Variables:
          NODE_ENV: production

# デプロイコマンド
sam build
sam deploy --guided

RDS(Relational Database Service)

RDSはマネージドリレーショナルデータベースサービスです。MySQL・PostgreSQL・Aurora等をフルマネージドで利用できます。

# RDS PostgreSQLインスタンス作成
aws rds create-db-instance   --db-instance-identifier tech-athletes-db   --db-instance-class db.t3.micro   --engine postgres   --engine-version 16.1   --master-username admin   --master-user-password SecurePassword123!   --allocated-storage 20   --storage-type gp3   --vpc-security-group-ids sg-xxxxxxxxxx   --db-subnet-group-name my-subnet-group   --no-publicly-accessible   --backup-retention-period 7   --multi-az

AWSの月額コスト最適化テクニック

AWSの料金を最小化するためのコスト最適化テクニックを紹介します。

  • Savings Plans / Reserved Instances:1〜3年コミットで最大72%割引
  • Spot Instances:バッチ処理・CI/CDには最大90%割引のスポットインスタンス
  • Graviton(ARM)インスタンス:同性能でx86比20〜40%コスト削減
  • Auto Scaling:負荷に応じて自動スケール、無駄なインスタンスを排除
  • S3 Intelligent-Tiering:アクセス頻度に応じて自動でストレージクラスを最適化
  • AWS Cost Anomaly Detection:コスト急増を自動検知してアラート

AWS認定資格でキャリアアップ

AWS認定資格はクラウドエンジニアとしての市場価値を大幅に高めます。おすすめの取得順序:

  1. AWS Cloud Practitioner(入門・3〜4週間)
  2. AWS Solutions Architect Associate(3〜4ヶ月)
  3. AWS Developer Associate(2〜3ヶ月)
  4. AWS DevOps Engineer Professional(上級・6ヶ月〜)

まとめ

AWSはエンジニアの必須スキルです。EC2・S3・Lambda・RDSの基本を押さえたら、次はECS・EKS・CloudFormation・Terraformと学習を進めましょう。AWS認定資格の取得もキャリアアップに非常に有効です。

投稿者 kasata

IT企業でエンジニアとして勤務後、テクノロジー情報メディア「Tech Athletes(テック・アスリート)」を運営。プログラミング、クラウドインフラ(AWS/GCP/Azure)、AI活用、Webサービス開発を専門とする。エンジニア・ビジネスパーソン向けに、実際に使ってみた経験をもとに信頼できる技術情報を発信中。資格:AWS認定ソリューションアーキテクト、Python 3 エンジニア認定試験合格。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

Click to listen highlighted text!