Serverless App Engine (SAE) にアプリケーションをデプロイした後、インターネット向け Server Load Balancer (SLB) インスタンスを追加してアプリケーションを公開したり、内部向け SLB インスタンスを追加して同一 Virtual Private Cloud (VPC) 内のアプリケーション間通信を可能にしたりできます。本トピックでは、Terraform を使用して、インターネット向け SLB インスタンスを SAE アプリケーションにバインドおよびバインド解除する方法について説明します。
本トピックのサンプルコードは、次のリンクから実行できます:ワンクリックで実行
前提条件
-
AccessKey を Alibaba Cloud アカウント で使用することは、アカウントがすべてのリソースに対する完全な権限を持つため、高いセキュリティリスクを伴います。インフラストラクチャ管理には、RAM ユーザー を作成して使用することを推奨します。詳細については、「RAM ユーザーの作成」および「AccessKey の作成」をご参照ください。
-
Terraform コマンドを実行する際に使用する RAM ユーザー に、次のポリシーをアタッチしてください。これにより、本チュートリアルのリソース管理に必要な最小限の権限が付与されます。詳細については、「RAM ユーザー権限の管理」をご参照ください。
このカスタム ポリシー により、RAM ユーザー は SLB インスタンスの記述、作成、削除、構成、ステータス設定が可能になります。また、SAE アプリケーションを SLB インスタンスにバインドおよびバインド解除する権限も付与されます。
{ "Version": "1", "Statement": [ { "Effect": "Allow", "Action": [ "slb:DescribeLoadBalancers", "slb:DescribeLoadBalancerAttribute", "slb:CreateLoadBalancer", "slb:DeleteLoadBalancer", "slb:ModifyLoadBalancerInternetSpec", "slb:CreateLoadBalancerTCPListener", "slb:CreateLoadBalancerHTTPListener", "slb:CreateLoadBalancerHTTPSListener", "slb:DeleteLoadBalancerListener", "slb:SetLoadBalancerStatus", "sae:BindSlb", "sae:UnbindSlb" ], "Resource": "*" } ] } -
Terraform 実行環境を準備します。次のいずれかの方法を使用できます:
-
Resource Orchestration Service (ROS) は、マネージド型の Terraform ホスティングサービスを提供しています。ROS コンソールで直接 Terraform テンプレートをデプロイできます。詳細については、「Terraform タイプのスタックの作成」をご参照ください。
-
Terraform Explorer で Terraform を使用する:この方法は、Terraform の迅速な実験とデバッグに最適です。
-
Terraform を使用してリソースを迅速に作成する:Cloud Shell には Terraform がプリインストールされており、認証情報が事前に設定されています。Cloud Shell で直接 Terraform コマンドを実行できます。この方法は、Terraform への迅速かつ便利なアクセスを提供します。
-
Terraform をローカルにインストールして構成する:この方法は、ネットワーク接続が不安定な場合や、カスタム開発環境が必要な場合に適しています。
-
リソース
-
alicloud_sae_namespace:SAE 名前空間を作成および管理します。
-
alicloud_security_group:セキュリティグループを作成および管理します。
-
alicloud_security_group_rule:セキュリティグループルールを作成および管理します。
-
alicloud_sae_application:SAE アプリケーションを作成および管理します。
-
alicloud_slb_load_balancer:SLB インスタンスを作成および管理します。
-
alicloud_sae_load_balancer_internet:SAE アプリケーションにインターネット向けロードバランサーを構成します。
アプリケーションの作成と SLB インスタンスのバインド
この例では、イメージからカスタムアプリケーションをデプロイし、SLB インスタンスにバインドする方法を 中国 (深圳) リージョンで示します。
-
Terraform ファイル用に、terraform という名前のプロジェクトフォルダーを作成します。
-
プロジェクトディレクトリに移動:
cd terraform -
main.tf という名前の設定ファイルを作成します。
ファイルには次の設定を含める必要があります:
# プロバイダーの設定 provider "alicloud" { region = var.region_id } # 変数の定義 variable "region_id" { type = string default = "cn-shenzhen" } variable "app_name" { description = "アプリケーションの名前。" type = string default = "app-slb" } variable "image_url" { description = "イメージの URL。" type = string default = "registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-slim:0.9" } variable "namespace_id" { description = "名前空間の ID。" type = string default = "cn-shenzhen:demo" } variable "namespace_name" { description = "名前空間の名前。" type = string default = "demo" } # 名前空間 resource "alicloud_sae_namespace" "default" { namespace_description = var.namespace_description namespace_id = var.namespace_id namespace_name = var.namespace_name } # VPC resource "alicloud_vpc" "default" { vpc_name = var.name cidr_block = "10.4.0.0/16" } # VSwitch resource "alicloud_vswitch" "default" { vswitch_name = var.name cidr_block = "10.4.0.0/24" vpc_id = alicloud_vpc.default.id zone_id = var.zone_id } # セキュリティグループ resource "alicloud_security_group" "sg" { name = var.name description = var.description vpc_id = alicloud_vpc.default.id } resource "alicloud_security_group_rule" "sg_rule" { type = "ingress" ip_protocol = "tcp" nic_type = "intranet" policy = "accept" port_range = var.port_range priority = 1 security_group_id = alicloud_security_group.sg.id cidr_ip = var.cidr_ip } # アプリケーションの設定 resource "alicloud_sae_application" "manual" { app_name = var.app_name app_description = var.app_description deploy = true image_url = var.image_url namespace_id = alicloud_sae_namespace.default.id vswitch_id = alicloud_vswitch.default.id vpc_id = alicloud_vpc.default.id security_group_id = alicloud_security_group.sg.id package_type = var.package_type timezone = "Asia/Beijing" replicas = var.replicas cpu = var.cpu memory = var.memory } # SLB の設定 resource "alicloud_slb_load_balancer" "slb" { load_balancer_name = "prod" address_type = "internet" load_balancer_spec = "slb.s2.small" vswitch_id = alicloud_vswitch.default.id } resource "alicloud_sae_load_balancer_internet" "example" { app_id = alicloud_sae_application.manual.id internet_slb_id = alicloud_slb_load_balancer.slb.id internet { protocol = "HTTP" port = var.port target_port = 80 } } # その他の変数定義 variable "namespace_description" { description = "名前空間の説明。" default = "a namespace" } variable "name" { default = "tf" description = "リソースの共通名。" type = string } variable "description" { default = "セキュリティグループルールの説明。" description = "セキュリティグループの説明。" type = string } variable "port_range" { default = "1/65535" description = "セキュリティグループルールのポート範囲。" type = string } variable "cidr_ip" { description = "新しいセキュリティグループルールの作成に使用される CIDR ブロック。" type = string default = "0.0.0.0/0" } variable "zone_id" { description = "アベイラビリティーゾーンの ID。" type = string default = "cn-shenzhen-a" } variable "app_description" { default = "Terraform で作成されたアプリケーションの説明。" description = "アプリケーションの説明。" type = string } variable "package_type" { default = "Image" description = "アプリケーションのパッケージタイプ。" type = string } variable "cpu" { default = "500" description = "アプリケーションの CPU 割り当て(ミリコア単位)。" type = string } variable "memory" { default = "1024" description = "アプリケーションのメモリ割り当て(MB 単位)。" type = string } variable "replicas" { default = "1" description = "アプリケーションインスタンス(レプリカ)の数。" type = string } variable "port" { description = "SLB インスタンスのリスニングポート。" type = string default = "8000" } # 出力 output "namespace_id" { value = var.namespace_id description = "名前空間の ID。" } output "app_id" { description = "アプリケーションの ID。" value = alicloud_sae_application.manual.id } output "app_name" { description = "アプリケーションの名前。" value = var.app_name } output "endpoint" { value = format("http://%s:%s", alicloud_slb_load_balancer.slb.address, var.port) } -
次のコマンドを実行して、Terraform の作業ディレクトリを初期化します。
terraform init -
次のような出力が表示されます:
Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary. -
以下のコマンドを実行してリソースを作成します。
-
アプリケーションをデプロイするには、次のコマンドを実行します。 プロンプトが表示されたら、
yesと入力し、[Enter] を押してデプロイを確定します。terraform apply次のような出力が表示されます:
Apply complete! Resources: 8 added, 0 changed, 0 destroyed. Outputs: app_id = "8abb794f-a47b-474b-b6f5-2xxx" app_name = "app-slb" endpoint = "http://8.xxx.xxx:8000" namespace_id = "cn-shenzhen:demo"
Terraform は
app-slbアプリケーションを作成し、SLB インスタンスに関連付けます。出力には、SLB インスタンスのパブリック IP アドレスとポートが表示されます。 -
-
結果の確認:
terraform show の実行
次のコマンドを実行して、Terraform で作成されたリソースの詳細を表示します:
terraform show# alicloud_sae_application.manual: resource "alicloud_sae_application" "manual" { app_description = "Terraform で作成されたアプリケーションの説明。" app_name = "app-slb" cpu = 500 deploy = true id = "8abb794f-a47b-474b-bxxx51" image_url = "registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-slim:0.9" memory = 1024 namespace_id = "cn-shenzhen:demo" package_type = "Image" replicas = 1 security_group_id = "sg-xxx" status = "RUNNING" timezone = "Asia/Beijing" vpc_id = "vpc-xxx" vswitch_id = "vsw-xxx" } # alicloud_sae_load_balancer_internet.example: resource "alicloud_sae_load_balancer_internet" "example" { app_id = "8abb794f-a47b-474b-bxxx51" id = "8abb794f-a47b-474b-bxxx51" internet_ip = "8.xxx.xxx" internet_slb_id = "lb-xxx" internet { port = 8000 protocol = "HTTP" target_port = 80 } }ブラウザで検証
Web ブラウザに SLB インスタンスの IP アドレスとポートを入力します (例:
http://121.43.XXX.XX:8000)。 Welcome to nginx! というメッセージが表示されている Nginx のウェルカムページが表示され、アプリケーションが正常に実行されていることを示しています。
リソースのクリーンアップ
このトピックでは、中国 (深圳) リージョンのアプリケーション app-slb を例に、SLB インスタンスをバインド解除し、アプリケーションを削除する方法を説明します。
-
作成されたすべてのリソースを削除するには、プロジェクトディレクトリで次のコマンドを実行します。
terraform destroy -
次のような出力が表示されます:
Destroy complete! Resources: 8 destroyed.このコマンドは、SLB インスタンスのバインドを解除し、SAE アプリケーションを削除し、本チュートリアルで作成した他のすべてのリソースを削除します。
完全なサンプルコード
本トピックのサンプルコードは、次のリンクから実行できます:ワンクリックで実行
# プロバイダーの設定
provider "alicloud" {
region = var.region_id
}
# 変数の定義
variable "region_id" {
type = string
default = "cn-shenzhen"
}
variable "app_name" {
description = "アプリケーションの名前。"
type = string
default = "app-slb"
}
variable "image_url" {
description = "イメージの URL。"
type = string
default = "registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-slim:0.9"
}
variable "namespace_id" {
description = "名前空間の ID。"
type = string
default = "cn-shenzhen:demo"
}
variable "namespace_name" {
description = "名前空間の名前。"
type = string
default = "demo"
}
# 名前空間
resource "alicloud_sae_namespace" "default" {
namespace_description = var.namespace_description
namespace_id = var.namespace_id
namespace_name = var.namespace_name
}
# VPC
resource "alicloud_vpc" "default" {
vpc_name = var.name
cidr_block = "10.4.0.0/16"
}
# VSwitch
resource "alicloud_vswitch" "default" {
vswitch_name = var.name
cidr_block = "10.4.0.0/24"
vpc_id = alicloud_vpc.default.id
zone_id = var.zone_id
}
# セキュリティグループ
resource "alicloud_security_group" "sg" {
name = var.name
description = var.description
vpc_id = alicloud_vpc.default.id
}
resource "alicloud_security_group_rule" "sg_rule" {
type = "ingress"
ip_protocol = "tcp"
nic_type = "intranet"
policy = "accept"
port_range = var.port_range
priority = 1
security_group_id = alicloud_security_group.sg.id
cidr_ip = var.cidr_ip
}
# アプリケーションの設定
resource "alicloud_sae_application" "manual" {
app_name = var.app_name
app_description = var.app_description
deploy = true
image_url = var.image_url
namespace_id = alicloud_sae_namespace.default.id
vswitch_id = alicloud_vswitch.default.id
vpc_id = alicloud_vpc.default.id
security_group_id = alicloud_security_group.sg.id
package_type = var.package_type
timezone = "Asia/Beijing"
replicas = var.replicas
cpu = var.cpu
memory = var.memory
}
# SLB の設定
resource "alicloud_slb_load_balancer" "slb" {
load_balancer_name = "prod"
address_type = "internet"
load_balancer_spec = "slb.s2.small"
vswitch_id = alicloud_vswitch.default.id
}
resource "alicloud_sae_load_balancer_internet" "example" {
app_id = alicloud_sae_application.manual.id
internet_slb_id = alicloud_slb_load_balancer.slb.id
internet {
protocol = "HTTP"
port = var.port
target_port = 80
}
}
# その他の変数定義
variable "namespace_description" {
description = "名前空間の説明。"
default = "a namespace"
}
variable "name" {
default = "tf"
description = "リソースの共通名。"
type = string
}
variable "description" {
default = "セキュリティグループルールの説明。"
description = "セキュリティグループの説明。"
type = string
}
variable "port_range" {
default = "1/65535"
description = "セキュリティグループルールのポート範囲。"
type = string
}
variable "cidr_ip" {
description = "新しいセキュリティグループルールの作成に使用される CIDR ブロック。"
type = string
default = "0.0.0.0/0"
}
variable "zone_id" {
description = "アベイラビリティーゾーンの ID。"
type = string
default = "cn-shenzhen-a"
}
variable "app_description" {
default = "Terraform で作成されたアプリケーションの説明。"
description = "アプリケーションの説明。"
type = string
}
variable "package_type" {
default = "Image"
description = "アプリケーションのパッケージタイプ。"
type = string
}
variable "cpu" {
default = "500"
description = "アプリケーションの CPU 割り当て(ミリコア単位)。"
type = string
}
variable "memory" {
default = "1024"
description = "アプリケーションのメモリ割り当て(MB 単位)。"
type = string
}
variable "replicas" {
default = "1"
description = "アプリケーションインスタンス(レプリカ)の数。"
type = string
}
variable "port" {
description = "SLB インスタンスのリスニングポート。"
type = string
default = "8000"
}
# 出力
output "namespace_id" {
value = var.namespace_id
description = "名前空間の ID。"
}
output "app_id" {
description = "アプリケーションの ID。"
value = alicloud_sae_application.manual.id
}
output "app_name" {
description = "アプリケーションの名前。"
value = var.app_name
}
output "endpoint" {
value = format("http://%s:%s", alicloud_slb_load_balancer.slb.address, var.port)
}
関連ドキュメント
-
Alibaba Cloud の Terraform の概要については、「Alibaba Cloud Terraform とは」をご参照ください。