借助阿里云在亚洲加速迈向成功
一站式安全合规咨询服务
MLPS 2.0 一站式合规解决方案
依托我们的网络进军中国市场
提升面向互联网应用的性能和安全性
保障您的中国业务安全无忧
通过强大的数据安全框架保护您的数据资产
申请 ICP 备案的流程解读和咨询服务
面向大数据建设、管理及应用的全域解决方案
企业内大数据建设、管理和应用的一站式解决方案
将您的采购和销售置于同一企业级全渠道数字平台上
全渠道内置 AI 驱动、拟人化、多语言对话的聊天机器人
快速搭建在线教育平台
提供域名注册、分析和保护服务
云原生 Kubernetes 容器化应用运行环境
以 Kubernetes 为使用界面的容器服务产品,提供符合容器规范的算力资源
安全的镜像托管服务,支持全生命周期管理
多集群环境下微服务应用流量统一管理
提供任意基础设施上容器集群的统一管控,助您轻松管控分布式云场景
高弹性、高可靠的企业级无服务器 Kubernetes 容器产品
敏捷安全的 Serverless 容器运行服务
为虚拟机和容器提供高可靠性、高性能、低时延的块存储服务
一款海量、安全、低成本、高可靠的云存储服务
可靠、弹性、高性能、多共享的文件存储服务
全托管、可扩展的并行文件系统服务。
全托管的 NoSQL 结构化数据实时存储服务
可抵扣多种存储产品的容量包,兼具灵活性和长期成本优化
让您的应用跨不同可用区资源自动分配访问量
随时绑定和解绑 VPC ECS
云网络公网、跨域流量统一计费
高性价比,可抵扣按流量计费的流量费用
创建云上隔离的网络,在专有环境中运行资源
在 VPC 环境下构建公网流量的出入口
具备网络状态可视化、故障智能诊断能力的自助式网络运维服务。
安全便捷的云上服务专属连接
基于阿里云专有网络的私有 DNS 解析服务
保障在线业务不受大流量 DDoS 攻击影响
系统运维和安全审计管控平台
业务上云的第一个网络安全基础设施
集零信任内网访问、办公数据保护、终端管理等多功能于一体的办公安全管控平台
提供7X24小时安全运维平台
防御常见 Web 攻击,缓解 HTTP 泛洪攻击
实现全站 HTTPS,呈现可信的 WEB 访问
为云上应用提供符合行业标准和密码算法等级的数据加解密、签名验签和数据认证能力
一款发现、分类和保护敏感数据的安全服务
创建、控制和管理您的加密密钥
快速提高应用高可用能力服务
围绕应用和微服务的 PaaS 平台
兼容主流开源微服务生态的一站式平台
多集群环境下微服务应用流量统一管理
Super MySQL 和 PostgreSQL,高度兼容 Oracle 语法
全托管 MySQL、PostgreSQL、SQL Server、MariaDB
兼容 Redis® 的缓存和KV数据库
兼容Apache Cassandra、Apache HBase、Elasticsearch、OpenTSDB 等多种开源接口
文档型数据库,支持副本集和分片架构
100%兼容 Apache HBase 并深度扩展,稳定、易用、低成本的NoSQL数据库。
低成本、高可用、可弹性伸缩的在线时序数据库服务
专为搜索和分析而设计,成本效益达到开源的两倍,采用最新的企业级AI搜索和AI助手功能。
一款兼容PostgreSQL协议的实时交互式分析产品
一种快速、完全托管的 TB/PB 级数据仓库
基于 Flink 为大数据行业提供解决方案
基于Qwen和其他热门模型的一站式生成式AI平台,可构建了解您业务的智能应用程
一站式机器学习平台,满足数据挖掘分析需求
高性能向量检索服务,提供低代码API和高成本效益
帮助您的应用快速构建高质量的个性化推荐服务能力
提供定制化的高品质机器翻译服务
全面的AI计算平台,满足大模型训练等高性能AI计算的算力和性能需求
具备智能会话能力的会话机器人
基于机器学习的智能图像搜索产品
基于阿里云深度学习技术,为用户提供图像分割、视频分割、文字识别等离线SDK能力,支持Android、iOS不同的适用终端。
语音识别、语音合成服务以及自学习平台
一站式智能搜索业务开发平台
助力金融企业快速搭建超低时延、高质量、稳定的行情数据服务
帮助企业快速测算和分析企业的碳排放和产品碳足迹
企业工作流程自动化,全面提高效率
金融级云原生分布式架构的一站式高可用应用研发、运维平台
eKYC 数字远程在线解决方案
可智能检测、大数据驱动的综合性反洗钱 (AML) 解决方案
阿里云APM类监控产品
实时云监控服务,确保应用及服务器平稳运行
为系统运维人员管理云基础架构提供全方位服务的云上自动化运维平台
面向您的云资源的风险检测服务
提升分布式环境下的诊断效率
日志类数据一站式服务,无需开发就能部署
ECS 预留实例
让弹性计算产品的成本和灵活性达到最佳平衡的付费方式。云原生 AI 套件
加速AI平台构建,提高资源效率和交付速度FinOps
实时分析您的云消耗并实现节约SecOps
实施细粒度安全控制DevOps
快速、安全地最大限度提高您的DevOps优势自带IP上云
自带公网 IP 地址上云全球网络互联
端到端的软件定义网络解决方案,可推动跨国企业的业务发展全球应用加速
提升面向互联网应用的性能和安全性全球互联网接入
将IDC网关迁移到云端云原生 AI 套件
加速AI平台构建,提高资源效率和交付速度FinOps
实时分析您的云消耗并实现节约SecOps
实施细粒度安全控制DevOps
快速、安全地最大限度提高您的DevOps优势金融科技云数据库解决方案
利用专为金融科技而设的云原生数据库解决方案游戏行业云数据库解决方案
提供多种成熟架构,解决所有数据问题Oracle 数据库迁移
将 Oracle 数据库顺利迁移到云原生数据库数据库迁移
加速迁移您的数据到阿里云阿里云上的数据湖
实时存储、管理和分析各种规模和类型的数据数码信贷
利用大数据和 AI 降低信贷和黑灰产风险面向企业数据技术的大数据咨询服务
帮助企业实现数据现代化并规划其数字化未来人工智能对话服务
全渠道内置 AI 驱动、拟人化、多语言对话的聊天机器人EasyDispatch 现场服务管理
为现场服务调度提供实时AI决策支持在线教育
快速搭建在线教育平台窄带高清 (HD) 转码
带宽成本降低高达 30%广电级大型赛事直播
为全球观众实时直播大型赛事,视频播放流畅不卡顿直播电商
快速轻松地搭建一站式直播购物平台用于供应链规划的Alibaba Dchain
构建和管理敏捷、智能且经济高效的供应链云胸牌
针对赛事运营的创新型凭证数字服务数字门店中的云 POS 解决方案
将所有操作整合到一个云 POS 系统中元宇宙
元宇宙是下一代互联网人工智能 (AI) 加速
利用阿里云 GPU 技术,为 AI 驱动型业务以及 AI 模型训练和推理加速DevOps
快速、安全地最大限度提高您的DevOps优势数据迁移解决方案
加速迁移您的数据到阿里云企业 IT 治理
在阿里云上构建高效可控的云环境基于日志管理的AIOps
登录到带有智能化日志管理解决方案的 AIOps 环境备份与存档
数据备份、数据存档和灾难恢复用阿里云金融服务加快创新
在云端开展业务,提升客户满意度
为全球资本市场提供安全、准确和数字化的客户体验
利用专为金融科技而设的云原生数据库解决方案
利用大数据和 AI 降低信贷和黑灰产风险
建立快速、安全的全球外汇交易平台
新零售时代下,实现传统零售业转型
利用云服务处理流量波动问题,扩展业务运营、降低成本
快速轻松地搭建一站式直播购物平台
面向大数据建设、管理及应用的全域解决方案
全渠道内置 AI 驱动、拟人化、多语言对话的聊天机器人
以数字化媒体旅程为当今的媒体市场准备就绪您的内容
带宽成本降低高达 30%
快速轻松地搭建一站式直播购物平台
为全球观众实时直播大型赛事,视频播放流畅不卡顿
使用阿里云弹性高性能计算 E-HPC 将本地渲染农场连接到云端
构建发现服务,帮助客户找到最合适的内容
保护您的媒体存档安全
通过统一的数据驱动平台提供一致的全生命周期客户服务
在钉钉上打造一个多功能的电信和数字生活平台
在线存储、共享和管理照片与文件
提供全渠道的无缝客户体验
面向中小型企业,为独立软件供应商提供可靠的IT服务
打造最快途径,助力您的新云业务扬帆起航
先进的SD-WAN平台,可实现WAN连接、实时优化并降低WAN成本
通过自动化和流程标准化实现快速事件响应
针对关键网络安全威胁提供集中可见性并进行智能安全分析
提供大容量、可靠且高度安全的企业文件传输
用智能技术数字化体育赛事
基于人工智能的低成本体育广播服务
专业的广播转码及信号分配管理服务
基于云的音视频内容引入、编辑和分发服务
在虚拟场馆中模拟关键运营任务
针对赛事运营的创新型凭证数字服务
智能和交互式赛事指南
轻松管理云端背包单元的绑定直播流
通过数据加强您的营销工作
元宇宙是下一代互联网
利用生成式 AI 加速创新,创造新的业务佳绩
阿里云高性能开源大模型
借助AI轻松解锁和提炼文档中的知识
通过AI驱动的语音转文本服务获取洞察
探索阿里云人工智能和数据智能的所有功能、新优惠和最新产品
该体验中心提供广泛的用例和产品帮助文档,助您开始使用阿里云 AI 产品和浏览您的业务数据。
利用阿里云 GPU 技术,为 AI 驱动型业务以及 AI 模型训练和推理加速
元宇宙是下一代互联网
构建发现服务,帮助客户找到最合适的内容
全渠道内置 AI 驱动、拟人化、多语言对话的聊天机器人
加速迁移您的数据到阿里云
在阿里云上建立一个安全且易扩容的环境,助力高效率且高成本效益的上云旅程
迁移到完全托管的云数据库
将 Oracle 数据库顺利迁移到云原生数据库
自带公网 IP 地址上云
利用阿里云强大的安全工具集,保障业务安全、应用程序安全、数据安全、基础设施安全和帐户安全
保护、备份和还原您的云端数据资产
MLPS 2.0 一站式合规解决方案
快速高效地将您的业务扩展到中国,同时遵守适用的当地法规
实现对 CloudOps、DevOps、SecOps、AIOps 和 FinOps 的高效、安全和透明的管理
构建您的原生云环境并高效管理集群
快速、安全地最大限度提高您的DevOps优势
实施细粒度安全控制
提供运维效率和总体系统安全性
实时分析您的云消耗并实现节约
实时存储、管理和分析各种规模和类型的数据
登录到带有智能化日志管理解决方案的 AIOps 环境
帮助企业实现数据现代化并规划其数字化未来
帮助零售商快速规划数字化之旅
将全球知名的 CRM 平台引入中国
在线存储、共享和管理照片与文件
构建、部署和管理高可用、高可靠、高弹性的应用程序
快速、安全地最大限度提高您的DevOps优势
将您的采购和销售置于同一企业级全渠道数字平台上
企业内大数据建设、管理和应用的一站式解决方案
帮助企业简化 IT 架构、实现商业价值、加速数字化转型的步伐
快速高效地将您的业务扩展到中国,同时遵守适用的当地法规
快速搜集、处理、分析联网设备产生的数据
0.0.201
本文主要介绍如何基于ROS CDK新建一台ECS,在其上部署一个2048小游戏,并在本地浏览器游玩。本教程支持多种编程语言,包括TypeScript、JavaScript、Java、Python、C#和Go。
ROS CDK本身不收费,但本方案需要部署ECS实例,会产生相关的资源费用。具体费用信息,请参见按量付费。
请确保您已经完成以下操作:
安装和配置阿里云CLI、安装ROS CDK。具体操作,请参见安装ROS CDK。请确保您安装的ros-cdk-cli
版本在 1.4.0 以上,可以通过运行 npm list -g
确认。
获取AccessKey信息,用于后续授权。如果没有,请参见创建AccessKey。
开发语言环境要求 。
TypeScript:Node.js 8.0或更高版本。
JavaScript:JSON解析工具包(工具包名称为jq
)。
Java:JDK 1.8或以上版本、Maven 2.5或以上版本。
Python:Python 3.4以上版本。
C#:.NET Core 2.0以上版本、Visual Studio 2010 及以上版本。
Go:1.16或以上版本。
在命令行窗口依次输入以下命令,在当前目录创建并初始化工程。
mkdir demo
cd demo
ros-cdk init --language=typescript --generate-only=true
mkdir demo
cd demo
ros-cdk init --language=javascript --generate-only=true
mkdir demo
cd demo
ros-cdk init --language=java --generate-only=true
mkdir demo
cd demo
ros-cdk init --language=python --generate-only=true
执行以下命令,创建一个属于当前工程的虚拟环境。
Python工程的运行依赖于虚拟环境(virtualenv),所以在初始化Python工程之后需要创建一个属于当前工程的虚拟环境。
python3 -m venv .venv
执行以下命令,进入虚拟环境。
source .venv/bin/activate
mkdir demo
cd demo
ros-cdk init --language=csharp --generate-only=true
mkdir demo
cd demo
ros-cdk init --language=go --generate-only=true
在命令行窗口执行以下命令,配置阿里云凭证信息。
ros-cdk config
根据界面提示依次输入配置信息。
endpoint(optional, default:https://ros.aliyuncs.com):
defaultRegionId(optional, default:cn-hangzhou):cn-beijing
[1] AK
[2] StsToken
[3] RamRoleArn
[4] EcsRamRole
[0] CANCEL
Authenticate mode [1...4 / 0]: 1
accessKeyId:************************
accessKeySecret:******************************
Your cdk configuration has been saved successfully!
配置内容说明如下:
参数 | 说明 |
参数 | 说明 |
defaultRegionId | ROS资源栈部署的地域。默认值为 |
endpoint | ROS服务地址。默认值为 |
Authenticate mode | 鉴权方式。本示例的鉴权方式为[1] AK,您需要输入准备工作中获取的AccessKey ID和AccessKey Secret。更多鉴权方式,请参见配置凭证。 |
在工程中修改配置文件,并安装依赖。
将package.json
文件内容,全部更改为下面代码。
{
"name": "demo",
"version": "0.1.0",
"bin": {
"demo": "bin/demo.js"
},
"scripts": {
"build": "tsc",
"test": "jest"
},
"devDependencies": {
"@types/jest": "^25.2.1",
"@types/node": "10.17.5",
"typescript": "^3.9.7",
"jest": "^25.5.0",
"ts-jest": "^25.3.1",
"ts-node": "^8.1.0",
"babel-jest": "^26.6.3",
"@babel/core": "^7.12.9",
"@babel/preset-env": "7.12.7",
"@babel/preset-typescript": "^7.12.7",
"@alicloud/ros-cdk-assert": "^1.4.0"
},
"dependencies": {
"@alicloud/ros-cdk-core": "^1.4.0",
"@alicloud/ros-cdk-ecs": "^1.4.0"
}
}
在命令行窗口执行以下命令,安装依赖。
npm install
将package.json
文件内容,全部更改为下面代码。
{
"name": "demo",
"version": "0.1.0",
"bin": {
"demo": "bin/demo.js"
},
"scripts": {
"build": "echo \"The build step is not required when using JavaScript!\" && exit 0",
"cdk": "cdk",
"test": "jest"
},
"devDependencies": {
"@types/jest": "^25.2.1",
"@types/node": "10.17.5",
"typescript": "^3.9.7",
"jest": "^25.5.0",
"ts-jest": "^25.3.1",
"ts-node": "^8.1.0",
"babel-jest": "^26.6.3",
"@babel/core": "^7.12.9",
"@babel/preset-env": "7.12.7",
"@babel/preset-typescript": "^7.12.7",
"@alicloud/ros-cdk-assert": "^1.1.0"
},
"dependencies": {
"@alicloud/ros-cdk-core": "^1.4.0",
"@alicloud/ros-cdk-ecs": "^1.4.0",
"@alicloud/ros-cdk-assert": "^1.4.0"
}
}
在命令行窗口执行以下命令,安装依赖。
npm install
将pom.xml
文件内容,全部更改为下面代码。
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myorg</groupId>
<artifactId>demo</artifactId>
<version>0.1</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<cdk.version>1.1.0</cdk.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>com.myorg.DemoApp</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- AliCloud ROS Cloud Development Kit (ROS CDK) -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>ros-cdk-core</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>ros-cdk-ecs</artifactId>
<version>1.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
执行以下命令,安装依赖:
mvn compile
将requirements.txt
文件内容,全部更改为下面代码。
ros-cdk-core==1.4.0
ros-cdk-ecs==1.4.0
在命令行窗口执行以下命令,安装依赖。
pip install -r requirements.txt
将src/Demo/Demo.csproj
文件内容,全部更改为下面代码(请根据您本地dotnet核心包版本修改)。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<!-- Roll forward to future major versions of the netcoreapp as needed -->
<RollForward>Major</RollForward>
</PropertyGroup>
<ItemGroup>
<!-- CDK Construct Library dependencies -->
<PackageReference Include="AlibabaCloud.SDK.ROS.CDK.Core" Version="1.4.0" />
<PackageReference Include="AlibabaCloud.SDK.ROS.CDK.Ecs" Version="1.4.0" />
<!-- jsii Roslyn analyzers (un-comment to obtain compile-time checks for missing required props
<PackageReference Include="Amazon.Jsii.Analyzers" Version="*" PrivateAssets="all" />
-->
</ItemGroup>
</Project>
将go.mod
文件内容,全部修改为下面代码(请根据您的Go语言版本修改)。
module demo
go 1.18
require (
github.com/alibabacloud-go/ros-cdk/alicloudroscdkcore v1.4.0
github.com/alibabacloud-go/ros-cdk/alicloudroscdkecs v1.4.0
github.com/stretchr/testify v1.8.4
)
在命令行窗口执行以下命令,安装依赖。
go get
为项目添加资源,本示例涉及到的资源有:
将lib/demo-stack.ts
文件内容,全部改为下面代码。
import * as ros from '@alicloud/ros-cdk-core';
import * as ecs from '@alicloud/ros-cdk-ecs';
export class DemoStack extends ros.Stack {
constructor(scope: ros.Construct, id: string, props?: ros.StackProps) {
super(scope, id, props);
new ros.RosInfo(this, ros.RosInfo.description, "Deploy 2048 game through ROS CDK.");
const zoneId = new ros.RosParameter(this, 'ZoneId', {
type: ros.RosParameterType.STRING
});
const vpc = new ecs.Vpc(this, 'Vpc', {
cidrBlock: '192.168.0.0/16'
});
const sg = new ecs.SecurityGroup(this, 'SecurityGroup', {
vpcId: vpc.ref
});
const vsw = new ecs.VSwitch(this, 'VSwitch', {
cidrBlock: '192.168.0.0/24',
zoneId: zoneId,
vpcId: vpc.ref
})
const instance = new ecs.Instance(this, 'Instance', {
vpcId: vpc.ref,
vSwitchId: vsw.ref,
securityGroupId: sg.ref,
zoneId: zoneId,
instanceType: 'ecs.c6e.large',
imageId: 'centos_7_8',
systemDiskCategory: 'cloud_essd',
ioOptimized: 'optimized'
});
const sgIngress = new ecs.SecurityGroupIngress(this, 'SecurityGroupIngress', {
ipProtocol: 'tcp',
portRange: '80/80',
sourceCidrIp: '0.0.0.0/0',
securityGroupId: sg.ref,
nicType: 'intranet'
});
const runCommand = new ecs.RunCommand(this, 'RunCommand', {
instanceIds: [instance.ref],
type: 'RunShellScript',
sync: true,
timeout: 3600,
commandContent: `yum install -y httpd wget\nsystemctl start httpd\nwget 'https://computenest-artifacts-cn-hangzhou.oss-cn-hangzhou-internal.aliyuncs.com/1853370294850618/cn-beijing/1697533575326/2048.tgz' -O 2048.tgz\ntar xvf 2048.tgz\nmv 2048/* /var/www/html && rm -rf 2048`
});
runCommand.addDependency(sgIngress);
new ros.RosOutput(this, 'Url', {
value: `http://${instance.attrPublicIp}:80`
});
}
}
将lib/demo-stack.js
文件内容,全部改为下面代码。
const ros = require('@alicloud/ros-cdk-core');
const ecs = require('@alicloud/ros-cdk-ecs');
class DemoStack extends ros.Stack {
/**
*
* @param {ros.Construct} scope
* @param {string} id
* @param {ros.StackProps} props
*/
constructor(scope, id, props) {
super(scope, id, props);
new ros.RosInfo(this, ros.RosInfo.description, "Deploy 2048 game through ROS CDK.");
// The code that defines your stack goes here
const zoneId = new ros.RosParameter(this, 'ZoneId', {
type: ros.RosParameterType.STRING
});
const vpc = new ecs.Vpc(this, 'Vpc', {
cidrBlock: '192.168.0.0/16'
});
const sg = new ecs.SecurityGroup(this, 'SecurityGroup', {
vpcId: vpc.ref
});
const vsw = new ecs.VSwitch(this, 'VSwitch', {
cidrBlock: '192.168.0.0/24',
zoneId: zoneId,
vpcId: vpc.ref
})
const instance = new ecs.Instance(this, 'Instance', {
vpcId: vpc.ref,
vSwitchId: vsw.ref,
securityGroupId: sg.ref,
zoneId: zoneId,
instanceType: 'ecs.c6e.large',
imageId: 'centos_7_8',
systemDiskCategory: 'cloud_essd',
ioOptimized: 'optimized'
});
const sgIngress = new ecs.SecurityGroupIngress(this, 'SecurityGroupIngress', {
ipProtocol: 'tcp',
portRange: '80/80',
sourceCidrIp: '0.0.0.0/0',
securityGroupId: sg.ref,
nicType: 'intranet'
});
const runCommand = new ecs.RunCommand(this, 'RunCommand', {
instanceIds: [instance.ref],
type: 'RunShellScript',
sync: true,
timeout: 3600,
commandContent: `yum install -y httpd wget\nsystemctl start httpd\nwget 'https://computenest-artifacts-cn-hangzhou.oss-cn-hangzhou-internal.aliyuncs.com/1853370294850618/cn-beijing/1697533575326/2048.tgz' -O 2048.tgz\ntar xvf 2048.tgz\nmv 2048/* /var/www/html && rm -rf 2048`
});
runCommand.addDependency(sgIngress);
new ros.RosOutput(this, 'Url', {
value: `http://${instance.attrPublicIp}:80`
});
}
}
module.exports = { DemoStack }
请将folderPath
变量设置为你的本地文件夹路径,同时,将ossBucketName
变量指定为你计划创建的OSS Bucket名称,Bucket 名称必须全局唯一!
将src/main/java/com/myorg/DemoStack.java
文件内容,全部改为下面代码。
package com.myorg;
import com.aliyun.ros.cdk.core.*;
import com.aliyun.ros.cdk.ecs.*;
import java.util.ArrayList;
import java.util.Arrays;
public class DemoStack extends Stack {
public DemoStack(final Construct scope, final String id) {
this(scope, id, null);
}
public DemoStack(final Construct scope, final String id, final StackProps props) {
super(scope, id, props);
// The code that defines your stack goes here
RosInfo.setDescription("Deploy 2048 game through ROS CDK.");
RosParameter zoneId = RosParameter.Builder.create(this, "ZoneId")
.type(RosParameterType.STRING)
.build();
Vpc vpc = Vpc.Builder.create(this, "Vpc")
.cidrBlock("192.168.0.0/16")
.build();
SecurityGroup securityGroup = SecurityGroup.Builder.create(this, "SecurityGroup")
.vpcId(vpc.getRef())
.build();
VSwitch vSwitch = VSwitch.Builder.create(this, "VSwitch")
.cidrBlock("192.168.0.0/24")
.zoneId(zoneId.getValue())
.vpcId(vpc.getRef())
.build();
Instance instance = Instance.Builder.create(this, "Instance")
.vpcId(vpc.getRef())
.vSwitchId(vSwitch.getRef())
.securityGroupId(securityGroup.getRef())
.zoneId(zoneId.getValue())
.instanceType("ecs.c6e.large")
.imageId("centos_7_8")
.systemDiskCategory("cloud_essd")
.ioOptimized("optimized")
.build();
SecurityGroupIngress securityGroupIngress = SecurityGroupIngress.Builder.create(this, "SecurityGroupIngress")
.ipProtocol("tcp")
.portRange("80/80")
.sourceCidrIp("0.0.0.0/0")
.securityGroupId(securityGroup.getRef())
.nicType("intranet")
.build();
RunCommand runCommand = RunCommand.Builder.create(this, "RunCommand")
.instanceIds(new ArrayList<>(Arrays.asList(instance.getRef())))
.type("RunShellScript")
.sync(true)
.timeout(3600)
.commandContent("yum install -y httpd wget\nsystemctl start httpd\nwget 'https://computenest-artifacts-cn-hangzhou.oss-cn-hangzhou-internal.aliyuncs.com/1853370294850618/cn-beijing/1697533575326/2048.tgz' -O 2048.tgz\ntar xvf 2048.tgz\nmv 2048/* /var/www/html && rm -rf 2048")
.build();
runCommand.addDependency(securityGroupIngress);
RosOutput.Builder.create(this, "Url")
.value("http://" + instance.getAttrPublicIp().toString() +":80")
.build();
}
}
运行mvn compile
以编译CDK项目。
将demo/demo_stack.py
文件内容,全部改为下面代码。
import ros_cdk_core as core
import ros_cdk_ecs as ecs
class DemoStack(core.Stack):
def __init__(self, scope: core.Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
# The code that defines your stack goes here
core.RosInfo(self, core.RosInfo.description, "Deploy 2048 game through ROS CDK.")
zone_id = core.RosParameter(self, "ZoneId", type=core.RosParameterType.STRING)
vpc = ecs.Vpc(self, "Vpc", ecs.VPCProps(cidr_block="192.168.0.0/16"))
sg = ecs.SecurityGroup(self, "SecurityGroup", ecs.SecurityGroupProps(vpc_id=vpc.ref))
vsw = ecs.VSwitch(self, "VSwitch", ecs.VSwitchProps(
cidr_block='192.168.0.0/24',
vpc_id=vpc.ref,
zone_id=zone_id,
))
instance = ecs.Instance(self, "Instance", ecs.InstanceProps(
vpc_id=vpc.ref,
instance_type="ecs.c6e.large",
image_id="centos_7_8",
security_group_id=sg.ref,
v_switch_id=vsw.ref,
zone_id=zone_id,
system_disk_category="cloud_essd",
io_optimized="optimized"
))
sg_ingress = ecs.SecurityGroupIngress(self, "SecurityGroupIngress", ecs.SecurityGroupIngressProps(
security_group_id=sg.ref,
port_range="80/80",
ip_protocol="tcp",
source_cidr_ip="0.0.0.0/0",
nic_type="intranet"
))
run_command = ecs.RunCommand(self, "RunCommand", ecs.RunCommandProps(
instance_ids=[instance.ref],
command_content= "yum install -y httpd wget\nsystemctl start httpd\nwget 'https://computenest-artifacts-cn-hangzhou.oss-cn-hangzhou-internal.aliyuncs.com/1853370294850618/cn-beijing/1697533575326/2048.tgz' -O 2048.tgz\ntar xvf 2048.tgz\nmv 2048/* /var/www/html && rm -rf 2048",
type="RunShellScript",
timeout=3600,
sync=True
))
run_command.add_dependency(sg_ingress)
core.RosOutput(self, "Url", value="http://{}:80".format(instance.attr_public_ip.to_string()))
将src/Demo/DemoStack.cs
文件内容,全部改为下面代码。
using System.Collections.Generic;
using AlibabaCloud.SDK.ROS.CDK.Core;
using AlibabaCloud.SDK.ROS.CDK.Ecs;
namespace Demo
{
public class DemoStack : Stack
{
public DemoStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props)
{
// The code that defines your stack goes here
new RosInfo(this, RosInfo.Description, "Deploy 2048 game through ROS CDK.");
var zoneId = new RosParameter(this, "ZoneId", new RosParameterProps{
Type = RosParameterType.STRING
});
var vpc = new Vpc(this, "Vpc", new VPCProps{
CidrBlock = "192.168.0.0/16"
});
var sg = new SecurityGroup(this, "SecurityGroup", new SecurityGroupProps{
VpcId = vpc.Ref
});
var vsw = new VSwitch(this, "VSwitch", new VSwitchProps{
CidrBlock = "192.168.0.0/24",
ZoneId = zoneId.Value,
VpcId = vpc.Ref
});
var instance = new Instance(this, "Instance", new InstanceProps{
VpcId = vpc.Ref,
VSwitchId = vsw.Ref,
SecurityGroupId = sg.Ref,
ZoneId = zoneId.Value,
InstanceType = "ecs.c6e.large",
ImageId = "centos_7_8",
SystemDiskCategory = "cloud_essd",
IoOptimized = "optimized"
});
var sgIngress = new SecurityGroupIngress(this, "SecurityGroupIngress", new SecurityGroupIngressProps{
IpProtocol = "tcp",
PortRange = "80/80",
SourceCidrIp = "0.0.0.0/0",
SecurityGroupId = sg.Ref,
NicType = "intranet"
});
var runCommand = new RunCommand(this, "RunCommand", new RunCommandProps{
InstanceIds = new IResolvable[] { instance.AttrInstanceId },
CommandContent = "yum install -y httpd wget\nsystemctl start httpd\nwget 'https://computenest-artifacts-cn-hangzhou.oss-cn-hangzhou-internal.aliyuncs.com/1853370294850618/cn-beijing/1697533575326/2048.tgz' -O 2048.tgz\ntar xvf 2048.tgz\nmv 2048/* /var/www/html && rm -rf 2048",
Type = "RunShellScript",
Timeout = 3600,
Sync = true
});
runCommand.AddDependency(sgIngress);
new RosOutput(this, "Url", new RosOutputProps{
Value = "http://" + instance.AttrPublicIp.ToString() + ":80"
});
}
}
}
将demo.go
文件内容,全部改为下面代码。
package main
import (
"github.com/alibabacloud-go/ros-cdk/alicloudroscdkcore"
"github.com/alibabacloud-go/ros-cdk/alicloudroscdkecs"
)
type demoStackProps struct {
alicloudroscdkcore.StackProps
}
func NewdemoStack(scope alicloudroscdkcore.Construct, id string, props *demoStackProps) alicloudroscdkcore.Stack {
var sprops alicloudroscdkcore.StackProps
if props != nil {
sprops = props.StackProps
}
stack := alicloudroscdkcore.NewStack(scope, &id, &sprops)
// The code that defines your stack goes here
description := "Deploy 2048 game through ROS CDK."
alicloudroscdkcore.RosInfo_SetDescription(&description)
zoneIdKey := "ZoneId"
zoneIdProps := alicloudroscdkcore.RosParameterProps{Type: alicloudroscdkcore.RosParameterType_STRING}
zoneId := alicloudroscdkcore.NewRosParameter(stack, &zoneIdKey, &zoneIdProps)
vpcKey := "Vpc"
vpcProps := alicloudroscdkecs.VPCProps{CidrBlock: "192.168.0.0/16"}
vpc := alicloudroscdkecs.NewVpc(stack, &vpcKey, &vpcProps, nil)
sgKey := "SecurityGroup"
sgProps := alicloudroscdkecs.SecurityGroupProps{VpcId: vpc.Ref()}
sg := alicloudroscdkecs.NewSecurityGroup(stack, &sgKey, &sgProps, nil)
vswKey := "VSwitch"
vswProps := alicloudroscdkecs.VSwitchProps{
CidrBlock: "192.168.0.0/24",
ZoneId: zoneId.Value(),
VpcId: vpc.Ref(),
}
vsw := alicloudroscdkecs.NewVSwitch(stack, &vswKey, &vswProps, nil)
instanceKey := "Instance"
instanceProps := alicloudroscdkecs.InstanceProps{
VpcId: vpc.Ref(),
VSwitchId: vsw.Ref(),
SecurityGroupId: sg.Ref(),
ZoneId: zoneId.Value(),
InstanceType: "ecs.c6e.large",
ImageId: "centos_7_8",
SystemDiskCategory: "cloud_essd",
IoOptimized: "optimized",
}
instance := alicloudroscdkecs.NewInstance(stack, &instanceKey, &instanceProps, nil)
sgIngressKey := "SecurityGroupIngress"
sgIngressProps := alicloudroscdkecs.SecurityGroupIngressProps{
IpProtocol: "tcp",
PortRange: "80/80",
SourceCidrIp: "0.0.0.0/0",
SecurityGroupId: sg.Ref(),
NicType: "intranet",
}
sgIngress := alicloudroscdkecs.NewSecurityGroupIngress(stack, &sgIngressKey, &sgIngressProps, nil)
var instanceIds []*string
runCommandKey := "RunCommand"
runCommandProps := alicloudroscdkecs.RunCommandProps{
InstanceIds: append(instanceIds, instance.Ref()),
Type: "RunShellScript",
Sync: true,
Timeout: 3600,
CommandContent: "yum install -y httpd wget\nsystemctl start httpd\nwget 'https://computenest-artifacts-cn-hangzhou.oss-cn-hangzhou-internal.aliyuncs.com/1853370294850618/cn-beijing/1697533575326/2048.tgz' -O 2048.tgz\ntar xvf 2048.tgz\nmv 2048/* /var/www/html && rm -rf 2048",
}
runCommand := alicloudroscdkecs.NewRunCommand(stack, &runCommandKey, &runCommandProps, nil)
runCommand.AddDependency(sgIngress)
urlKey := "Url"
urlProps := alicloudroscdkcore.RosOutputProps{Value: "http://" + *instance.AttrPublicIp().ToString() + ":80"}
alicloudroscdkcore.NewRosOutput(stack, &urlKey, &urlProps)
return stack
}
func main() {
app := alicloudroscdkcore.NewApp(nil)
props := &demoStackProps{}
NewdemoStack(app, "demoStack", props)
app.Synth(nil)
}
在命令行窗口运行如下命令,创建资源栈。
ros-cdk deploy --parameters ZoneId=cn-hangzhou-i --sync=true
请选择合适的可用区进行部署。命令行终端返回The deployment(sync create stack) has completed!
表示创建成功,全部信息如下。
Create bucket(cdk-tcbgaxxxx-assets-cn-hangzhou) successfully!
Upload file(./cdk.out/DemoStack.template.json) to bucket(cdk-tcbgaxxxx-assets-cn-hangzhou) successfully!
DemoStack: deploying...
|DemoStack |2024-09-09T08:56:47 | CREATE_COMPLETE | ALIYUN::ECS::SecurityGroup | sg-bp121z2wd687mdxxxxxx | SecurityGroup
|DemoStack |2024-09-09T08:56:47 | CREATE_COMPLETE | ALIYUN::ECS::SecurityGroupIngress | sg-bp121z2wd687mdxxxxxx | SecurityGroupIngress
|DemoStack |2024-09-09T08:56:47 | CREATE_COMPLETE | ALIYUN::ECS::Instance | i-bp16t9mt9b0uscixxxxx | Instance
|DemoStack |2024-09-09T08:56:47 | CREATE_COMPLETE | ALIYUN::ECS::VPC | vpc-bp10y18ke2x6v0bjxxxxx | Vpc
|DemoStack |2024-09-09T08:56:47 | CREATE_COMPLETE | ALIYUN::ECS::VSwitch | vsw-bp1qfb9u90cuyu69xxxxx | VSwitch
|DemoStack |2024-09-09T08:56:47 | CREATE_COMPLETE | ALIYUN::ECS::RunCommand | t-hz04w6ckiuxxxxx | RunCommand
Outputs:
Key: Url Value: http://121.40.xxx.xx:80 Description: No description given
✅ The deployment(sync create stack) has completed!
Status: CREATE_COMPLETE
StatusReason: Stack CREATE completed successfully
StackId: 40c92773-33fb-4249-bd46-3ab6xxxxxxxx
部署完成后,复制Outputs中的链接并在浏览器中打开,即可畅玩2048。
命令行窗口执行以下命令以删除资源栈。
ros-cdk destroy --sync=true
CDK会二次确认即将删除的资源栈。
The following stack(s) will be destroyed(Only deployed stacks will be displayed).
DemoStack
Please confirm.(Y/N)
输入y
并回车,删除成功会显示如下信息。
The task(sync destroy stack) has finished!
status: DELETE_COMPLETE
StatusReason: Stack DELETE completed successfully
StackId: 40c92773-33fb-4249-bd46-3ab6xxxxxxxx
CDK构造库提供了一系列API,用于构建您的CDK应用程序。想要了解如何使用这些API及其特性,请参阅ROS CDK API参考。
想要了解如何使用CDK命令行及其使用示例,请参阅ROS CDK命令简介。
想要了解如何使用CDK输出、伪参数等功能及其使用示例,请参阅ROS CDK功能简介。
有关ROS CDK的官方GitHub存储库,请参阅Resource-Orchestration-Service-Cloud-Development-Kit。在这里,您可以提交问题,查看我们的许可证、发布记录等。