Serverless App Engine (SAE) は、アプリケーション指向のサーバーレス PaaS プラットフォームです。アプリケーションを SAE にデプロイすると、クラスターやサーバーを管理・保守する必要がなくなります。アプリケーションの設計と構築に集中し、SAE にデプロイできます。コンソール、API、プラグイン、CI/CD を使用したアプリケーションのデプロイに加えて、Terraform を使用して SAE アプリケーションをデプロイすることもできます。このトピックでは、Terraform を使用して、SAE アプリケーションを自動的に作成、カスタム構成で作成、および削除する方法について説明します。
前提条件
Alibaba Cloud アカウント (root ユーザー) は、すべてのリソースに対する完全な権限を持っています。認証情報が漏洩すると、ご利用のリソースは高いリスクにさらされます。RAM ユーザーを使用し、その AccessKey を作成することを推奨します。詳細については、「RAM ユーザーの作成」および「AccessKey の作成」をご参照ください。
この例のリソースを管理するには、Terraform コマンドを実行する RAM ユーザーに、次の最小権限ポリシーをアタッチします。詳細については、「RAM ユーザーの権限管理」をご参照ください。
このカスタムポリシーにより、RAM ユーザーは SAE のアプリケーションとサービスを管理できます。権限には、アプリケーションの記述、作成、更新、削除、デプロイ、開始、停止、およびサービスの作成、更新、削除、バインド、アンバインドが含まれます。
{ "Version": "1", "Statement": [ { "Effect": "Allow", "Action": [ "sae:DescribeApplications", "sae:DescribeApplication", "sae:DescribeInstances", "sae:DescribeInstance", "sae:DescribeServices", "sae:DescribeService" ], "Resource": "*" }, { "Effect": "Allow", "Action": [ "sae:CreateApplication", "sae:UpdateApplication", "sae:DeleteApplication", "sae:DeployApplication", "sae:StartApplication", "sae:StopApplication" ], "Resource": "acs:sae:*:*:application/*" }, { "Effect": "Allow", "Action": [ "sae:CreateService", "sae:UpdateService", "sae:DeleteService", "sae:BindService", "sae:UnbindService" ], "Resource": "acs:sae:*:*:service/*" } ] }Terraform 実行環境を準備します。次のいずれかのオプションを選択してください:
ROS は Terraform のマネージドサービスを提供します。ROS コンソールで Terraform テンプレートを直接デプロイできます。詳細については、「Terraform ベースのスタックの作成」をご参照ください。
Terraform Explorer で Terraform を使用する:Alibaba Cloud は Terraform を実行するためのオンライン環境を提供します。ログイン後、インストール不要でオンラインで Terraform を使用できます。この方法は、Terraform を迅速、便利、かつ無料で試したりデバッグしたりするのに適しています。
Terraform を使用してリソースを迅速に作成する:Terraform コンポーネントは Alibaba Cloud Cloud Shell にプリインストール済みで、認証情報も設定されています。Cloud Shell で直接 Terraform コマンドを実行できます。この方法は、低コストで迅速かつ便利に Terraform にアクセスするのに適しています。
オンプレミスで Terraform をインストールおよび設定する:この方法は、ネットワーク接続が悪い場合や、カスタム開発環境が必要なシナリオに適しています。
使用するリソース
alicloud_sae_application:SAE アプリケーションを管理および作成します。
alicloud_vpc:サブネット、ルートテーブル、セキュリティ設定など、VPC を構成します。
alicloud_vswitch:VPC 内にサブネットを作成します。各サブネットは独自の IP アドレス範囲とルートテーブルを持つことができます。vSwitch を使用して、異なるサブネットに異なるアプリケーションやサービスをデプロイできます。
alicloud_security_group:VPC 内のネットワークアクセス制御を管理します。
アプリケーションの作成
SAE は、イメージおよびコードパッケージからのデプロイをサポートしています。サポートされているパッケージタイプには、JAR、WAR、PHP ZIP パッケージが含まれます。アプリケーションを作成する際、次のいずれかの方法で VPC を構成できます:
自動構成:SAE は、名前空間、VPC、vSwitch、セキュリティグループなどのリソースを自動的に構成します。デフォルトの名前空間が使用されます。
カスタム構成:アプリケーション用に、名前空間、VPC、vSwitch、セキュリティグループなどのリソースを構成する必要があります。
自動構成
この例では、中国 (北京) リージョンでイメージからアプリケーションを自動的にデプロイする方法を示します。
- Terraform リソースを格納するために、terraform という名前のプロジェクトフォルダを作成します。
- 次のコマンドを実行して、プロジェクトディレクトリに移動します:
cd terraform main.tf という名前の設定ファイルを作成し、次の内容を追加します。
provider "alicloud" { region = var.region } variable "region" { default = "cn-beijing" } variable "name" { default = "serverless-example" } resource "random_integer" "default" { max = 99999 min = 10000 } data "alicloud_regions" "default" { current = true } data "alicloud_zones" "default" { available_resource_creation = "VSwitch" } resource "alicloud_vpc" "default" { vpc_name = var.name cidr_block = "10.4.0.0/16" } resource "alicloud_vswitch" "default" { vswitch_name = var.name cidr_block = "10.4.0.0/24" vpc_id = alicloud_vpc.default.id zone_id = data.alicloud_zones.default.zones.0.id } resource "alicloud_security_group" "default" { vpc_id = alicloud_vpc.default.id } resource "alicloud_sae_namespace" "default" { namespace_id = "${data.alicloud_regions.default.regions.0.id}:example${random_integer.default.result}" namespace_name = var.name namespace_description = var.name enable_micro_registration = false } resource "alicloud_sae_application" "default" { app_description = var.name app_name = "${var.name}-${random_integer.default.result}" namespace_id = alicloud_sae_namespace.default.id image_url = "registry-vpc.${data.alicloud_regions.default.regions.0.id}.aliyuncs.com/sae-demo-image/consumer:1.0" package_type = "Image" security_group_id = alicloud_security_group.default.id vpc_id = alicloud_vpc.default.id vswitch_id = alicloud_vswitch.default.id timezone = "Asia/Beijing" replicas = "1" cpu = "500" memory = "2048" }Container Registry コンソールにログインし、ターゲットインスタンスリポジトリの [基本情報] ページでイメージアドレスを表示できます。アドレスは次のフォーマットです:
registry.<regionId>.aliyuncs.com/<namespace_name>/<image_repository_name>:<image_version_number>- 次のコマンドを実行して、構成を初期化します:
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.次のコマンドを実行して、SAE アプリケーションを作成します。
次のコマンドを実行して、設定ファイルを適用します。コマンドの実行中にプロンプトが表示されたら、
yesと入力し、[Enter] キーを押して、コマンドが完了するのを待ちます。次の情報が表示された場合、権限付与は完了です。
terraform apply期待される出力:
Apply complete! Resources: 6 added, 0 changed, 0 destroyed.結果の確認
Terraform show コマンド
次のコマンドを実行して、Terraform によって作成されたリソースの詳細をクエリできます:
terraform showshell@Alicloud: /serverless$ terraform show
# alicloud_sae_application.default:
resource "alicloud_sae_application" "default" {
app_description = "serverless-xxx"
app_name = "serverless-xxx"
batch_wait_time = 10
command_args_v2 = []
config_map_mount_desc = jsonencode([])
cpu = 500
custom_host_alias = jsonencode([])
enable_ahas = "false"
enable_grey_tag_route = false
envs = jsonencode([])
id = "131976e5-7264-xxx"
image_url = "registry-vpc.cn-beijing.aliyuncs.com/sae-demo-image/consumer:1.0"
memory = 2048
min_ready_instance_ratio = -1
min_ready_instances = -1
namespace_id = "cn-beijing:xxx"
package_type = "Image"
package_version = "1731047094"
replicas = 1
security_group_id = "sg-2zxxx"
status = "RUNNING"
termination_grace_period_seconds = 30
timezone = "Asia/Beijing"
vpc_id = "vpc-xxx"
vswitch_id = "vsw-xxx"
}SAE コンソール
SAE コンソールにログインし、作成されたアプリケーションの一覧を表示します。
アプリケーション一覧には、serverless-xxx のように自動作成されたアプリケーションが表示されます。インスタンス数が 1/1 であることは、アプリケーションが正常に実行されていることを示します。
次のサンプルコードは、Terraform Explorer でワンクリックで実行できます。
カスタム構成:イメージデプロイメント
この例では、中国 (深セン) リージョンで、カスタム構成を使用してイメージからアプリケーションをデプロイする方法を示します。
- Terraform リソースを格納するために、terraform という名前のプロジェクトフォルダを作成します。
- 次のコマンドを実行して、プロジェクトディレクトリに移動します:
cd terraform main.tf という名前の設定ファイルを作成し、次の内容を追加します。
provider "alicloud" { region = var.region_id } # リージョン ID variable "region_id" { type = string default = "cn-shenzhen" } # アプリケーション名 variable "app_name" { description = "The name of the application" type = string default = "manual-jar-tf" } # アプリケーションの説明 variable "app_description" { default = "description created by Terraform" description = "The description of the application" type = string } # アプリケーションのデプロイ方法 variable "package_type" { default = "FatJar" description = "The package type of the application" type = string } # インスタンスの CPU 仕様 variable "cpu" { default = "500" description = "The cpu of the application, in unit of millicore" type = string } # インスタンスのメモリ仕様 variable "memory" { default = "1024" description = "The memory of the application, in unit of MB" type = string } # JAR パッケージの URL variable "jar_url" { description = "The JAR url of the application, like `oss://my-bucket/my-app.jar`" type = string default = "https://edas-sz.oss-cn-shenzhen.aliyuncs.com/prod/demo/SPRING_CLOUD_CONSUMER.jar" } # アプリケーションインスタンスの数 variable "replicas" { default = "1" description = "The replicas of the application" type = string } # 名前空間名 variable "namespace_name" { description = "Namespace Name" type = string default = "tfjardemo" } # 名前空間 ID variable "namespace_id" { description = "Namespace ID" type = string default = "cn-shenzhen:tfjardemo" # 既存の名前空間 ID を参照 } # 名前空間の説明 variable "namespace_description" { description = "Namespace Description" default = "a namespace" } # VPC とセキュリティグループ variable "name" { default = "tf" description = "The name of the security group rule" type = string } variable "description" { default = "The description of the security group rule" description = "The description of the security group rule" type = string } # ポート範囲 variable "port_range" { default = "1/65535" description = "The port range of the security group rule" type = string } # CIDR ブロック variable "cidr_ip" { description = "cidr blocks used to create a new security group rule" type = string default = "0.0.0.0/0" } # リージョン内のアベイラビリティゾーン variable "zone_id" { description = "Availability Zone ID" type = string default = "cn-shenzhen-e" # 十分なリソースがあるアベイラビリティゾーンを選択 } # アプリケーションログを SLS に収集 variable "slsConfig" { default = "[{\"logDir\":\"\",\"logType\":\"stdout\"},{\"logDir\":\"/home/admin/logs/*.log\"}]" description = "The config of sls log collect" type = string } resource "alicloud_vpc" "vpc" { vpc_name = "tf-vpc" cidr_block = "172.16.0.0/16" } resource "alicloud_vswitch" "vswitch" { vpc_id = alicloud_vpc.vpc.id cidr_block = "172.16.1.0/24" zone_id = var.zone_id vswitch_name = "tf-vswitch" description = "tf-vswitch description" } resource "alicloud_sae_namespace" "default" { namespace_id = var.namespace_id namespace_name = var.namespace_name namespace_description = var.namespace_description } output "namespace_id" { value = var.namespace_id description = "Namespace ID" } resource "alicloud_security_group" "sg" { name = var.name description = var.description vpc_id = alicloud_vpc.vpc.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 package_url = var.jar_url namespace_id = alicloud_sae_namespace.default.id vswitch_id = alicloud_vswitch.vswitch.id vpc_id = alicloud_vpc.vpc.id security_group_id = alicloud_security_group.sg.id package_type = var.package_type jdk = "Open JDK 8" timezone = "Asia/Beijing" replicas = var.replicas cpu = var.cpu memory = var.memory micro_registration = "0" lifecycle { ignore_changes = [ micro_registration ] } } output "app_id" { description = "The id of the application" value = alicloud_sae_application.manual.id } output "app_name" { description = "The name of the application" value = var.app_name }- 次のコマンドを実行して、構成を初期化します:
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: 6 added, 0 changed, 0 destroyed.
Outputs:
app_id = "72c052ba-xxx"
app_name = "manual-image-tf"
namespace_id = "cn-shenzhen:tfdemo"期待どおりの出力が得られた場合、アプリケーションは正常にデプロイされています。
結果の確認
Terraform show コマンド
次のコマンドを実行して、Terraform によって作成されたリソースの詳細をクエリできます:
terraform showshell@Alicloud: ~/serverless$ terraform show
# alicloud_sae_application.manual:
resource "alicloud_sae_application" "manual" {
app_description = "description created by Terraform"
app_name = "manual-image-tf"
batch_wait_time = 10
command_args_v2 = []
config_map_mount_desc = jsonencode([])
cpu = 500
custom_host_alias = jsonencode([])
deploy = true
enable_ahas = "false"
enable_grey_tag_route = false
envs = jsonencode([])
id = "72c05xxx"
image_url = "registry.cn-beijing.aliyuncs.com/sae-serverless-demo/sae-demo:microservice-java-provider-v1.0"
memory = 1024
min_ready_instance_ratio = -1
min_ready_instances = -1
namespace_id = "cn-shenzhen:tfdemo"
package_type = "Image"
package_version = "1732169733"
replicas = 1
security_group_id = "sg-wz96xxx"
status = "RUNNING"
termination_grace_period_seconds = 30
timezone = "Asia/Beijing"
vpc_id = "vpc-xxx"
vswitch_id = "vsw-xxx"
}
# alicloud_sae_namespace.default:
resource "alicloud_sae_namespace" "default" {
enable_micro_registration = true
id = "cn-shenzhen:tfdemo"
namespace_description = "a namespace"
namespace_id = "cn-shenzhen:tfdemo"
namespace_name = "tfdemo"
namespace_short_id = "tfdemo"
}SAE コンソール
SAE コンソールにログインし、作成されたアプリケーションの一覧を表示します。
マイクロサービスアプリケーションの一覧で、tfdemo 名前空間にアプリケーション manual-image-tf が作成されていることがわかります。アプリケーションの現在のインスタンス数/ターゲットインスタンス数が 1/1 であり、正常に実行されていることが確認できます。
manual-image-tf アプリケーションの左側のナビゲーションウィンドウで、[変更履歴] をクリックします。変更タイプが [アプリケーションのデプロイ] で、ステータスが [成功] であることを確認します。
カスタム構成:JAR パッケージデプロイメント
この例では、中国 (深セン) リージョンで、カスタム構成を使用して JAR パッケージからアプリケーションをデプロイする方法を示します。
- Terraform リソースを格納するために、terraform という名前のプロジェクトフォルダを作成します。
- 次のコマンドを実行して、プロジェクトディレクトリに移動します:
cd terraform main.tf という名前の設定ファイルを作成し、次の内容を追加します。
provider "alicloud" { region = var.region_id } # リージョン ID variable "region_id" { type = string default = "cn-shenzhen" } # アプリケーション名 variable "app_name" { description = "The name of the application" type = string default = "manual-jar-tf" } # アプリケーションの説明 variable "app_description" { default = "description created by Terraform" description = "The description of the application" type = string } # アプリケーションのデプロイ方法 variable "package_type" { default = "FatJar" description = "The package type of the application" type = string } # インスタンスの CPU 仕様 variable "cpu" { default = "500" description = "The cpu of the application, in unit of millicore" type = string } # インスタンスのメモリ仕様 variable "memory" { default = "1024" description = "The memory of the application, in unit of MB" type = string } # JAR パッケージの URL variable "jar_url" { description = "The JAR url of the application, like `oss://my-bucket/my-app.jar`" type = string default = "https://edas-sz.oss-cn-shenzhen.aliyuncs.com/prod/demo/SPRING_CLOUD_CONSUMER.jar" } # アプリケーションインスタンスの数 variable "replicas" { default = "1" description = "The replicas of the application" type = string } # 名前空間名 variable "namespace_name" { description = "Namespace Name" type = string default = "tfjardemo" } # 名前空間 ID variable "namespace_id" { description = "Namespace ID" type = string default = "cn-shenzhen:tfjardemo" # 既存の名前空間 ID を参照 } # 名前空間の説明 variable "namespace_description" { description = "Namespace Description" default = "a namespace" } # VPC とセキュリティグループ variable "name" { default = "tf" description = "The name of the security group rule" type = string } variable "description" { default = "The description of the security group rule" description = "The description of the security group rule" type = string } # ポート範囲 variable "port_range" { default = "1/65535" description = "The port range of the security group rule" type = string } # CIDR ブロック variable "cidr_ip" { description = "cidr blocks used to create a new security group rule" type = string default = "0.0.0.0/0" } # リージョン内のアベイラビリティゾーン variable "zone_id" { description = "Availability Zone ID" type = string default = "cn-shenzhen-e" # 十分なリソースがあるアベイラビリティゾーンを選択 } # アプリケーションログを SLS に収集 variable "slsConfig" { default = "[{\"logDir\":\"\",\"logType\":\"stdout\"},{\"logDir\":\"/home/admin/logs/*.log\"}]" description = "The config of sls log collect" type = string } resource "alicloud_vpc" "vpc" { vpc_name = "tf-vpc" cidr_block = "172.16.0.0/16" } resource "alicloud_vswitch" "vswitch" { vpc_id = alicloud_vpc.vpc.id cidr_block = "172.16.1.0/24" zone_id = var.zone_id vswitch_name = "tf-vswitch" description = "tf-vswitch description" } resource "alicloud_sae_namespace" "default" { namespace_id = var.namespace_id namespace_name = var.namespace_name namespace_description = var.namespace_description } output "namespace_id" { value = var.namespace_id description = "Namespace ID" } resource "alicloud_security_group" "sg" { name = var.name description = var.description vpc_id = alicloud_vpc.vpc.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 package_url = var.jar_url namespace_id = alicloud_sae_namespace.default.id vswitch_id = alicloud_vswitch.vswitch.id vpc_id = alicloud_vpc.vpc.id security_group_id = alicloud_security_group.sg.id package_type = var.package_type jdk = "Open JDK 8" timezone = "Asia/Beijing" replicas = var.replicas cpu = var.cpu memory = var.memory micro_registration = "0" lifecycle { ignore_changes = [ micro_registration ] } } output "app_id" { description = "The id of the application" value = alicloud_sae_application.manual.id } output "app_name" { description = "The name of the application" value = var.app_name }次のコマンドを実行して、構成を初期化します。
terraform init次のコマンドを実行して、JAR パッケージからアプリケーションを作成します。
次のコマンドを実行して、アプリケーションをデプロイします。プロンプトが表示されたら、
yesと入力し、[Enter] キーを押します。コマンドの実行が完了した後に次のメッセージが表示された場合、権限付与は完了です。terraform apply期待される出力:
Apply complete! Resources: 6 added, 0 changed, 4 destroyed. Outputs: app_id = "aad70a1a-xxx" app_name = "manual-jar-tf" namespace_id = "cn-shenzhen:tfjardemo"
期待どおりの出力が得られた場合、アプリケーションは JAR パッケージから正常にデプロイされています。
結果の確認:
Terraform show コマンド
次のコマンドを実行して、Terraform によって作成されたリソースの詳細をクエリできます:
terraform showshell1@Alicloud:~/serverless$ terraform show
# alicloud_sae_application.manual:
resource "alicloud_sae_application" "manual" {
app_description = "description created by Terraform"
app_name = "manual-jar-tf"
batch_wait_time = 10
command_args_v2 = []
config_map_mount_desc = jsonencode([])
cpu = 500
custom_host_alias = jsonencode([])
deploy = true
enable_ahas = "false"
enable_grey_tag_route = false
envs = jsonencode([])
id = "6acbf6fc-6b1xxx"
jdk = "Open JDK 8"
memory = 1024
micro_registration = "0"
min_ready_instance_ratio = -1
min_ready_instances = -1
namespace_id = "cn-shenzhen:tfjardemo"
package_type = "FatJar"
package_url = "https://edas-sz.oss-cn-shenzhen.aliyuncs.com/prod/demo/SPRING_CLOUD_CONSUMER.jar"
package_version = "1732241446"
}
programming_language = "java"
replicas = 1
security_group_id = "sg-wz9j10xxx"
status = "RUNNING"
termination_grace_period_seconds = 30
timezone = "Asia/Beijing"
vpc_id = "vpc-wz9sjxxx"
vswitch_id = "vsw-wz9e1xxx"
}SAE コンソール
SAE コンソールにログインし、作成された名前空間を表示します。
左側のナビゲーションウィンドウで、[マイクロサービスアプリケーション] をクリックします。アプリケーション一覧で、tfjardemo 名前空間に manual-jar-tf アプリケーションが作成されていることを確認します。インスタンス数は 1/1 で、仕様は 0.5 コア 1 GB です。
アプリケーションの削除
作成したリソースを削除するには、terraform destroy コマンドを実行します。
プロジェクトディレクトリで、次のコマンドを実行します。
terraform destroy期待される出力:
module.vpc.alicloud_vswitch.vswitches: Destroying... [id=vsw-wz9bdsgvhit7jv2hb4li] module.vpc.alicloud_security_group_rule.sg_rule: Destroying... [id=sg-wz9ba3de0fxxriu5c0m5:i] alicloud_security_group_rule.sg_rule: Destruction complete after 1s alicloud_security_group.sg: Destroying... [id=sg-wz9ba3de0fxxriu5c0m5] alicloud_security_group.sg: Destruction complete after 0s module.vpc.alicloud_vswitch.vswitches: Destruction complete after 4s module.vpc.alicloud_vpc.vpc[0]: Destroying... [id=vpc-wz9cg2ifzghh2pt948zcj] module.vpc.alicloud_vpc.vpc[0]: Destruction complete after 6s Destroy complete! Resources: 4 destroyed.
参考資料
Terraform の詳細については、「Alibaba Cloud Terraform とは」をご参照ください。
Terraform を使用したアプリケーション管理
コンソールを使用したアプリケーション管理:「アプリケーションホスティングの概要」
API を使用したアプリケーション管理:「API 概要」