

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用自动创建 Amazon WorkSpaces 应用程序资源 AWS CloudFormation
<a name="automate-the-creation-of-appstream-2-0-resources-using-aws-cloudformation"></a>

*Ram Kandaswamy，Amazon Web Services*

## Summary
<a name="automate-the-creation-of-appstream-2-0-resources-using-aws-cloudformation-summary"></a>

此模式提供了使用[AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html)模板在中自动创建 [Amazon A WorkSpaces pp](https://aws.amazon.com/workspaces/applications/) lication AWS Cloud s 资源的代码示例和步骤。该模式向您展示如何使用 CloudFormation 堆栈自动创建 WorkSpaces 应用程序资源，包括映像生成器、映像、队列实例和堆栈。您可以使用桌面或 WorkSpaces 应用程序交付模式，在 HTML5兼容的浏览器上将应用程序流式传输给最终用户。

## 先决条件和限制
<a name="automate-the-creation-of-appstream-2-0-resources-using-aws-cloudformation-prereqs"></a>

**先决条件**
+ 活跃的 AWS 账户
+ 接受 WorkSpaces 应用程序条款和条件
+  WorkSpaces [应用程序资源的基础知识，例如[队列和堆栈以及映](https://docs.aws.amazon.com/appstream2/latest/developerguide/managing-stacks-fleets.html)像生成器](https://docs.aws.amazon.com/appstream2/latest/developerguide/managing-image-builders.html)

**限制**
+ 创建 WorkSpaces 应用程序实例后，您无法修改与该实例关联的 [AWS Identity and Access Management](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html)(IAM) 角色。
+ 创建 WorkSpaces 应用镜像生成器实例后，您无法修改该映像生成器实例的属性（例如[子网](https://docs.aws.amazon.com/vpc/latest/userguide/configure-subnets.html#subnet-basics)或[安全组](https://docs.aws.amazon.com/vpc/latest/userguide/vpc-security-groups.html)）。

## 架构
<a name="automate-the-creation-of-appstream-2-0-resources-using-aws-cloudformation-architecture"></a>

下图向您展示了如何使用 CloudFormation 模板自动创建 WorkSpaces 应用程序资源。

![自动创建 WorkSpaces 应用程序资源的工作流程。](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/4f0205f5-5b91-4832-9f0f-2135ae866226/images/cb578939-d9af-4f60-93c9-286881df4c3a.png)


下图显示了如下工作流：

1. 您可以根据此模式的 “[其他信息](#automate-the-creation-of-appstream-2-0-resources-using-aws-cloudformation-additional)” 部分中的 YAML 代码创建 CloudFormation 模板。

1. 该 CloudFormation 模板创建了一个 CloudFormation 测试堆栈。

   1. （可选）您可以使用 WorkSpaces 应用程序创建映像生成器实例。

   1. （可选）您可以使用自定义软件创建 Windows 映像。

1.  CloudFormation 堆栈创建 WorkSpaces 应用程序队列实例和堆栈。

1. 您可以在 HTML5兼容的浏览器上将 WorkSpaces 应用程序资源部署给最终用户。

## 工具
<a name="automate-the-creation-of-appstream-2-0-resources-using-aws-cloudformation-tools"></a>
+ [Amazon A WorkSpaces ](https://docs.aws.amazon.com/appstream2/latest/developerguide/what-is-appstream.html) pplications 是一项完全托管的应用程序流服务，可让您随时随地即时访问您的桌面应用程序。 WorkSpaces 应用程序管理托管和运行应用程序所需的 AWS 资源，自动扩展，并按需向用户提供访问权限。
+ [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html)帮助您对资源进行建模和设置，快速一致地配置 AWS 资源，并在资源的整个生命周期中对其进行管理。您可以使用模板来描述资源及其依赖关系，然后将它们作为堆栈一起启动和配置，而不必单独管理资源。您可以跨多个 AWS 账户 和 AWS 区域管理和配置堆栈。

## 最佳实践
<a name="automate-the-creation-of-appstream-2-0-resources-using-aws-cloudformation-best-practices"></a>
+ **正确配置映像生成器的网络访问权限 — 使用 NAT 网关进行**仅限出站互联网访问，在具有适当互联网访问权限的虚拟私有云 (VPC) 子网中启动映像生成器。

  在创建映像之前，测试与所需资源（例如应用程序服务器、数据库和许可服务器）的网络连接。验证 VPC 路由表是否允许连接到所有必需的网络资源。有关更多信息，请参阅 WorkSpaces 应用程序文档中的[互联网接入](https://docs.aws.amazon.com/appstream2/latest/developerguide/internet-access.html)。
+ **根据服务配额主动监控队列容量** — WorkSpaces 应用程序实例类型和大小配额按每个 AWS 账户、每个 AWS 区域。如果您在同一区域有多个实例集使用相同的实例类型和大小，则该区域所有实例集中的实例总数必须小于或等于适用的配额。有关更多信息，请参阅 WorkSpaces 应用程序文档中的[队列故障排除](https://docs.aws.amazon.com/appstream2/latest/developerguide/troubleshooting-fleets.html)。
+ **部署队列之前，在 Image Builder 测试模式下测试应用程序 — 在创建映像并部署到队列**之前，请务必在 Image Builder 测试模式下验证应用程序。测试模式模拟最终用户对舰队实例的有限权限。有关更多信息，请参阅 WorkSpaces 应用程序文档中的[映像生成器故障排除](https://docs.aws.amazon.com/appstream2/latest/developerguide/troubleshooting-image-builder.html#troubleshooting-07)。

## 操作说明
<a name="automate-the-creation-of-appstream-2-0-resources-using-aws-cloudformation-epics"></a>

### （可选）创建 WorkSpaces 应用程序镜像
<a name="optional-create-a-aas2-image"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 安装自定义软件和创建映像。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automate-the-creation-of-appstream-2-0-resources-using-aws-cloudformation.html)考虑使用 Windows AppLocker 功能进一步锁定图像。 | AWS DevOps，云架构师 | 

### 部署 CloudFormation 模板
<a name="deploy-the-cfn-template"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 更新 CloudFormation 模板。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automate-the-creation-of-appstream-2-0-resources-using-aws-cloudformation.html) | AWS 系统管理员、云管理员、云架构师、常规 AWS、AWS 系统管理员 | 
| 使用模板创建 CloudFormation 堆栈。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automate-the-creation-of-appstream-2-0-resources-using-aws-cloudformation.html) | 应用程序所有者、AWS 系统管理员、Windows 工程师 | 

## 问题排查
<a name="automate-the-creation-of-appstream-2-0-resources-using-aws-cloudformation-troubleshooting"></a>


| 问题 | 解决方案 | 
| --- | --- | 
| 各种问题 | 有关更多信息，请参阅 WorkSpaces 应用程序文档中的[故障排除](https://docs.aws.amazon.com/appstream2/latest/developerguide/troubleshooting.html)。 | 

## 相关资源
<a name="automate-the-creation-of-appstream-2-0-resources-using-aws-cloudformation-resources"></a>

**参考**
+ [开始使用 Amazon WorkSpaces 应用程序：使用示例应用程序进行设置](https://docs.aws.amazon.com/appstream2/latest/developerguide/getting-started.html)
+ [创建 Amazon WorkSpaces 应用程序队列和堆栈](https://docs.aws.amazon.com/appstream2/latest/developerguide/set-up-stacks-fleets.html)

**教程和视频**
+ [Amazon WorkSpaces 应用程序用户工作流程](https://www.youtube.com/watch?v=hVGQ87-Uhrc)
+ [如何将旧版 Windows Forms 应用程序迁移到亚马逊 WorkSpaces 应用程序](https://www.youtube.com/watch?v=CIImtS2iVbg)
+ [AWS re: Invent 2018：使用亚马逊 WorkSpaces 应用程序安全交付桌面应用程序 (01) BAP2](https://www.youtube.com/watch?v=xNIyc_inOhM)

## 附加信息
<a name="automate-the-creation-of-appstream-2-0-resources-using-aws-cloudformation-additional"></a>

以下代码是一个模板示例，您可以使用该 CloudFormation 模板自动创建 WorkSpaces 应用程序资源。

```
AWSTemplateFormatVersion: 2010-09-09
Parameters:
  SubnetIds:
    Type: 'List<AWS::EC2::Subnet::Id>'
  testSecurityGroup:
    Type: 'AWS::EC2::SecurityGroup::Id'
  ImageName:
    Type: String
Resources:
  
  AppStreamFleet:
    Type: 'AWS::AppStream::Fleet'
    Properties:
      ComputeCapacity:
        DesiredInstances: 5
      InstanceType: stream.standard.medium
      Name: appstream-test-fleet
      DisconnectTimeoutInSeconds: 1200
      FleetType: ON_DEMAND
      IdleDisconnectTimeoutInSeconds: 1200
      ImageName: !Ref ImageName
      MaxUserDurationInSeconds: 345600
      VpcConfig:
        SecurityGroupIds:
          - !Ref testSecurityGroup
        SubnetIds: !Ref SubnetIds
  AppStreamStack:
    Type: 'AWS::AppStream::Stack'
    Properties:
      Description: AppStream stack for test
      DisplayName: AppStream test Stack
      Name: appstream-test-stack
      StorageConnectors:
        - ConnectorType: HOMEFOLDERS
      UserSettings:
        - Action: CLIPBOARD_COPY_FROM_LOCAL_DEVICE
          Permission: ENABLED
        - Action: CLIPBOARD_COPY_TO_LOCAL_DEVICE
          Permission: ENABLED
        - Action: FILE_DOWNLOAD
          Permission: ENABLED
        - Action: PRINTING_TO_LOCAL_DEVICE
          Permission: ENABLED
  AppStreamFleetAssociation:
    Type: 'AWS::AppStream::StackFleetAssociation'
    Properties:
      FleetName: appstream-test-fleet
      StackName: appstream-test-stack
    DependsOn:
      - AppStreamFleet
      - AppStreamStack
```