

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# Terraform을 사용하여 사용자 지정 AMI 생성
<a name="tutorial-create-ami-terraform"></a>

를 사용하는 경우 AWS ParallelCluster 이미지 및 클러스터를 생성하거나 업데이트할 때 생성된 AWS 리소스 AWS ParallelCluster에 대해서만 비용을 지불합니다. 자세한 내용은 [AWS 에서 사용하는 서비스 AWS ParallelCluster](aws-services-v3.md) 단원을 참조하십시오.

**사전 조건**
+  Terraform v1.5.7\$1가 설치되었습니다.
+ [AWS ParallelCluster API](api-reference-v3.md) v3.8.0\$1가 계정에 배포됩니다. [Terraform을 사용하여 클러스터 생성](tutorial-create-cluster-terraform.md)을(를) 참조하세요.
+ ParallelCluster API를 간접적으로 호출할 수 있는 권한이 있는 IAM 역할입니다. [필수 권한](tutorial-create-ami-terraform-permissions.md)을(를) 참조하세요.

# Terraform 프로젝트 정의
<a name="tutorial-create-ami-terraform-define"></a>

이 자습서에서는 ParallelCluster 사용자 지정 AMI를 배포하기 위한 간단한 Terraform 프로젝트를 정의합니다.

1. 이름이 `my-amis`인 디렉터리를 생성합니다.

   생성하는 모든 파일은 이 디렉터리 내에 있습니다.

1. `terraform.tf` 파일을 생성하여 ParallelCluster 공급자를 가져옵니다.

   ```
   terraform {
     required_version = ">= 1.5.7"
     required_providers {
       aws-parallelcluster = {
         source  = "aws-tf/aws-parallelcluster"
         version = "~> 1.0"
       }
     }
   }
   ```

1. `providers.tf` 파일을 생성하여 ParallelCluster 및 AWS 공급자를 구성합니다.

   ```
   provider "aws" {
     region  = var.region
     profile = var.profile
   }
   
   provider "aws-parallelcluster" {
     region         = var.region
     profile        = var.profile
     api_stack_name = var.api_stack_name
     use_user_role  = true
   }
   ```

1. `main.tf` 파일을 생성하여 ParallelCluster 모듈을 사용하여 리소스를 정의합니다.

   `image_configuration` 요소 내에서 설정할 수 있는 이미지 속성을 검토하려면 [빌드 이미지 구성 파일](image-builder-configuration-file-v3.md) 섹션을 참조하세요.

   이미지 생성을 위해 설정할 수 있는 옵션, 예를 들어 `image_id` 및 `rollback_on_failure`를 검토하려면 [`pcluster build-image`](pcluster.build-image-v3.md) 섹션을 참조하세요.

   ```
   data "aws-parallelcluster_list_official_images" "parent_image" {
     region = var.region
     os = var.os
     architecture = var.architecture
   }
   
   resource "aws-parallelcluster_image" "demo01" {
     image_id            = "demo01"
     image_configuration = yamlencode({
       "Build":{
         "InstanceType": "c5.2xlarge",
         "ParentImage": data.aws-parallelcluster_list_official_images.parent_image.official_images[0].amiId,
         "UpdateOsPackages": {"Enabled": false}
       }
     })
     rollback_on_failure = false
   }
   ```

1. `variables.tf` 파일을 생성하여 이 프로젝트에 주입할 수 있는 변수를 정의합니다.

   ```
   variable "region" {
     description = "The region the ParallelCluster API is deployed in."
     type        = string
     default     = "us-east-1"
   }
   
   variable "profile" {
     type        = string
     description = "The AWS profile used to deploy the clusters."
     default     = null
   }
   
   variable "api_stack_name" {
     type        = string
     description = "The name of the CloudFormation stack used to deploy the ParallelCluster API."
     default     = "ParallelCluster"
   }
   
   variable "api_version" {
     type        = string
     description = "The version of the ParallelCluster API."
   }
   
   variable "os" {
     type        = string
     description = "The OS of the ParallelCluster image."
   }
   
   variable "architecture" {
     type        = string
     description = "The architecture of the ParallelCluster image."
   }
   ```

1. 파일을 생성`terraform.tfvars`하여 변수에 대한 임의 값을 설정합니다.

   아래 파일을 사용하면 스택 이름이 인에 이미 배포된 기존 ParallelCluster API 3.11.1을 사용하여 x86\$164 아키텍처용 Amazon Linux 2를 `us-east-1` 기반으로 사용자 지정 AMI`us-east-1`를에 배포합니다`MyParallelClusterAPI-3111`.

   ```
   region = "us-east-1"
   api_stack_name = "MyParallelClusterAPI-3111"
   api_version = "3.11.1"
   
   os = "alinux2"
   architecture = "x86_64"
   ```

1. `outputs.tf` 파일을 생성하여 이 프로젝트에서 반환되는 출력을 정의합니다.

   ```
   output "parent_image" {
     value = data.aws-parallelcluster_list_official_images.parent_image.official_images[0]
   }
   
   output "custom_image" {
     value = aws-parallelcluster_image.demo01
   }
   ```

   프로젝트 디렉터리:

   ```
   my-amis
   ├── main.tf - Terraform entrypoint where the ParallelCluster module is configured.
   ├── outputs.tf - Defines the cluster as a Terraform output.
   ├── providers.tf - Configures the providers: ParallelCluster and AWS.
   ├── terraform.tf - Import the ParallelCluster provider.
   ├── terraform.tfvars - Defines values for variables, e.g. region, PCAPI stack name.
   └── variables.tf - Defines the variables, e.g. region, PCAPI stack name.
   ```

# API 배포
<a name="tutorial-create-ami-terraform-deploy"></a>

AMI를 배포하려면 표준 Terraform 명령을 순서대로 실행합니다.

1. 프로젝트 빌드:

   ```
   terraform init
   ```

1. 배포 계획 정의:

   ```
   terraform plan -out tfplan
   ```

1. 계획 배포:

   ```
   terraform apply tfplan
   ```

# 필수 권한
<a name="tutorial-create-ami-terraform-permissions"></a>

Terraform을 사용하여 사용자 지정 AMI를 배포하려면 다음 권한이 필요합니다.
+ ParallelCluster API와의 상호 작용을 담당하는 ParallelCluster API 역할을 수임합니다.
+ ParallelCluster API의 CloudFormation 스택을 설명하여 API가 존재하는지 확인하고 파라미터 및 출력을 검색합니다.

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Action": "sts:AssumeRole",
            "Resource": "arn:aws:sts::111122223333:role/PCAPIUserRole-*",
            "Effect": "Allow",
            "Sid": "AssumePCAPIUserRole"
        },
        {
            "Action": [
                "cloudformation:DescribeStacks"
            ],
            "Resource": "arn:aws:cloudformation:us-east-1:111122223333:stack/*",
            "Effect": "Allow",
            "Sid": "CloudFormation"
        }
    ]
}
```

------