

AWS Migration Hub 重构空间目前为预览版，可能会发生变化。

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

# 使用 CloudFormation 创建重构空间资源
<a name="creating-resources-with-cloudformation"></a>

AWS Migration Hub 重构空间与AWS CloudFormation，是一项服务，可帮助您对您进行建模和设置AWS这样您只需花较少的时间来创建和管理资源与基础设施。您创建一个模板来描述所有AWS所需的资源（例如环境、应用程序、服务和路由），以及CloudFormation为您预置和配置这些资源。

在您使用时CloudFormation，可重复使用您的模板来不断地重复设置您的 Rreation Space 资源。描述一次您的资源，然后在多个 AWS 账户和区域中反复预置相同的资源。

## 重构空间和 CloudFormation 模板
<a name="working-with-templates"></a>

要为 Rreation Space 和相关服务设置和配置资源，您必须了解。[CloudFormation模板](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-guide.html). 模板是 JSON 或 YAML 格式的文本文件。这些模板描述要在 CloudFormation 堆栈中调配的资源。如果您不熟悉 JSON 或 YAML，可以在 CloudFormation Designer 的帮助下开始使用 CloudFormation 模板。有关更多信息，请参阅 *AWS CloudFormation 用户指南*中的[什么是 CloudFormation Designer？](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/working-with-templates-cfn-designer.html)。

重构 Spaces 支持在中创建环境、应用程序、服务和路由CloudFormation. 有关更多信息（包括用于环境、应用程序、服务和路由的 JSON 和 YAML 模板示例），请参阅[AWS Migration Hub 重构空间](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html)中的*AWS CloudFormation用户指南*.

### 模板示例
<a name="working-with-templates-example"></a>

以下示例模板创建了一个 Virtual Private Cloud (VPC) 和重构 Spaces 资源。当你选择部署CloudFormation模板来创建演示重构环境入门对话框中，重构空间控制台将部署以下模板。

**Example YAML 重构空间模板**  

```
AWSTemplateFormatVersion: '2010-09-09'
Description: This creates resources in one account.
Resources:
  VPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.2.0.0/16
      Tags:
        - Key: Name
          Value: VpcForRefactorSpaces
  PrivateSubnet1:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone: !Select [ 0, !GetAZs  '' ]
      CidrBlock: 10.2.1.0/24
      MapPublicIpOnLaunch: false
      Tags:
        - Key: Name
          Value: RefactorSpaces Private Subnet (AZ1)
  PrivateSubnet2:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone: !Select [ 1, !GetAZs  '' ]
      CidrBlock: 10.2.2.0/24
      MapPublicIpOnLaunch: false
      Tags:
        - Key: Name
          Value: RefactorSpaces Private Subnet (AZ2)
  RefactorSpacesTestEnvironment:
    Type: AWS::RefactorSpaces::Environment
    DeletionPolicy: Delete
    Properties:
      Name: EnvWithMultiAccountServices
      NetworkFabricType: TRANSIT_GATEWAY
      Description: "This is a test environment"
  TestApplication:
    Type: AWS::RefactorSpaces::Application
    DeletionPolicy: Delete
    DependsOn:
      - PrivateSubnet1
      - PrivateSubnet2
    Properties:
      Name: proxytest
      EnvironmentIdentifier: !Ref RefactorSpacesTestEnvironment
      VpcId: !Ref VPC
      ProxyType: API_GATEWAY
      ApiGatewayProxy:
        EndpointType: "REGIONAL"
        StageName: "admintest"
  AdminAccountService:
    Type: AWS::RefactorSpaces::Service
    DeletionPolicy: Delete
    Properties:
      Name: AdminAccountService
      EnvironmentIdentifier: !Ref RefactorSpacesTestEnvironment
      ApplicationIdentifier: !GetAtt TestApplication.ApplicationIdentifier
      EndpointType: URL
      VpcId: !Ref VPC
      UrlEndpoint:
        Url: "http://aws.amazon.com"
  RefactorSpacesDefaultRoute:
    Type: AWS::RefactorSpaces::Route
    Properties:
      RouteType: "DEFAULT"
      EnvironmentIdentifier: !Ref RefactorSpacesTestEnvironment
      ApplicationIdentifier: !GetAtt TestApplication.ApplicationIdentifier
      ServiceIdentifier: !GetAtt AdminAccountService.ServiceIdentifier
  RefactorSpacesURIRoute:
    Type: AWS::RefactorSpaces::Route
    DependsOn: 'RefactorSpacesDefaultRoute'
    Properties:
      RouteType: "URI_PATH"
      EnvironmentIdentifier: !Ref RefactorSpacesTestEnvironment
      ApplicationIdentifier: !GetAtt TestApplication.ApplicationIdentifier
      ServiceIdentifier: !GetAtt AdminAccountService.ServiceIdentifier
      UriPathRoute:
        SourcePath: "/cfn-created-route"
        ActivationState: ACTIVE
        Methods: [ "GET" ]
```

## 了解有关的更 CloudFormation
<a name="learn-more-cloudformation"></a>

要了解有关 CloudFormation 的更多信息，请参阅以下资源：
+ [AWS CloudFormation](https://aws.amazon.com/cloudformation/)
+ [AWS CloudFormation 用户指南](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html)
+ [CloudFormation API 参考](https://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/Welcome.html)
+ [AWS CloudFormation 命令行界面用户指南](https://docs.aws.amazon.com/cloudformation-cli/latest/userguide/what-is-cloudformation-cli.html)