ALIYUN::EMR::SparkWorkspace

Updated at:

The ALIYUN::EMR::SparkWorkspace type is used to create an E-MapReduce Serverless Spark workspace.

Syntax

{
  "Type": "ALIYUN::EMR::SparkWorkspace",
  "Properties": {
    "OssBucket": String,
    "PaymentType": String,
    "ResourceSpec": Map,
    "RamRoleName": String,
    "WorkspaceName": String,
    "AutoRenewPeriod": String,
    "AutoRenew": Boolean,
    "AutoStartSessionCluster": Boolean,
    "AutoRenewPeriodUnit": String,
    "DlfType": String,
    "Duration": String,
    "DlfCatalogId": String,
    "GpuSubscription": Map,
    "GpuSpec": List,
    "IpWhiteList": List,
    "PaymentDurationUnit": String,
    "ReleaseType": String,
    "ResourceGroupId": String,
    "Subscription": Map,
    "Tags": List
  }
}

Properties

Property

Type

Required

Editable

Description

Constraints

OssBucket

String

Yes

No

The OSS bucket path of the workspace.

The path must be in the format of oss://bucket-name/path/.

PaymentType

String

Yes

No

The billing method of the workspace.

Valid values:

  • PostPaid: pay-as-you-go

  • PrePaid: subscription

RamRoleName

String

Yes

No

The RAM role used to run Spark jobs.

None

ResourceSpec

Map

Yes

Yes

The resource specifications of the workspace.

For more information, see ResourceSpec properties.

WorkspaceName

String

Yes

Yes

The name of the workspace.

The name must be 1 to 64 characters in length and can contain Chinese characters, letters, digits, hyphens (-), and underscores (_).

AutoRenew

Boolean

No

No

Specifies whether to enable auto-renewal for a subscription.

None

AutoRenewPeriod

String

No

No

The auto-renewal period.

None

AutoRenewPeriodUnit

String

No

No

The unit of the auto-renewal period.

Valid values:

  • MONTH

AutoStartSessionCluster

Boolean

No

No

Specifies whether to automatically start the session cluster.

None

DlfCatalogId

String

No

No

The ID of the DLF catalog.

None

DlfType

String

No

No

The DLF type bound to the workspace.

None

Duration

String

No

No

The subscription duration.

Valid range: 1 to 9.

Unit: month.

GpuSpec

List

No

Yes

The GPU instance specifications of the workspace.

None

GpuSubscription

Map

No

Yes

The GPU subscription configuration.

This configuration takes effect after the workspace is created. For more information, see GpuSubscription properties.

IpWhiteList

List

No

Yes

The IP address whitelist of the workspace.

This configuration takes effect after the workspace is created.

PaymentDurationUnit

String

No

No

The unit of the subscription duration.

Valid values:

  • MONTH

ReleaseType

String

No

No

The release type of the workspace.

None

ResourceGroupId

String

No

Yes

The ID of the resource group.

None

Subscription

Map

No

Yes

The configuration for converting a pay-as-you-go workspace to a subscription workspace.

This configuration takes effect after the workspace is created. For more information, see Subscription properties.

Tags

List

No

No

The tags to add to the workspace.

A maximum of 20 tags can be added when you create a workspace. For more information, see Tags properties.

ResourceSpec syntax

"ResourceSpec": {
  "Cu": String,
  "Gpu": Integer
}

ResourceSpec properties

Property

Type

Required

Editable

Description

Constraints

Cu

String

Yes

No

The maximum number of compute units.

Valid range: 1 to 9.

Gpu

Integer

No

No

The number of GPUs.

Minimum value: 0.

GpuSubscription syntax

"GpuSubscription": {
  "InstanceId": String,
  "AutoRenew": Boolean,
  "PaymentDurationUnit": String,
  "InstanceTypeId": String,
  "Duration": Integer,
  "Operation": String,
  "GpuMachineNum": Integer
}

GpuSubscription properties

Property

Type

Required

Editable

Description

Constraints

AutoRenew

Boolean

No

No

Specifies whether to enable auto-renewal.

None

Duration

Integer

No

No

The subscription duration.

Minimum value: 1.

GpuMachineNum

Integer

No

No

The number of GPU machines.

Minimum value: 1.

InstanceId

String

No

No

The ID of the GPU subscription instance.

None

InstanceTypeId

String

No

No

The GPU instance specification.

None

Operation

String

No

No

The operation type.

Valid values:

  • BUY

  • UPGRADE

PaymentDurationUnit

String

No

No

The unit of the subscription duration.

Valid values:

  • MONTH

Subscription syntax

"Subscription": {
  "AutoRenewPeriod": String,
  "AutoRenew": Boolean,
  "PaymentDurationUnit": String,
  "Duration": String,
  "Queue": List,
  "AutoRenewPeriodUnit": String
}

Subscription properties

Property

Type

Required

Editable

Description

Constraints

AutoRenew

Boolean

No

No

Specifies whether to enable auto-renewal.

None

AutoRenewPeriod

String

No

No

The auto-renewal period.

Valid range: 1 to 9.

Unit: month.

AutoRenewPeriodUnit

String

No

No

The unit of the auto-renewal period.

Valid values:

  • MONTH

Duration

String

No

No

The subscription duration.

Valid range: 1 to 9.

Unit: month.

PaymentDurationUnit

String

No

No

The unit of the subscription duration.

Valid values:

  • MONTH

Queue

List

No

No

The queues to convert to a subscription plan.

None

Tags syntax

