![[アップデート] AWS PCS が HashiCorp Terraform でデプロイできるようになりました](https://images.ctfassets.net/ct0aopd36mqt/wp-thumbnail-242d6adbbf7d12f8c05dfdd896582262/2aac9bbff0d5d56bc0a4ab752ced5d22/hpc_eyecatch.png)
[アップデート] AWS PCS が HashiCorp Terraform でデプロイできるようになりました
こんにちは!クラウド事業本部コンサルティング部のたかくに(@takakuni_)です。
AWS PCS が HashiCorp Terraform でデプロイできるようになりました。
先日、 CloudFormation もサポートしましたね。
アップデート内容
今回のアップデートは Terraform の Cloud Control Provider です。
いわゆる awscc_
から始まるリソースブロックで、デプロイできるようになったということです。サポートされたリソースは次のとおりです。
- awscc_pcs_cluster
- awscc_pcs_compute_node_group | Resources | hashicorp/awscc | Terraform | Terraform Registry
- awscc_pcs_queue | Resources | hashicorp/awscc | Terraform | Terraform Registry
やってみる
それでは、実際に Terraform で PCS をデプロイしてみようと思います。
今回はクラスター、ノードグループ、キューを VPC 上にホストした最低限の構成とします。
Provider
まずは Provider です。CHANGELOG.md
をみると 1.34.0 (March 20, 2025)
でサポート開始したようなので awscc のバージョンは明示的に指定します。
aws プロバイダーも合わせて指定しました。
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.92.0"
}
awscc = {
source = "hashicorp/awscc"
version = "1.34.0"
}
}
}
provider "aws" {
# Configuration options
region = "ap-northeast-1"
}
provider "awscc" {
# Configuration options
region = "ap-northeast-1"
}
data "aws_region" "this" {}
data "aws_caller_identity" "this" {}
locals {
account_id = data.aws_caller_identity.this.account_id
region = data.aws_region.this.name
}
VPC
ネットワーク周りは VPC Module を流用しました。とても便利ですよね。
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
name = "pcs"
cidr = "10.0.0.0/16"
azs = ["${local.region}a", "${local.region}c"]
private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
public_subnets = ["10.0.101.0/24", "10.0.102.0/24"]
enable_nat_gateway = true
enable_vpn_gateway = true
single_nat_gateway = true
}
PCS
続いてメインの PCS に移ります。
セキュリティグループ
まずは、クラスターとノードグループが作成する、セキュリティグループを定義します。
22番ポートは最小の CIDR 範囲を設定しましょう。
###########################################
# Node Group
###########################################
variable "my_ip" {
description = "My IP address"
default = "0.0.0.0/0"
}
# クラスターのセキュリティグループ
resource "aws_security_group" "cluster" {
name = "pcs-cluster-sg"
description = "pcs-cluster-sg"
vpc_id = module.vpc.vpc_id
# 自己参照のインバウンドルール
ingress {
from_port = 0
to_port = 0
protocol = "-1"
self = true
description = "Allow all inbound from self"
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [
var.my_ip
]
description = "Allow all outbound to world"
}
# 自己参照のアウトバウンドルール
egress {
from_port = 0
to_port = 0
protocol = "-1"
self = true
description = "Allow all outbound to self"
}
# インターネットへのアウトバウンドルール
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
description = "Allow all outbound to world"
}
tags = {
Name = "pcs-cluster-sg"
}
}
セキュリティグループについてより細かく絞りたい場合は以下をご覧ください。
ただし、 EFA を利用する場合は、すべてのトラフィックをノード/クラスター間で許可する必要があります。
Cluster
クラスターです。先ほど作成したサブネットとセキュリティグループを指定しましょう。
###########################################
# Cluster
###########################################
resource "awscc_pcs_cluster" "this" {
name = "pcs-cluster"
networking = {
subnet_ids = [module.vpc.private_subnets[0]]
security_group_ids = [aws_security_group.cluster.id]
}
scheduler = {
type = "SLURM"
version = "24.05"
}
size = "SMALL"
}
IAM ロール
続いてノードグループが利用する IAM ロールでせす。
インスタンスプロファイルについて補足ですが、プレフィックスは事前に予約されており、 AWSPCS-
から始まるまたは、/aws-pcs/
のパスを含む必要があります。ご注意ください。
- iamInstanceProfileArn (string) –
[REQUIRED]
The Amazon Resource Name (ARN) of the IAM instance profile used to pass an IAM role when launching EC2 instances. The role contained in your instance profile must have the > pcs:RegisterComputeNodeGroupInstance permission. The resource identifier of the ARN must start with AWSPCS or it must have /aws-pcs/ in its path.
Examples
- arn:aws:iam::111122223333:instance-profile/AWSPCS-example-role-1
- arn:aws:iam::111122223333:instance-profile/aws-pcs/example-role-2
###########################################
# Role for the cluster
###########################################
resource "aws_iam_role" "pcs_cluster" {
name = "AWSPCS-cluster-role"
path = "/"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Principal = {
Service = "ec2.amazonaws.com"
}
Action = "sts:AssumeRole"
}
]
})
}
resource "aws_iam_role_policy" "pcs_cluster" {
name = "AWSPCS-cluster-policy"
role = aws_iam_role.pcs_cluster.id
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = ["pcs:RegisterComputeNodeGroupInstance"]
Resource = "*"
}
]
})
}
resource "aws_iam_role_policy_attachment" "pcs_cluster" {
role = aws_iam_role.pcs_cluster.name
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}
resource "aws_iam_instance_profile" "pcs_cluster" {
name = aws_iam_role.pcs_cluster.name
path = "/"
role = aws_iam_role.pcs_cluster.name
}
キーペア
キーペアは terraform(git)で管理したくなかったため、コンソールから作成しました。pcs-blog
という名前で作成しました。
'
Node Group
ノードグループです。 PCS は起動テンプレートを利用して起動するため、まずは aws_launch_template
を定義します。
先ほど作成した、ネットワーク、キーペア、インスタンスプロファイルを指定します。
ami_id は固定で設定したため、随時更新してください。
resource "aws_launch_template" "pcs" {
name = "pcs-launch-template"
description = "PCS cluster node launch template"
# AMIとインスタンスタイプ
# aws-pcs-sample_ami-amzn2-x86_64-slurm-24.05-2024-12-14T05-28-32.441Z at ap-northeast-1
image_id = "ami-0e18e980afc64cc20"
instance_type = "c6i.xlarge"
key_name = "pcs-blog"
# ネットワーク設定
vpc_security_group_ids = [aws_security_group.cluster.id]
# IAMインスタンスプロファイル(必要に応じて)
iam_instance_profile {
name = aws_iam_instance_profile.pcs_cluster.name
}
}
resource "awscc_pcs_compute_node_group" "this" {
cluster_id = awscc_pcs_cluster.this.cluster_id
iam_instance_profile_arn = aws_iam_instance_profile.pcs_cluster.arn
subnet_ids = [module.vpc.private_subnets[0]]
custom_launch_template = {
template_id = aws_launch_template.pcs.id
version = aws_launch_template.pcs.latest_version
}
scaling_configuration = {
max_instance_count = 2
min_instance_count = 0
}
instance_configs = [
{
instance_type = "c6i.xlarge"
}
]
}
Queue
最後にキューです。
こちらは特段変わったことなく、クラスターID、ノードグループ ID を指定して終わりです。
###########################################
# Queue
###########################################
resource "awscc_pcs_queue" "this" {
cluster_id = awscc_pcs_cluster.this.cluster_id
compute_node_group_configurations = [
{
compute_node_group_id = awscc_pcs_compute_node_group.this.compute_node_group_id
}
]
}
クラスターがアクティブになっていますね。
まとめ
以上、「AWS PCS が HashiCorp Terraform でデプロイできるようになりました。」でした。
サービスよって IaC をサポートした/しないのアナウンスがまちまちですが、何か基準があるのか気になりますね。
このブログがどなたかの参考になれば幸いです。クラウド事業本部コンサルティング部のたかくに(@takakuni_)でした!