以下の内容はhttps://htn20190109.hatenablog.com/entry/2024/06/16/201316より取得しました。


{Alibaba API Gateway} Create an API with a resource in a VPC as the backend service

https://www.alibabacloud.com/help/en/api-gateway/getting-started/create-an-api-operation-with-a-resource-in-a-vpc-as-the-backend-service

https://www.alibabacloud.com/help/en/api-gateway/developer-reference/api-cloudapi-2016-07-14-overview

 


前提: API Gatewayアクティベート済み

東京リージョンはバックエンドサービスの登録ができないので
杭州市で実施

 

-- 1. Create instances in a VPC

 


cat <<-'EOF' | base64
#!/bin/bash
yum install -y httpd
systemctl start httpd
systemctl enable httpd
echo $(uname -n) > /var/www/html/index.html
EOF


echo "IyEvYmluL2Jhc2gKeXVtIGluc3RhbGwgLXkgaHR0cGQKc3lzdGVtY3RsIHN0YXJ0IGh0dHBkCnN5c3RlbWN0bCBlbmFibGUgaHR0cGQKZWNobyAkKHVuYW1lIC1uKSA+IC92YXIvd3d3L2h0bWwvaW5kZXguaHRtbAo=" | base64 -d

 

cat <<-'EOF' > variables.tf

locals {
  availability_zone = "cn-hangzhou-h"
}

 

variable "instance_type" {
  description = "instance_type"
  type = string
  default = "ecs.n1.tiny"
}

variable "image_id" {
  description = "image_id"
  type = string
  default = "aliyun_2_1903_x64_20G_alibase_20231221.vhd"
}

EOF

 

cat <<-'EOF' > main.tf

terraform {
  required_version = ">= 1.0.0, < 2.0.0"
  required_providers {
    alicloud = {
       source  = "aliyun/alicloud"
       version = "= 1.217.0"
    }
  }
}

provider "alicloud" {
    region  = "cn-hangzhou"
}


resource "alicloud_vpc" "vpc01" {
  vpc_name          = "vpc01"
  description       = "vpc01"
  cidr_block        = "10.2.0.0/16"
}

 

resource "alicloud_vswitch" "sw01" {
  vswitch_name      = "sw01"
  description       = "sw01"
  vpc_id            = alicloud_vpc.vpc01.id
  cidr_block        = "10.2.1.0/24"
  zone_id           = local.availability_zone
}

 resource "alicloud_security_group" "sg01" {
   name                = "sg01"
   description         = "sg01"
   vpc_id              = alicloud_vpc.vpc01.id
   security_group_type = "normal"
 }


resource "alicloud_security_group_rule" "sg0101" {
  type              = "ingress"
  ip_protocol       = "tcp"
  port_range        = "80/80"
  security_group_id = alicloud_security_group.sg01.id
  nic_type          = "intranet"
  policy            = "accept"
  priority          = 10
  cidr_ip           = "0.0.0.0/0"
  description       = "sg0101"
}

 

resource "alicloud_vpc_ipv4_gateway" "gw01" {
  ipv4_gateway_name        = "gw01"
  ipv4_gateway_description = "gw01"
  vpc_id                   = alicloud_vpc.vpc01.id
  enabled                  = true
}


# ルートテーブルへvSwitchの関連付け
resource "alicloud_route_table_attachment" "sw01_rt01" {
  vswitch_id     = alicloud_vswitch.sw01.id
  route_table_id = alicloud_vpc.vpc01.route_table_id
}


# ルートテーブルへカスタムルート追加
resource "alicloud_route_entry" "rt0101" {
  name                  = "rt0101"
  route_table_id        = alicloud_vpc.vpc01.route_table_id
  destination_cidrblock = "0.0.0.0/0"
  nexthop_type          = "Ipv4Gateway"
  nexthop_id            = alicloud_vpc_ipv4_gateway.gw01.id
}

 

resource "alicloud_instance" "ecs01" {
  image_id                   = var.image_id
  instance_type              = var.instance_type
  security_groups            = [alicloud_security_group.sg01.id]
  instance_name              = "ecs01"
  system_disk_category       = "cloud_ssd"
  system_disk_name           = "ecs01"
  system_disk_size           = 20
  description                = "ecs01"
  internet_charge_type       = "PayByBandwidth"
  internet_max_bandwidth_out = 0
  host_name                  = "ecs01"
  vswitch_id                 = alicloud_vswitch.sw01.id
  instance_charge_type       = "PostPaid"
  key_name                   = "alibabakey02"
  deletion_protection        = false
  credit_specification       = "Standard"
  user_data                  = "IyEvYmluL2Jhc2gKeXVtIGluc3RhbGwgLXkgaHR0cGQKc3lzdGVtY3RsIHN0YXJ0IGh0dHBkCnN5c3RlbWN0bCBlbmFibGUgaHR0cGQKZWNobyAkKHVuYW1lIC1uKSA+IC92YXIvd3d3L2h0bWwvaW5kZXguaHRtbAo="
}


resource "alicloud_eip_address" "eip01" {
  address_name              = "eip01"
  bandwidth                 = 1
  deletion_protection       = false
  description               = "eip01"
  internet_charge_type      = "PayByTraffic"
  isp                       = "BGP"
  payment_type              = "PayAsYouGo"
  netmode                   = "public"
}

# インスタンスとEIPの関連付け
resource "alicloud_eip_association" "ecs01_eip01" {
  instance_id              = alicloud_instance.ecs01.id
  allocation_id            = alicloud_eip_address.eip01.id
}

 

EOF

 

cat <<-'EOF' > outputs.tf

 

output "vpc01_id" {
  value = alicloud_vpc.vpc01.id
  description = "vpc01.id"
}