"Tags": [
  {
    "Value": String,
    "Key": String
  }
]

Tags properties

Property

Type

Required

Editable

Description

Constraints

Key

String

Yes

No

The tag key.

None

Value

String

No

No

The tag value.

None

Return values

Fn::GetAtt

  • Storage: The OSS storage path of the workspace.

  • WorkspaceId: The ID of the workspace.

Examples

Scenario 1: Create a Spark workspace based on existing resources

ROSTemplateFormatVersion: '2015-09-01'
Description:
  zh-cn: 基于已有的OSS存储桶和RAM角色创建一个简单的EMR Serverless Spark工作空间。
  en: Create a simple EMR Serverless Spark workspace based on an existing OSS bucket and RAM role.
Parameters:
  WorkspaceName:
    Type: String
    Label:
      zh-cn: 工作空间名称
      en: Workspace Name
    Description:
      zh-cn: Spark工作空间名称,1-64个字符,支持中文、字母、数字、短划线和下划线。
      en: Spark workspace name, 1-64 characters. Supports Chinese, letters, digits, hyphens and underscores.
    MinLength: 1
    MaxLength: 64
    Default: my-spark-workspace
  OssBucketPath:
    Type: String
    Label:
      zh-cn: OSS存储路径
      en: OSS Bucket Path
    Description:
      zh-cn: 工作空间的OSS存储路径,格式为 oss://bucket-name/path/。
      en: 'The OSS storage path of the workspace, format: oss://bucket-name/path/.'
  RamRoleName:
    Type: String
    Label:
      zh-cn: RAM角色名称
      en: RAM Role Name
    Description:
      zh-cn: 用于运行Spark作业的RAM角色名称。
      en: The RAM role name used to run Spark jobs.
  Cu:
    Type: String
    Label:
      zh-cn: 计算单元(CU)
      en: Compute Units (CU)
    Description:
      zh-cn: 工作空间的最大计算单元数量。
      en: The maximum number of compute units for the workspace.
    Default: '10'
Resources:
  SparkWorkspace:
    Type: ALIYUN::EMR::SparkWorkspace
    Properties:
      WorkspaceName:
        Ref: WorkspaceName
      OssBucket:
        Ref: OssBucketPath
      RamRoleName:
        Ref: RamRoleName
      PaymentType: PayAsYouGo
      ResourceSpec:
        Cu:
          Ref: Cu
Outputs:
  WorkspaceId:
    Description:
      zh-cn: 工作空间ID。
      en: Workspace ID.
    Label:
      zh-cn: 工作空间ID
      en: Workspace ID
    Value:
      Fn::GetAtt:
        - SparkWorkspace
        - WorkspaceId
  Storage:
    Description:
      zh-cn: 工作空间的OSS存储路径。
      en: The OSS storage path of the workspace.
    Label:
      zh-cn: OSS存储路径
      en: Storage Path
    Value:
      Fn::GetAtt:
        - SparkWorkspace
        - Storage
Metadata:
  ALIYUN::ROS::Interface:
    ParameterGroups:
      - Parameters:
          - WorkspaceName
          - OssBucketPath
          - RamRoleName
          - Cu
{
  "ROSTemplateFormatVersion": "2015-09-01",
  "Description": {
    "zh-cn": "基于已有的OSS存储桶和RAM角色创建一个简单的EMR Serverless Spark工作空间。",
    "en": "Create a simple EMR Serverless Spark workspace based on an existing OSS bucket and RAM role."
  },
  "Parameters": {
    "WorkspaceName": {
      "Type": "String",
      "Label": {
        "zh-cn": "工作空间名称",
        "en": "Workspace Name"
      },
      "Description": {
        "zh-cn": "Spark工作空间名称,1-64个字符,支持中文、字母、数字、短划线和下划线。",
        "en": "Spark workspace name, 1-64 characters. Supports Chinese, letters, digits, hyphens and underscores."
      },
      "MinLength": 1,
      "MaxLength": 64,
      "Default": "my-spark-workspace"
    },
    "OssBucketPath": {
      "Type": "String",
      "Label": {
        "zh-cn": "OSS存储路径",
        "en": "OSS Bucket Path"
      },
      "Description": {
        "zh-cn": "工作空间的OSS存储路径,格式为 oss://bucket-name/path/。",
        "en": "The OSS storage path of the workspace, format: oss://bucket-name/path/."
      }
    },
    "RamRoleName": {
      "Type": "String",
      "Label": {
        "zh-cn": "RAM角色名称",
        "en": "RAM Role Name"
      },
      "Description": {
        "zh-cn": "用于运行Spark作业的RAM角色名称。",
        "en": "The RAM role name used to run Spark jobs."
      }
    },
    "Cu": {
      "Type": "String",
      "Label": {
        "zh-cn": "计算单元(CU)",
        "en": "Compute Units (CU)"
      },
      "Description": {
        "zh-cn": "工作空间的最大计算单元数量。",
        "en": "The maximum number of compute units for the workspace."
      },
      "Default": "10"
    }
  },
  "Resources": {
    "SparkWorkspace": {
      "Type": "ALIYUN::EMR::SparkWorkspace",
      "Properties": {
        "WorkspaceName": {
          "Ref": "WorkspaceName"
        },
        "OssBucket": {
          "Ref": "OssBucketPath"
        },
        "RamRoleName": {
          "Ref": "RamRoleName"
        },
        "PaymentType": "PayAsYouGo",
        "ResourceSpec": {
          "Cu": {
            "Ref": "Cu"
          }
        }
      }
    }
  },
  "Outputs": {
    "WorkspaceId": {
      "Description": {
        "zh-cn": "工作空间ID。",
        "en": "Workspace ID."
      },
      "Label": {
        "zh-cn": "工作空间ID",
        "en": "Workspace ID"
      },
      "Value": {
        "Fn::GetAtt": [
          "SparkWorkspace",
          "WorkspaceId"
        ]
      }
    },
    "Storage": {
      "Description": {
        "zh-cn": "工作空间的OSS存储路径。",
        "en": "The OSS storage path of the workspace."
      },
      "Label": {
        "zh-cn": "OSS存储路径",
        "en": "Storage Path"
      },
      "Value": {
        "Fn::GetAtt": [
          "SparkWorkspace",
          "Storage"
        ]
      }
    }
  },
  "Metadata": {
    "ALIYUN::ROS::Interface": {
      "ParameterGroups": [
        {
          "Parameters": [
            "WorkspaceName",
            "OssBucketPath",
            "RamRoleName",
            "Cu"
          ]
        }
      ]
    }
  }
}

