轉換用于簡化特定場景的模板編寫復雜度,可通過在模板中指定Transform進行聲明。ROS會將含有Transform的模板轉換為普通模板,然后進行資源的創建、更新等操作。
ROS支持Aliyun::Serverless轉換,可用于基于函數計算服務的無服務器(Serverless)場景。更多信息,請參見無服務器架構。
Aliyun::Serverless轉換會將使用SAM(Serverless Application Model)語法編寫的模板(基于阿里云Funcraft工具),轉換為ROS的普通模板。
更多信息,請參見什么是Serverless Devs和SAM(Serverless Application Model) 語法。
支持的資源類型
語法
轉換聲明的值必須為字符串,不能使用參數或函數來指定轉換值。
JSON
示例"Transform" : "Aliyun::Serverless-2018-04-03"
YAML
示例Transform: Aliyun::Serverless-2018-04-03
示例
以下是一個使用阿里云SAM語法編寫的模板:
ROSTemplateFormatVersion: "2015-09-01"
Transform: "Aliyun::Serverless-2018-04-03"
Resources:
MyService: # service name
Type: "Aliyun::Serverless::Service"
Properties:
Policies:
- AliyunFCReadOnlyAccess # Managed Policy
- Version: "1" # Policy Document
Statement:
- Effect: Allow
Action:
- oss:GetObject
- oss:GetObjectACL
Resource: "*"
MyFunction: # function name
Type: "Aliyun::Serverless::Function"
Properties:
Handler: index.handler
Runtime: nodejs6
CodeUri: "oss://testBucket/myCode.zip"
使用模板創建更改集或者資源棧時,ROS會展開SAM語法。上述模板將會轉換以下內容:
- 將Aliyun::Serverless::Service資源轉換為ALIYUN::FC::Service資源。
- 將Aliyun::Serverless::Service資源中定義的Policies轉換為ALIYUN::RAM::Role和ALIYUN::RAM::AttachPolicyToRole資源,并指定在 ALIYUN::FC::Service的Role參數中。
- 將Aliyun::Serverless::Function資源轉換為ALIYUN::FC::Function資源。
轉換后的ROS模板如下:
{
"ROSTemplateFormatVersion": "2015-09-01",
"Resources": {
"MyServiceRole": {
"Type": "ALIYUN::RAM::Role",
"Properties": {
"RoleName": "test-MyServiceRole-7840BA19B01C",
"Description": "Function Compute default role",
"Policies": [
{
"PolicyName": "test-MyServiceRole-7840BA19B01CPolicy1",
"PolicyDocument": {
"Version": "1",
"Statement": [
{
"Action": ["oss:GetObject", "oss:GetObjectACL"],
"Resource": ["*"],
"Effect": "Allow"
}
]
}
}
],
"AssumeRolePolicyDocument": {
"Version": "1",
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": ["fc.aliyuncs.com"]
}
}
]
}
}
},
"AliyunFCReadOnlyAccessMyServiceRole": {
"Type": "ALIYUN::RAM::AttachPolicyToRole",
"Properties": {
"PolicyName": "AliyunFCReadOnlyAccess",
"PolicyType": "System",
"RoleName": {
"Fn::GetAtt": ["MyServiceRole", "RoleName"]
}
},
"DependsOn": "MyServiceRole"
},
"MyService": {
"Type": "ALIYUN::FC::Service",
"Properties": {
"ServiceName": "test-MyService-E522E1B83D81",
"Role": {
"Fn::GetAtt": ["MyServiceRole", "Arn"]
},
"LogConfig": {
"Project": "",
"Logstore": ""
},
"InternetAccess": true
},
"DependsOn": ["MyServiceRole", "AliyunFCReadOnlyAccessMyServiceRole"]
},
"MyServiceMyFunction": {
"Type": "ALIYUN::FC::Function",
"Properties": {
"Code": {
"OssBucketName": "testBucket",
"OssObjectName": "myCode.zip"
},
"FunctionName": "MyFunction",
"ServiceName": "test-MyService-E522E1B83D81",
"EnvironmentVariables": {
"PATH": "/code/.fun/root/usr/local/bin:/code/.fun/root/usr/local/sbin:/code/.fun/root/usr/bin:/code/.fun/root/usr/sbin:/code/.fun/root/sbin:/code/.fun/root/bin:/code/.fun/python/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/sbin:/bin",
"LD_LIBRARY_PATH": "/code/.fun/root/usr/lib:/code/.fun/root/usr/lib/x86_64-linux-gnu:/code/.fun/root/lib/x86_64-linux-gnu:/code/.fun/root/usr/lib64:/code:/code/lib:/usr/local/lib",
"PYTHONUSERBASE": "/code/.fun/python"
},
"Handler": "index.handler",
"Runtime": "nodejs6"
},
"DependsOn": ["MyService"]
}
}
}