0Pricing
Terraform Infrastructure as Code · レッスン

EC2インスタンスのプロビジョニング

ネットワークやセキュリティグループの設定を含め、Amazon EC2仮想マシンをデプロイして管理するTerraform設定を記述します。

「EC2インスタンスのプロビジョニング」はCoddyKit上の無料Terraform Infrastructure as Codeレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはTerraform Infrastructure as Code学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Terraform Infrastructure as Codeコースには全4レッスンが含まれています。

TerraformでEC2インスタンスを使う

ようこそ。このレッスンでは、Terraformを使ってAmazon EC2仮想マシンをプロビジョニングおよび管理する方法を学びます。

EC2(Elastic Compute Cloud)は、クラウド上でスケーラブルな仮想サーバーを提供します。EC2にInfrastructure as Code(IaC)とTerraformを使用すると、サーバーをコードで定義でき、一貫性のある再現可能なデプロイを実現できます。

EC2インスタンスを定義する

TerraformでEC2インスタンスを表す基本的なリソースはaws_instanceです。少なくとも次の2つの主要な引数を指定する必要があります。

  • ami:使用するAmazon Machine Image(OSやソフトウェア)。
  • instance_type:ハードウェア構成(例:無料利用枠の対象となるインスタンスのt2.micro)。

基本的なaws_instanceブロックは次のようになります。

resource "aws_instance" "web_server" {
  ami           = "ami-053b04dcd42d5397e" # Example: Amazon Linux 2 in us-east-1
  instance_type = "t2.micro"

  tags = {
    Name = "MyWebServer"
  }
}

最初のEC2を起動する

ここまでの内容を組み合わせて、シンプルなEC2インスタンスを起動しましょう。この設定では、AWS providerとEC2インスタンスを定義します。

デプロイするには、これを.tfファイルとして保存し、ターミナルでterraform init、terraform plan、terraform applyを実行します。

provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "web_server" {
  ami           = "ami-053b04dcd42d5397e" # Amazon Linux 2 AMI for us-east-1
  instance_type = "t2.micro"

  tags = {
    Name = "MyBasicWebServer"
  }
}

EC2を保護する:Security Groups

Security Groupsは、EC2インスタンスの仮想ファイアウォールとして機能します。インスタンスに到達またはインスタンスから送信されるネットワークトラフィックのうち、インバウンド(ingress)とアウトバウンド(egress)のどちらを許可するかを制御します。

SSHやHTTPなど必要なサービスを許可しながら、不正アクセスからサーバーを保護するために、正しく設定することが重要です。

Security Groupを作成する

aws_security_groupリソースを使用してSecurity Groupを定義します。どこからでも(0.0.0.0/0)SSHアクセス(ポート22)を許可するingressルールを追加します。

Security Groupを配置する既存のvpc_idを指定する必要があります。

resource "aws_security_group" "allow_ssh" {
  name        = "allow_ssh_traffic"
  description = "Allow SSH inbound traffic"
  vpc_id      = "vpc-0123456789abcdef0" # IMPORTANT: Replace with YOUR VPC ID!

  ingress {
    description = "SSH from anywhere"
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1" # All protocols
    cidr_blocks = ["0.0.0.0/0"]
  }

  tags = {
    Name = "allow_ssh_sg"
  }
}

Security GroupをEC2に関連付ける

では、先ほど作成したSecurity Groupを使用するようにaws_instanceを更新しましょう。aws_security_group.allow_ssh.idを使用して、Security GroupのIDを参照します。

実行可能にするには、Security Group内のvpc_idをAWSアカウントの実際のVPC IDに置き換えてください。

provider "aws" {
  region = "us-east-1"
}

resource "aws_security_group" "allow_ssh" {
  name        = "allow_ssh_traffic_example"
  description = "Allow SSH inbound traffic"
  vpc_id      = "vpc-0123456789abcdef0" # IMPORTANT: Replace with YOUR VPC ID!

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_instance" "web_server" {
  ami             = "ami-053b04dcd42d5397e"
  instance_type   = "t2.micro"
  vpc_security_group_ids = [
    aws_security_group.allow_ssh.id
  ]

  tags = {
    Name = "WebServerWithSSH"
  }
}

EC2のネットワーク:VPCとサブネット

すべてのEC2インスタンスは、Virtual Private Cloud(VPC)と、そのVPC内の特定のサブネットに配置されます。VPCは分離された仮想ネットワークで、サブネットはVPCを論理的に分割したものです(例:パブリックまたはプライベート)。

デフォルトでは、EC2はデフォルトVPCやサブネットで起動する場合があります。より細かく制御して整理するために、通常はインスタンスが使用するサブネットを指定します。

基本的なネットワークを作成する