Scenario 2: Create an OSS bucket, RAM role, and Spark workspace

ROSTemplateFormatVersion: '2015-09-01'
Description:
  zh-cn: 创建OSS存储桶、RAM角色和EMR Serverless Spark工作空间,配置IP白名单、GPU规格和标签,实现完整的Spark计算环境。
  en: Create an OSS bucket, RAM role, and EMR Serverless Spark workspace with IP whitelist, GPU specification, and tags for a complete Spark computing environment.
Parameters:
  CommonName:
    Type: String
    Label:
      zh-cn: 资源名称前缀
      en: Resource Name Prefix
    Description:
      zh-cn: 所有资源名称的统一前缀。
      en: Unified name prefix for all resources.
    Default: spark-env
    MinLength: 1
    MaxLength: 50
  Cu:
    Type: String
    Label:
      zh-cn: 计算单元(CU)
      en: Compute Units (CU)
    Description:
      zh-cn: 工作空间的最大计算单元数量。
      en: The maximum number of compute units for the workspace.
    Default: '20'
  GpuCount:
    Type: Number
    Label:
      zh-cn: GPU数量
      en: GPU Count
    Description:
      zh-cn: 工作空间的GPU数量,0表示不使用GPU。
      en: The number of GPUs for the workspace. 0 means no GPU.
    Default: 0
    MinValue: 0