output "sw01_id" {
  value = alicloud_vswitch.sw01.id
  description = "sw01.id"
}


output "sg01_id" {
  value = alicloud_security_group.sg01.id
  description = "sg01.id"
}

 

output "ecs01_id" {
  value = alicloud_instance.ecs01.id
  description = "ecs01.id"
}


EOF

 


terraform init
terraform fmt
terraform -version

 

terraform plan

 

terraform apply -auto-approve

 


-- 2. Authorize API Gateway to access the VPC

aliyun cloudapi SetVpcAccess \
--InstanceId i-11111111111111111111 \
--Name vpc01 \
--Port 80 \
--VpcId vpc-111111111111111111111 

aliyun cloudapi DescribeVpcAccesses 

 

-- 3. バックエンドサービスの作成

aliyun cloudapi DescribeBackendList

 

aliyun cloudapi CreateBackend \
--BackendName be01 \
--BackendType "VPC

aliyun cloudapi DescribeBackendInfo \
--BackendId 11111111111111111111111111111111


-- 4. バックエンドサービスの設定


aliyun cloudapi DescribeBackendInfo \
--BackendId 11111111111111111111111111111111

 

aliyun cloudapi CreateBackendModel \
--BackendId 11111111111111111111111111111111 \
--StageName RELEASE \
--BackendType VPC \
--BackendModelData '{
                                        "DiscoveryConfig": {
                                                "NacosConfig": {},
                                                "ZookeeperConfig": {}
                                        },
                                        "EdasConfig": {},
                                        "EventBridgeConfig": {},
                                        "FunctionComputeConfig": {},
                                        "MockConfig": {},
                                        "OssConfig": {},
                                        "Type": "VPC",
                                        "VpcConfig": {
                                                "InstanceId": "i-11111111111111111111",
                                                "Name": "vpc01",
                                                "Port": 80,
                                                "VpcAccessId": "11111111111111111111111111111111",
                                                "VpcId": "vpc-111111111111111111111",
                                                "VpcScheme": "",
                                                "VpcTargetHostName": ""
                                        }
                                }'
                                

 


-- 5. Create an API group


aliyun cloudapi DescribeInstances 

 

aliyun cloudapi DescribeApiGroups 


aliyun cloudapi CreateApiGroup \
--GroupName apig01 \
--BasePath "/" \
--InstanceId api-shared-vpc-hz-003 


aliyun cloudapi DescribeApiGroup \
--GroupId 11111111111111111111111111111111

 

-- 6. Create an API

 

aliyun cloudapi DescribeApis


aliyun cloudapi CreateApi \
--ApiName api01 \
--GroupId 11111111111111111111111111111111 \
--RequestConfig '{
                "BodyFormat": "",
                "PostBodyDescription": "",
                "RequestHttpMethod": "GET",
                "RequestMode": "PASSTHROUGH",
                "RequestPath": "/test",
                "RequestProtocol": "HTTP"
        }' \
--ServiceConfig ' {
                "ContentTypeCatagory": "CLIENT",
                "ContentTypeValue": "",
                "Mock": "FALSE",
                "MockHeaders": {
                        "MockHeader":
                },
                "MockResult": "",
                "ServiceAddress": "",
                "ServiceHttpMethod": "GET",
                "ServicePath": "/",
                "ServiceProtocol": "HTTP",
                "ServiceTimeout": 10000,
                "ServiceVpcEnable": "TRUE",
                "VpcConfig": {
                        "Name": "vpc01",
                        "Port": 80,
                        "VpcId": "vpc-111111111111111111111"
                }
        }' \
--Visibility PRIVATE \
--BackendEnable true \
--BackendId 11111111111111111111111111111111 \
--DisableInternet false \
--ErrorCodeSamples '{
                "ErrorCodeSample":

        }' \
--FailResultSample "" \
--ForceNonceCheck false \
--ResultDescriptions '{
                "ResultDescription": []
        }' \
--ResultSample "" \
--ResultType PASSTHROUGH \
--WebSocketApiType COMMON \
--AuthType ANONYMOUS \

 

 

※Message: The request processing has failed due to some unknown error.
となるが、APIは作成される


aliyun cloudapi DescribeApi \
--ApiId 11111111111111111111111111111111

 

 

 

-- 7. Publish the API

aliyun cloudapi DeployApi \
--ApiId 11111111111111111111111111111111 \
--Description "test" \
--StageName RELEASE 

 


aliyun cloudapi DescribeDeployedApi \
--ApiId 11111111111111111111111111111111 \
--StageName RELEASE 

 

-- 8. 動作確認

curl http://11111111111111111111111111111111-cn-hangzhou.alicloudapi.com/test


-- 9. クリーンアップ


aliyun cloudapi DescribeDeployedApi \
--ApiId 11111111111111111111111111111111 \
--StageName RELEASE 

aliyun cloudapi AbolishApi \
--ApiId 11111111111111111111111111111111 \
--StageName RELEASE 

 

aliyun cloudapi DescribeApi \
--ApiId 11111111111111111111111111111111

aliyun cloudapi DeleteApi \
--ApiId 11111111111111111111111111111111

 

aliyun cloudapi DescribeApiGroups 

aliyun cloudapi DeleteApiGroup \
--GroupId 11111111111111111111111111111111 

 

aliyun cloudapi DescribeBackendList


aliyun cloudapi DeleteBackend \
--BackendId 11111111111111111111111111111111 


aliyun cloudapi DescribeVpcAccesses 

aliyun cloudapi RemoveVpcAccess \
--InstanceId i-11111111111111111111 \
--Port 80 \
--VpcId vpc-111111111111111111111 

 

terraform destroy -auto-approve

 

 

 

 

 




以上の内容はhttps://htn20190109.hatenablog.com/entry/2024/06/16/201316より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14