

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# Terraform プロバイダーについて
<a name="providers"></a>

Terraform では、*プロバイダー*はクラウドプロバイダー、サードパーティーツール、その他の APIs。で Terraform AWSを使用するには、 AWS リソースとやり取りする[AWS プロバイダー ](https://registry.terraform.io/providers/hashicorp/aws/latest/docs)を使用します。

[AWS CloudFormation レジストリ](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/registry.html)を使用してデプロイスタックにサードパーティーの拡張機能を組み込むことがない場合、Terraform [プロバイダー](https://developer.hashicorp.com/terraform/language/providers)は慣れている可能性があります。 CloudFormation は にネイティブであるため AWS、リソースの AWS プロバイダーはデフォルトで既に存在します。一方、Terraform には単一のデフォルトプロバイダーがないため、特定のリソースのオリジンについては何も想定できません。つまり、Terraform 設定ファイルで最初に宣言する必要があるのは、リソースがどこに向かっていて、どのようにそこに到達するかということです。

この区別により、 には存在しない Terraform の複雑さがさらに増します CloudFormation。ただし、その複雑さにより柔軟性が向上します。1 つの Terraform モジュール内で複数のプロバイダーを宣言でき、作成された基盤となるリソースは、同じデプロイレイヤーの一部として相互にやり取りできます。

これは、さまざまな方法で役立ちます。プロバイダーは必ずしも個別のクラウドプロバイダー用である必要はありません。プロバイダーは、クラウドリソースの任意のソースを表すことができます。例えば、Amazon Elastic Kubernetes Service (Amazon EKS) があるとします。Amazon EKS クラスターをプロビジョニングするときは、Helm チャートを使用してサードパーティーの拡張機能を管理し、Kubernetes 自体を使用してポッドリソースを管理できます。 AWS、[Helm ](https://registry.terraform.io/providers/hashicorp/helm/latest/docs)、および [Kubernetes](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs) にはそれぞれ独自の Terraform プロバイダーがあるため、これらのリソースをすべて同時にプロビジョニングして統合し、それらの間で値を渡すことができます。

Terraform の次のコード例では、 AWS プロバイダーは Amazon EKS クラスターを作成し、結果の Kubernetes 設定情報が Helm プロバイダーと Kubernetes プロバイダーの両方に渡されます。

```
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = ">= 4.33.0"
    }

    helm = {
      source  = "hashicorp/helm"
      version = "2.12.1"
    }

    kubernetes = {
      source  = "hashicorp/kubernetes"
      version = "2.26.0"
    }
  }
  required_version = ">= 1.2.0"
}

provider "aws" {
  region = "us-west-2"
}

resource "aws_eks_cluster" "example_0" {
  name     = "example_0"
  role_arn = aws_iam_role.cluster_role.arn
  vpc_config {
    endpoint_private_access = true
    endpoint_public_access  = true
    subnet_ids              = var.subnet_ids
  }
}

locals {
  host        = aws_eks_cluster.example_0.endpoint
  certificate = base64decode(aws_eks_cluster.example_0.certificate_authority.data)
}

provider "helm" {
  kubernetes {
    host                   = local.host
    cluster_ca_certificate = local.certificate
    # exec allows for an authentication command to be run to obtain user
    # credentials rather than having them stored directly in the file
    exec {
      api_version = "client.authentication.k8s.io/v1beta1"
      args        = ["eks", "get-token", "--cluster-name", aws_eks_cluster.example_0.name]
      command     = "aws"
    }
  }
}

provider "kubernetes" {
  host                   = local.host
  cluster_ca_certificate = local.certificate
  exec {
    api_version = "client.authentication.k8s.io/v1beta1"
    args        = ["eks", "get-token", "--cluster-name", aws_eks_cluster.example_0.name]
    command     = "aws"
  }
}
```

2 つの IaC ツールに関しては、プロバイダーに関するトレードオフがあります。Terraform は、デプロイを駆動するエンジンである外部に配置されたプロバイダーパッケージに完全に依存します。 は、すべての主要な AWS プロセスを CloudFormation 内部でサポートしています。では CloudFormation、サードパーティーの拡張機能を組み込む場合にのみ、サードパーティープロバイダーについて心配する必要があります。各アプローチには長所と短所があります。どちらが適しているかは、このガイドの範囲外ですが、両方のツールを評価する際には違いを覚えておくことが重要です。

## Terraform エイリアスの使用
<a name="using-terraform-aliases"></a>

Terraform では、各プロバイダーにカスタム設定を渡すことができます。同じモジュール内で複数のプロバイダー設定を使用する場合はどうなりますか？ その場合は、エイ[リアス](https://developer.hashicorp.com/terraform/language/providers/configuration#alias-multiple-provider-configurations) を使用する必要があります。  エイリアスは、リソースレベルまたはモジュールレベルごとに使用するプロバイダーを選択するのに役立ちます。同じプロバイダーのインスタンスが複数ある場合は、エイリアスを使用してデフォルト以外のインスタンスを定義します。例えば、デフォルトのプロバイダーインスタンスは特定の かもしれませんが AWS リージョン、エイリアスを使用して代替リージョンを定義します。

次の Terraform の例は、エイリアスを使用して異なる でバケットをプロビジョニングする方法を示しています AWS リージョン。プロバイダーのデフォルトリージョンは ですが`us-west-2`、 でリソースをプロビジョニングするために east エイリアスを使用できます`us-east-2`。

```
provider "aws" {
  region = "us-west-2"
}

provider "aws" {
  alias  = "east"
  region = "us-east-2"
}

resource "aws_s3_bucket" "myWestS3Bucket" {
  bucket = "my-west-s3-bucket"
}

resource "aws_s3_bucket" "myEastS3Bucket" {
  provider = aws.east
  bucket   = "my-east-s3-bucket"
}
```

前の例に示すように、メタ引数`alias`とともに `provider` を使用する場合、特定のリソースに対して別のプロバイダー設定を指定できます。1 つのスタック内の複数の AWS リージョン にリソースをプロビジョニングすることは、ほんの始まりです。エイリアスプロバイダーは、さまざまな点で便利に使い分けることができます。

例えば、一度に複数の Kubernetes クラスターをプロビジョニングするのが非常に一般的です。エイリアスを使用すると、追加の Helm プロバイダーと Kubernetes プロバイダーを設定して、Amazon EKS リソースごとにこれらのサードパーティーツールを異なる方法で使用できるようになります。次の Terraform コード例は、エイリアスを使用してこのタスクを実行する方法を示しています。

```
resource "aws_eks_cluster" "example_0" {
  name     = "example_0"
  role_arn = aws_iam_role.cluster_role.arn
  vpc_config {
    endpoint_private_access = true
    endpoint_public_access  = true
    subnet_ids              = var.subnet_ids[0]
  }
}

resource "aws_eks_cluster" "example_1" {
  name     = "example_1"
  role_arn = aws_iam_role.cluster_role.arn
  vpc_config {
    endpoint_private_access = true
    endpoint_public_access  = true
    subnet_ids              = var.subnet_ids[1]
  }
}

locals {
  host         = aws_eks_cluster.example_0.endpoint
  certificate  = base64decode(aws_eks_cluster.example_0.certificate_authority.data)
  host1        = aws_eks_cluster.example_1.endpoint
  certificate1 = base64decode(aws_eks_cluster.example_1.certificate_authority.data)
}

provider "helm" {
  kubernetes {
    host                   = local.host
    cluster_ca_certificate = local.certificate
    exec {
      api_version = "client.authentication.k8s.io/v1beta1"
      args        = ["eks", "get-token", "--cluster-name", aws_eks_cluster.example_0.name]
      command     = "aws"
    }
  }
}

provider "helm" {
  alias = "helm1"
  kubernetes {
    host                   = local.host1
    cluster_ca_certificate = local.certificate1
    exec {
      api_version = "client.authentication.k8s.io/v1beta1"
      args        = ["eks", "get-token", "--cluster-name", aws_eks_cluster.example_1.name]
      command     = "aws"
    }
  }
}

provider "kubernetes" {
  host                   = local.host
  cluster_ca_certificate = local.certificate
  exec {
    api_version = "client.authentication.k8s.io/v1beta1"
    args        = ["eks", "get-token", "--cluster-name", aws_eks_cluster.example_0.name]
    command     = "aws"
  }
}

provider "kubernetes" {
  alias                  = "kubernetes1"
  host                   = local.host1
  cluster_ca_certificate = local.certificate1
  exec {
    api_version = "client.authentication.k8s.io/v1beta1"
    args        = ["eks", "get-token", "--cluster-name", aws_eks_cluster.example_1.name]
    command     = "aws"
  }
}
```