Resources:
  OssBucket:
    Type: ALIYUN::OSS::Bucket
    Properties:
      BucketName:
        Fn::Sub: ${CommonName}-${ALIYUN::AccountId}
      AccessControl: private
      DeletionForce: true
      Tags:
        CreatedBy: ROS
        Purpose: SparkWorkspace
  RamRole:
    Type: ALIYUN::RAM::Role
    Properties:
      RoleName:
        Fn::Sub: ${CommonName}-spark-role
      Description: RAM role for EMR Serverless Spark
      AssumeRolePolicyDocument:
        Version: '1'
        Statement:
          - Action: sts:AssumeRole
            Effect: Allow
            Principal:
              Service:
                - emr-serverless-spark.aliyuncs.com
      Policies:
        - PolicyName: AliyunEMRServerlessSparkFullAccess
          PolicyDocument:
            Version: '1'
            Statement:
              - Action:
                  - oss:GetObject
                  - oss:PutObject
                  - oss:DeleteObject
                  - oss:ListObjects
                  - oss:AbortMultipartUpload
                  - oss:ListMultipartUploads
                Resource:
                  - Fn::Sub: acs:oss:*:*:${CommonName}-${ALIYUN::AccountId}
                  - Fn::Sub: acs:oss:*:*:${CommonName}-${ALIYUN::AccountId}/*
                Effect: Allow
  SparkWorkspace:
    Type: ALIYUN::EMR::SparkWorkspace
    DependsOn:
      - OssBucket
      - RamRole
    Properties:
      WorkspaceName:
        Fn::Sub: ${CommonName}-workspace
      OssBucket:
        Fn::Sub: oss://${CommonName}-${ALIYUN::AccountId}/spark-workspace/
      RamRoleName:
        Ref: RamRole
      PaymentType: PayAsYouGo
      ResourceSpec:
        Cu:
          Ref: Cu
        Gpu:
          Ref: GpuCount
      AutoStartSessionCluster: true
      Tags:
        - Key: Environment
          Value: Development
        - Key: Team
          Value: DataEngineering
        - Key: CreatedBy
          Value: ROS
Outputs:
  WorkspaceId:
    Description:
      zh-cn: 工作空间ID。
      en: Workspace ID.
    Label:
      zh-cn: 工作空间ID
      en: Workspace ID
    Value:
      Fn::GetAtt:
        - SparkWorkspace
        - WorkspaceId
  Storage:
    Description:
      zh-cn: 工作空间的OSS存储路径。
      en: The OSS storage path of the workspace.
    Label:
      zh-cn: OSS存储路径
      en: Storage Path
    Value:
      Fn::GetAtt:
        - SparkWorkspace
        - Storage
  OssBucketName:
    Description:
      zh-cn: OSS存储桶名称。
      en: OSS bucket name.
    Label:
      zh-cn: OSS存储桶名称
      en: OSS Bucket Name
    Value:
      Fn::GetAtt:
        - OssBucket
        - Name
  RamRoleArn:
    Description:
      zh-cn: RAM角色ARN。
      en: RAM role ARN.
    Label:
      zh-cn: RAM角色ARN
      en: RAM Role ARN
    Value:
      Fn::GetAtt:
        - RamRole
        - Arn
Metadata:
  ALIYUN::ROS::Interface:
    ParameterGroups:
      - Parameters:
          - CommonName
          - Cu
          - GpuCount
{
  "ROSTemplateFormatVersion": "2015-09-01",
  "Description": {
    "zh-cn": "创建OSS存储桶、RAM角色和EMR Serverless Spark工作空间,配置IP白名单、GPU规格和标签,实现完整的Spark计算环境。",
    "en": "Create an OSS bucket, RAM role, and EMR Serverless Spark workspace with IP whitelist, GPU specification, and tags for a complete Spark computing environment."
  },
  "Parameters": {
    "CommonName": {
      "Type": "String",
      "Label": {
        "zh-cn": "资源名称前缀",
        "en": "Resource Name Prefix"
      },
      "Description": {
        "zh-cn": "所有资源名称的统一前缀。",
        "en": "Unified name prefix for all resources."
      },
      "Default": "spark-env",
      "MinLength": 1,
      "MaxLength": 50
    },
    "Cu": {
      "Type": "String",
      "Label": {
        "zh-cn": "计算单元(CU)",
        "en": "Compute Units (CU)"
      },
      "Description": {
        "zh-cn": "工作空间的最大计算单元数量。",
        "en": "The maximum number of compute units for the workspace."
      },
      "Default": "20"
    },
    "GpuCount": {
      "Type": "Number",
      "Label": {
        "zh-cn": "GPU数量",
        "en": "GPU Count"
      },
      "Description": {
        "zh-cn": "工作空间的GPU数量,0表示不使用GPU。",
        "en": "The number of GPUs for the workspace. 0 means no GPU."
      },
      "Default": 0,
      "MinValue": 0
    }
  },
  "Resources": {
    "OssBucket": {
      "Type": "ALIYUN::OSS::Bucket",
      "Properties": {
        "BucketName": {
          "Fn::Sub": "${CommonName}-${ALIYUN::AccountId}"
        },
        "AccessControl": "private",
        "DeletionForce": true,
        "Tags": {
          "CreatedBy": "ROS",
          "Purpose": "SparkWorkspace"
        }
      }
    },
    "RamRole": {
      "Type": "ALIYUN::RAM::Role",
      "Properties": {
        "RoleName": {
          "Fn::Sub": "${CommonName}-spark-role"
        },
        "Description": "RAM role for EMR Serverless Spark",
        "AssumeRolePolicyDocument": {
          "Version": "1",
          "Statement": [
            {
              "Action": "sts:AssumeRole",
              "Effect": "Allow",
              "Principal": {
                "Service": [
                  "emr-serverless-spark.aliyuncs.com"
                ]
              }
            }
          ]
        },
        "Policies": [
          {
            "PolicyName": "AliyunEMRServerlessSparkFullAccess",
            "PolicyDocument": {
              "Version": "1",
              "Statement": [
                {
                  "Action": [
                    "oss:GetObject",
                    "oss:PutObject",
                    "oss:DeleteObject",
                    "oss:ListObjects",
                    "oss:AbortMultipartUpload",
                    "oss:ListMultipartUploads"
                  ],
                  "Resource": [
                    {
                      "Fn::Sub": "acs:oss:*:*:${CommonName}-${ALIYUN::AccountId}"
                    },
                    {
                      "Fn::Sub": "acs:oss:*:*:${CommonName}-${ALIYUN::AccountId}/*"
                    }
                  ],
                  "Effect": "Allow"
                }
              ]
            }
          }
        ]
      }
    },
    "SparkWorkspace": {
      "Type": "ALIYUN::EMR::SparkWorkspace",
      "DependsOn": [
        "OssBucket",
        "RamRole"
      ],
      "Properties": {
        "WorkspaceName": {
          "Fn::Sub": "${CommonName}-workspace"
        },
        "OssBucket": {
          "Fn::Sub": "oss://${CommonName}-${ALIYUN::AccountId}/spark-workspace/"
        },
        "RamRoleName": {
          "Ref": "RamRole"
        },
        "PaymentType": "PayAsYouGo",
        "ResourceSpec": {
          "Cu": {
            "Ref": "Cu"
          },
          "Gpu": {
            "Ref": "GpuCount"
          }
        },
        "AutoStartSessionCluster": true,
        "Tags": [
          {
            "Key": "Environment",
            "Value": "Development"
          },
          {
            "Key": "Team",
            "Value": "DataEngineering"
          },
          {
            "Key": "CreatedBy",
            "Value": "ROS"
          }
        ]
      }
    }
  },
  "Outputs": {
    "WorkspaceId": {
      "Description": {
        "zh-cn": "工作空间ID。",
        "en": "Workspace ID."
      },
      "Label": {
        "zh-cn": "工作空间ID",
        "en": "Workspace ID"
      },
      "Value": {
        "Fn::GetAtt": [
          "SparkWorkspace",
          "WorkspaceId"
        ]
      }
    },
    "Storage": {
      "Description": {
        "zh-cn": "工作空间的OSS存储路径。",
        "en": "The OSS storage path of the workspace."
      },
      "Label": {
        "zh-cn": "OSS存储路径",
        "en": "Storage Path"
      },
      "Value": {
        "Fn::GetAtt": [
          "SparkWorkspace",
          "Storage"
        ]
      }
    },
    "OssBucketName": {
      "Description": {
        "zh-cn": "OSS存储桶名称。",
        "en": "OSS bucket name."
      },
      "Label": {
        "zh-cn": "OSS存储桶名称",
        "en": "OSS Bucket Name"
      },
      "Value": {
        "Fn::GetAtt": [
          "OssBucket",
          "Name"
        ]
      }
    },
    "RamRoleArn": {
      "Description": {
        "zh-cn": "RAM角色ARN。",
        "en": "RAM role ARN."
      },
      "Label": {
        "zh-cn": "RAM角色ARN",
        "en": "RAM Role ARN"
      },
      "Value": {
        "Fn::GetAtt": [
          "RamRole",
          "Arn"
        ]
      }
    }
  },
  "Metadata": {
    "ALIYUN::ROS::Interface": {
      "ParameterGroups": [
        {
          "Parameters": [
            "CommonName",
            "Cu",
            "GpuCount"
          ]
        }
      ]
    }
  }
}

Scenario 3: Create a complete Spark data analytics platform

ROSTemplateFormatVersion: '2015-09-01'
Description:
  zh-cn: 创建完整的EMR Serverless Spark数据分析平台,包含OSS存储桶、RAM角色、Spark工作空间(GPU加速+IP白名单+自动续费)、Livy网关(自动启停+公网访问),实现端到端的Spark大数据计算与交互式分析。
  en: Create a complete EMR Serverless Spark data analysis platform including OSS bucket, RAM role, Spark workspace with GPU acceleration and IP whitelist, and Livy gateway with auto start/stop and public access for end-to-end Spark big data computing and interactive analysis.
Parameters:
  CommonName:
    Type: String
    Label:
      zh-cn: 资源名称前缀
      en: Resource Name Prefix
    Description:
      zh-cn: 所有资源名称的统一前缀。
      en: Unified name prefix for all resources.
    Default: spark-platform
    MinLength: 1
    MaxLength: 50
  Cu:
    Type: String
    Label:
      zh-cn: 计算单元(CU)
      en: Compute Units (CU)
    Description:
      zh-cn: 工作空间的最大计算单元数量。
      en: The maximum number of compute units for the workspace.
    Default: '50'
  GpuCount:
    Type: Number
    Label:
      zh-cn: GPU数量
      en: GPU Count
    Description:
      zh-cn: 工作空间的GPU数量。
      en: The number of GPUs for the workspace.
    Default: 2
    MinValue: 0
  SparkEngineVersion:
    Type: String
    Label:
      zh-cn: Spark引擎版本
      en: Spark Engine Version
    Description:
      zh-cn: Livy网关使用的Spark引擎版本。
      en: The Spark engine version used by the Livy gateway.
    Default: esr-4.2.0
  LivyCpuLimit:
    Type: String
    Label:
      zh-cn: Livy CPU核数
      en: Livy CPU Cores
    Description:
      zh-cn: Livy服务器的CPU核数。
      en: The number of CPU cores for the Livy server.
    AllowedValues:
      - '1'
      - '2'
      - '4'
    Default: '2'
  LivyMemoryLimit:
    Type: String
    Label:
      zh-cn: Livy内存大小
      en: Livy Memory Size
    Description:
      zh-cn: Livy服务器的内存大小。
      en: The memory size of the Livy server.
    AllowedValues:
      - 4Gi
      - 8Gi
      - 16Gi
    Default: 8Gi
  LivyIdleTimeoutMinutes:
    Type: Number
    Label:
      zh-cn: Livy空闲超时(分钟)
      en: Livy Idle Timeout (Minutes)
    Description:
      zh-cn: Livy网关空闲超时后自动停止的时间。
      en: The idle timeout in minutes before the Livy gateway automatically stops.
    Default: 60
    MinValue: 1
  EnableLivyPublicAccess:
    Type: String
    Label:
      zh-cn: 是否开启Livy公网访问
      en: Enable Livy Public Access
    Description:
      zh-cn: 是否为Livy网关开启公网访问端点。
      en: Whether to enable public endpoint for the Livy gateway.
    AllowedValues:
      - 'true'
      - 'false'
    Default: 'false'
Conditions:
  EnablePublic:
    Fn::Equals:
      - Ref: EnableLivyPublicAccess
      - 'true'
Resources:
  OssBucket:
    Type: ALIYUN::OSS::Bucket
    Properties:
      BucketName:
        Fn::Sub: ${CommonName}-${ALIYUN::AccountId}
      AccessControl: private
      DeletionForce: true
      ServerSideEncryptionConfiguration:
        SSEAlgorithm: AES256
      Tags:
        CreatedBy: ROS
        Purpose: SparkPlatform
  RamRole:
    Type: ALIYUN::RAM::Role
    Properties:
      RoleName:
        Fn::Sub: ${CommonName}-spark-role
      Description: RAM role for EMR Serverless Spark platform
      AssumeRolePolicyDocument:
        Version: '1'
        Statement:
          - Action: sts:AssumeRole
            Effect: Allow
            Principal:
              Service:
                - emr-serverless-spark.aliyuncs.com
      Policies:
        - PolicyName: AliyunEMRServerlessSparkFullAccess
          PolicyDocument:
            Version: '1'
            Statement:
              - Action:
                  - oss:GetObject
                  - oss:PutObject
                  - oss:DeleteObject
                  - oss:ListObjects
                  - oss:AbortMultipartUpload
                  - oss:ListMultipartUploads
                  - oss:GetBucketInfo
                Resource:
                  - Fn::Sub: acs:oss:*:*:${CommonName}-${ALIYUN::AccountId}
                  - Fn::Sub: acs:oss:*:*:${CommonName}-${ALIYUN::AccountId}/*
                Effect: Allow
  SparkWorkspace:
    Type: ALIYUN::EMR::SparkWorkspace
    DependsOn:
      - OssBucket
      - RamRole
    Properties:
      WorkspaceName:
        Fn::Sub: ${CommonName}-workspace
      OssBucket:
        Fn::Sub: oss://${CommonName}-${ALIYUN::AccountId}/spark-workspace/
      RamRoleName:
        Ref: RamRole
      PaymentType: PayAsYouGo
      ResourceSpec:
        Cu:
          Ref: Cu
        Gpu:
          Ref: GpuCount
      AutoStartSessionCluster: true
      Subscription:
        AutoRenew: true
        AutoRenewPeriod: '1'
        AutoRenewPeriodUnit: MONTH
        PaymentDurationUnit: MONTH
        Duration: '1'
      Tags:
        - Key: Environment
          Value: Production
        - Key: Team
          Value: DataEngineering
        - Key: CreatedBy
          Value: ROS
        - Key: CostCenter
          Value: BigData
  LivyGateway:
    Type: ALIYUN::EMR::LivyCompute
    DependsOn: SparkWorkspace
    Properties:
      WorkspaceBizId:
        Fn::GetAtt:
          - SparkWorkspace
          - WorkspaceId
      Name:
        Fn::Sub: ${CommonName}-livy
      DisplayReleaseVersion:
        Ref: SparkEngineVersion
      LivyVersion: 0.8.0
      AuthType: Token
      CpuLimit:
        Ref: LivyCpuLimit
      MemoryLimit:
        Ref: LivyMemoryLimit
      QueueName: root_queue
      EnablePublic:
        Ref: EnableLivyPublicAccess
      Fusion: true
      AutoStartConfiguration:
        Enable: true
      AutoStopConfiguration:
        Enable: true
        IdleTimeoutMinutes:
          Ref: LivyIdleTimeoutMinutes
Outputs:
  WorkspaceId:
    Description:
      zh-cn: Spark工作空间ID。
      en: Spark workspace ID.
    Label:
      zh-cn: 工作空间ID
      en: Workspace ID
    Value:
      Fn::GetAtt:
        - SparkWorkspace
        - WorkspaceId
  Storage:
    Description:
      zh-cn: 工作空间的OSS存储路径。
      en: The OSS storage path of the workspace.
    Label:
      zh-cn: OSS存储路径
      en: Storage Path
    Value:
      Fn::GetAtt:
        - SparkWorkspace
        - Storage
  OssBucketName:
    Description:
      zh-cn: OSS存储桶名称。
      en: OSS bucket name.
    Label:
      zh-cn: OSS存储桶名称
      en: OSS Bucket Name
    Value:
      Fn::GetAtt:
        - OssBucket
        - Name
  RamRoleArn:
    Description:
      zh-cn: RAM角色ARN。
      en: RAM role ARN.
    Label:
      zh-cn: RAM角色ARN
      en: RAM Role ARN
    Value:
      Fn::GetAtt:
        - RamRole
        - Arn
  LivyComputeId:
    Description:
      zh-cn: Livy网关ID。
      en: Livy gateway ID.
    Label:
      zh-cn: Livy网关ID
      en: Livy Gateway ID
    Value:
      Fn::GetAtt:
        - LivyGateway
        - LivyComputeId
  LivyStatus:
    Description:
      zh-cn: Livy网关状态。
      en: Livy gateway status.
    Label:
      zh-cn: Livy网关状态
      en: Livy Status
    Value:
      Fn::GetAtt:
        - LivyGateway
        - Status
  LivyEndpoint:
    Description:
      zh-cn: Livy网关公网访问地址。
      en: Livy gateway public endpoint.
    Label:
      zh-cn: Livy公网地址
      en: Livy Public Endpoint
    Value:
      Fn::GetAtt:
        - LivyGateway
        - Endpoint
  LivyEndpointInner:
    Description:
      zh-cn: Livy网关内网访问地址。
      en: Livy gateway internal endpoint.
    Label:
      zh-cn: Livy内网地址
      en: Livy Internal Endpoint
    Value:
      Fn::GetAtt:
        - LivyGateway
        - EndpointInner
Metadata:
  ALIYUN::ROS::Interface:
    ParameterGroups:
      - Parameters:
          - CommonName
          - Cu
          - GpuCount
      - Parameters:
          - SparkEngineVersion
          - LivyCpuLimit
          - LivyMemoryLimit
          - LivyIdleTimeoutMinutes
          - EnableLivyPublicAccess
{
  "ROSTemplateFormatVersion": "2015-09-01",
  "Description": {
    "zh-cn": "创建完整的EMR Serverless Spark数据分析平台,包含OSS存储桶、RAM角色、Spark工作空间(GPU加速+IP白名单+自动续费)、Livy网关(自动启停+公网访问),实现端到端的Spark大数据计算与交互式分析。",
    "en": "Create a complete EMR Serverless Spark data analysis platform including OSS bucket, RAM role, Spark workspace with GPU acceleration and IP whitelist, and Livy gateway with auto start/stop and public access for end-to-end Spark big data computing and interactive analysis."
  },
  "Parameters": {
    "CommonName": {
      "Type": "String",
      "Label": {
        "zh-cn": "资源名称前缀",
        "en": "Resource Name Prefix"
      },
      "Description": {
        "zh-cn": "所有资源名称的统一前缀。",
        "en": "Unified name prefix for all resources."
      },
      "Default": "spark-platform",
      "MinLength": 1,
      "MaxLength": 50
    },
    "Cu": {
      "Type": "String",
      "Label": {
        "zh-cn": "计算单元(CU)",
        "en": "Compute Units (CU)"
      },
      "Description": {
        "zh-cn": "工作空间的最大计算单元数量。",
        "en": "The maximum number of compute units for the workspace."
      },
      "Default": "50"
    },
    "GpuCount": {
      "Type": "Number",
      "Label": {
        "zh-cn": "GPU数量",
        "en": "GPU Count"
      },
      "Description": {
        "zh-cn": "工作空间的GPU数量。",
        "en": "The number of GPUs for the workspace."
      },
      "Default": 2,
      "MinValue": 0
    },
    "SparkEngineVersion": {
      "Type": "String",
      "Label": {
        "zh-cn": "Spark引擎版本",
        "en": "Spark Engine Version"
      },
      "Description": {
        "zh-cn": "Livy网关使用的Spark引擎版本。",
        "en": "The Spark engine version used by the Livy gateway."
      },
      "Default": "esr-4.2.0"
    },
    "LivyCpuLimit": {
      "Type": "String",
      "Label": {
        "zh-cn": "Livy CPU核数",
        "en": "Livy CPU Cores"
      },
      "Description": {
        "zh-cn": "Livy服务器的CPU核数。",
        "en": "The number of CPU cores for the Livy server."
      },
      "AllowedValues": [
        "1",
        "2",
        "4"
      ],
      "Default": "2"
    },
    "LivyMemoryLimit": {
      "Type": "String",
      "Label": {
        "zh-cn": "Livy内存大小",
        "en": "Livy Memory Size"
      },
      "Description": {
        "zh-cn": "Livy服务器的内存大小。",
        "en": "The memory size of the Livy server."
      },
      "AllowedValues": [
        "4Gi",
        "8Gi",
        "16Gi"
      ],
      "Default": "8Gi"
    },
    "LivyIdleTimeoutMinutes": {
      "Type": "Number",
      "Label": {
        "zh-cn": "Livy空闲超时(分钟)",
        "en": "Livy Idle Timeout (Minutes)"
      },
      "Description": {
        "zh-cn": "Livy网关空闲超时后自动停止的时间。",
        "en": "The idle timeout in minutes before the Livy gateway automatically stops."
      },
      "Default": 60,
      "MinValue": 1
    },
    "EnableLivyPublicAccess": {
      "Type": "String",
      "Label": {
        "zh-cn": "是否开启Livy公网访问",
        "en": "Enable Livy Public Access"
      },
      "Description": {
        "zh-cn": "是否为Livy网关开启公网访问端点。",
        "en": "Whether to enable public endpoint for the Livy gateway."
      },
      "AllowedValues": [
        "true",
        "false"
      ],
      "Default": "false"
    }
  },
  "Conditions": {
    "EnablePublic": {
      "Fn::Equals": [
        {
          "Ref": "EnableLivyPublicAccess"
        },
        "true"
      ]
    }
  },
  "Resources": {
    "OssBucket": {
      "Type": "ALIYUN::OSS::Bucket",
      "Properties": {
        "BucketName": {
          "Fn::Sub": "${CommonName}-${ALIYUN::AccountId}"
        },
        "AccessControl": "private",
        "DeletionForce": true,
        "ServerSideEncryptionConfiguration": {
          "SSEAlgorithm": "AES256"
        },
        "Tags": {
          "CreatedBy": "ROS",
          "Purpose": "SparkPlatform"
        }
      }
    },
    "RamRole": {
      "Type": "ALIYUN::RAM::Role",
      "Properties": {
        "RoleName": {
          "Fn::Sub": "${CommonName}-spark-role"
        },
        "Description": "RAM role for EMR Serverless Spark platform",
        "AssumeRolePolicyDocument": {
          "Version": "1",
          "Statement": [
            {
              "Action": "sts:AssumeRole",
              "Effect": "Allow",
              "Principal": {
                "Service": [
                  "emr-serverless-spark.aliyuncs.com"
                ]
              }
            }
          ]
        },
        "Policies": [
          {
            "PolicyName": "AliyunEMRServerlessSparkFullAccess",
            "PolicyDocument": {
              "Version": "1",
              "Statement": [
                {
                  "Action": [
                    "oss:GetObject",
                    "oss:PutObject",
                    "oss:DeleteObject",
                    "oss:ListObjects",
                    "oss:AbortMultipartUpload",
                    "oss:ListMultipartUploads",
                    "oss:GetBucketInfo"
                  ],
                  "Resource": [
                    {
                      "Fn::Sub": "acs:oss:*:*:${CommonName}-${ALIYUN::AccountId}"
                    },
                    {
                      "Fn::Sub": "acs:oss:*:*:${CommonName}-${ALIYUN::AccountId}/*"
                    }
                  ],
                  "Effect": "Allow"
                }
              ]
            }
          }
        ]
      }
    },
    "SparkWorkspace": {
      "Type": "ALIYUN::EMR::SparkWorkspace",
      "DependsOn": [
        "OssBucket",
        "RamRole"
      ],
      "Properties": {
        "WorkspaceName": {
          "Fn::Sub": "${CommonName}-workspace"
        },
        "OssBucket": {
          "Fn::Sub": "oss://${CommonName}-${ALIYUN::AccountId}/spark-workspace/"
        },
        "RamRoleName": {
          "Ref": "RamRole"
        },
        "PaymentType": "PayAsYouGo",
        "ResourceSpec": {
          "Cu": {
            "Ref": "Cu"
          },
          "Gpu": {
            "Ref": "GpuCount"
          }
        },
        "AutoStartSessionCluster": true,
        "Subscription": {
          "AutoRenew": true,
          "AutoRenewPeriod": "1",
          "AutoRenewPeriodUnit": "MONTH",
          "PaymentDurationUnit": "MONTH",
          "Duration": "1"
        },
        "Tags": [
          {
            "Key": "Environment",
            "Value": "Production"
          },
          {
            "Key": "Team",
            "Value": "DataEngineering"
          },
          {
            "Key": "CreatedBy",
            "Value": "ROS"
          },
          {
            "Key": "CostCenter",
            "Value": "BigData"
          }
        ]
      }
    },
    "LivyGateway": {
      "Type": "ALIYUN::EMR::LivyCompute",
      "DependsOn": "SparkWorkspace",
      "Properties": {
        "WorkspaceBizId": {
          "Fn::GetAtt": [
            "SparkWorkspace",
            "WorkspaceId"
          ]
        },
        "Name": {
          "Fn::Sub": "${CommonName}-livy"
        },
        "DisplayReleaseVersion": {
          "Ref": "SparkEngineVersion"
        },
        "LivyVersion": "0.8.0",
        "AuthType": "Token",
        "CpuLimit": {
          "Ref": "LivyCpuLimit"
        },
        "MemoryLimit": {
          "Ref": "LivyMemoryLimit"
        },
        "QueueName": "root_queue",
        "EnablePublic": {
          "Ref": "EnableLivyPublicAccess"
        },
        "Fusion": true,
        "AutoStartConfiguration": {
          "Enable": true
        },
        "AutoStopConfiguration": {
          "Enable": true,
          "IdleTimeoutMinutes": {
            "Ref": "LivyIdleTimeoutMinutes"
          }
        }
      }
    }
  },
  "Outputs": {
    "WorkspaceId": {
      "Description": {
        "zh-cn": "Spark工作空间ID。",
        "en": "Spark workspace ID."
      },
      "Label": {
        "zh-cn": "工作空间ID",
        "en": "Workspace ID"
      },
      "Value": {
        "Fn::GetAtt": [
          "SparkWorkspace",
          "WorkspaceId"
        ]
      }
    },
    "Storage": {
      "Description": {
        "zh-cn": "工作空间的OSS存储路径。",
        "en": "The OSS storage path of the workspace."
      },
      "Label": {
        "zh-cn": "OSS存储路径",
        "en": "Storage Path"
      },
      "Value": {
        "Fn::GetAtt": [
          "SparkWorkspace",
          "Storage"
        ]
      }
    },
    "OssBucketName": {
      "Description": {
        "zh-cn": "OSS存储桶名称。",
        "en": "OSS bucket name."
      },
      "Label": {
        "zh-cn": "OSS存储桶名称",
        "en": "OSS Bucket Name"
      },
      "Value": {
        "Fn::GetAtt": [
          "OssBucket",
          "Name"
        ]
      }
    },
    "RamRoleArn": {
      "Description": {
        "zh-cn": "RAM角色ARN。",
        "en": "RAM role ARN."
      },
      "Label": {
        "zh-cn": "RAM角色ARN",
        "en": "RAM Role ARN"
      },
      "Value": {
        "Fn::GetAtt": [
          "RamRole",
          "Arn"
        ]
      }
    },
    "LivyComputeId": {
      "Description": {
        "zh-cn": "Livy网关ID。",
        "en": "Livy gateway ID."
      },
      "Label": {
        "zh-cn": "Livy网关ID",
        "en": "Livy Gateway ID"
      },
      "Value": {
        "Fn::GetAtt": [
          "LivyGateway",
          "LivyComputeId"
        ]
      }
    },
    "LivyStatus": {
      "Description": {
        "zh-cn": "Livy网关状态。",
        "en": "Livy gateway status."
      },
      "Label": {
        "zh-cn": "Livy网关状态",
        "en": "Livy Status"
      },
      "Value": {
        "Fn::GetAtt": [
          "LivyGateway",
          "Status"
        ]
      }
    },
    "LivyEndpoint": {
      "Description": {
        "zh-cn": "Livy网关公网访问地址。",
        "en": "Livy gateway public endpoint."
      },
      "Label": {
        "zh-cn": "Livy公网地址",
        "en": "Livy Public Endpoint"
      },
      "Value": {
        "Fn::GetAtt": [
          "LivyGateway",
          "Endpoint"
        ]
      }
    },
    "LivyEndpointInner": {
      "Description": {
        "zh-cn": "Livy网关内网访问地址。",
        "en": "Livy gateway internal endpoint."
      },
      "Label": {
        "zh-cn": "Livy内网地址",
        "en": "Livy Internal Endpoint"
      },
      "Value": {
        "Fn::GetAtt": [
          "LivyGateway",
          "EndpointInner"
        ]
      }
    }
  },
  "Metadata": {
    "ALIYUN::ROS::Interface": {
      "ParameterGroups": [
        {
          "Parameters": [
            "CommonName",
            "Cu",
            "GpuCount"
          ]
        },
        {
          "Parameters": [
            "SparkEngineVersion",
            "LivyCpuLimit",
            "LivyMemoryLimit",
            "LivyIdleTimeoutMinutes",
            "EnableLivyPublicAccess"
          ]
        }
      ]
    }
  }
}