EC2のネットワークを完全に制御するため、最小構成のVPCとパブリックサブネットを定義しましょう。パブリックアクセスにはInternet Gateway、トラフィックの経路指定にはRoute Tableも必要です。

この完全な構成により、EC2がインターネットと通信できるようになります。

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
  tags = {
    Name = "main-vpc-lesson"
  }
}

resource "aws_subnet" "public" {
  vpc_id            = aws_vpc.main.id
  cidr_block        = "10.0.1.0/24"
  availability_zone = "us-east-1a"
  map_public_ip_on_launch = true # Instances in this subnet get a public IP
  tags = {
    Name = "public-subnet-lesson"
  }
}

resource "aws_internet_gateway" "gw" {
  vpc_id = aws_vpc.main.id
  tags = {
    Name = "main-gw-lesson"
  }
}

resource "aws_route_table" "public_rt" {
  vpc_id = aws_vpc.main.id

  route {
    cidr_block = "0.0.0.0/0" # Traffic to the internet
    gateway_id = aws_internet_gateway.gw.id
  }
  tags = {
    Name = "public-rt-lesson"
  }
}

resource "aws_route_table_association" "public_rt_assoc" {
  subnet_id      = aws_subnet.public.id
  route_table_id = aws_route_table.public_rt.id
}

カスタムネットワーク内のEC2

ここでは、カスタムVPC、サブネット、Security Group、EC2インスタンスのすべてを組み合わせます。EC2は指定したパブリックサブネットで起動し、自動的にパブリックIPを取得します。

この完全な設定により、EC2のデプロイを細かく制御でき、SSHでアクセスできるようになります。

provider "aws" {
  region = "us-east-1"
}

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
  tags = { Name = "main-vpc-lesson" }
}

resource "aws_subnet" "public" {
  vpc_id            = aws_vpc.main.id
  cidr_block        = "10.0.1.0/24"
  availability_zone = "us-east-1a"
  map_public_ip_on_launch = true
  tags = { Name = "public-subnet-lesson" }
}

resource "aws_internet_gateway" "gw" {
  vpc_id = aws_vpc.main.id
  tags = { Name = "main-gw-lesson" }
}

resource "aws_route_table" "public_rt" {
  vpc_id = aws_vpc.main.id
  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.gw.id
  }
  tags = { Name = "public-rt-lesson" }
}

resource "aws_route_table_association" "public_rt_assoc" {
  subnet_id      = aws_subnet.public.id
  route_table_id = aws_route_table.public_rt.id
}

resource "aws_security_group" "allow_ssh" {
  name        = "allow_ssh_lesson"
  description = "Allow SSH inbound traffic"
  vpc_id      = aws_vpc.main.id
  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_instance" "web_server" {
  ami             = "ami-053b04dcd42d5397e"
  instance_type   = "t2.micro"
  subnet_id       = aws_subnet.public.id
  vpc_security_group_ids = [
    aws_security_group.allow_ssh.id
  ]
  associate_public_ip_address = true
  tags = {
    Name = "FullWebServerLesson"
  }
}

EC2 構成チェック

Terraform で基本的な aws_instance リソースを定義する際に、次の引数のうち必須なのはどれですか?

振り返り:Terraform での EC2

よくできました!Terraform を使って Amazon EC2 インスタンスをプロビジョニングする方法を学びました。

  • aws_instance リソースを使って EC2 インスタンスを定義しました。
  • aws_security_group を使ってネットワークトラフィックを制御し、インスタンスを保護しました。
  • EC2 をホストするための基本的なカスタムネットワーク(VPC、サブネット、インターネットゲートウェイ、ルートテーブル)を設定しました。
  • これで、仮想サーバーをコードとしてデプロイおよび管理するための基礎が身につきました。

よくある質問

「EC2インスタンスのプロビジョニング」レッスンは無料ですか?

はい。「EC2インスタンスのプロビジョニング」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Terraform Infrastructure as Codeコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Terraform Infrastructure as Codeコースには全4レッスンが含まれています。

「EC2インスタンスのプロビジョニング」で何を学びますか?

ネットワークやセキュリティグループの設定を含め、Amazon EC2仮想マシンをデプロイして管理するTerraform設定を記述します。 ブラウザで直接実行するハンズオンコードでTerraform Infrastructure as Codeを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Terraform Infrastructure as Codeを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのTerraform Infrastructure as Codeは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「EC2インスタンスのプロビジョニング」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このTerraform Infrastructure as Codeレッスンでコードを書いて実行できますか?

はい。すべてのTerraform Infrastructure as Codeレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. AWS Providerの設定
  2. EC2インスタンスのプロビジョニング
  3. S3バケットとIAMの管理
  4. VPCとネットワークの設定
← Terraform Infrastructure as Codeに戻る