

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

# 步骤 2：将您的源内容配置为部署到 Windows Server Amazon EC2 实例
<a name="tutorials-windows-configure-content"></a>

现在可以配置您的应用程序的源内容了，这样您就有可以部署到 Amazon EC2 实例的内容了。在此教程中，您会将一个网页部署到运行 Windows Server 的 Amazon EC2 实例，后者将 Internet Information Services（IIS）作为其 Web 服务器运行。此网页将简单地显示“Hello，World\$1” 消息。

**Topics**
+ [创建网页](#tutorials-windows-configure-content-download-code)
+ [创建运行应用程序的脚本](#tutorials-windows-configure-content-create-scripts)
+ [添加应用程序规范文件](#tutorials-windows-configure-content-add-appspec-file)

## 创建网页
<a name="tutorials-windows-configure-content-download-code"></a>

1. 在您的 `HelloWorldApp` 文件夹中创建一个名为 `c:\temp` 的子目录（子文件夹），然后切换到该文件夹。

   ```
   mkdir c:\temp\HelloWorldApp
   cd c:\temp\HelloWorldApp
   ```
**注意**  
您不必使用 `c:\temp` 作为位置或 `HelloWorldApp` 作为子文件夹名称。如果您使用不同的位置或子文件夹名称，请确保在本教程中通篇使用它。

1. 使用文本编辑器在文件夹内创建一个文件。将文件命名为 `index.html`。

   ```
   notepad index.html
   ```

1. 将以下 HTML 代码添加到该文件中，然后保存文件。

   ```
   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
   <html>
   <head>
     <title>Hello, World!</title>
     <style>
       body {
         color: #ffffff;
         background-color: #0188cc;
         font-family: Arial, sans-serif;  
         font-size:14px;
       }
     </style>
   </head>
   <body>
     <div align="center"><h1>Hello, World!</h1></div>
     <div align="center"><h2>You have successfully deployed an application using CodeDeploy</h2></div>
     <div align="center">
       <p>What to do next? Take a look through the <a href="https://aws.amazon.com/codedeploy">CodeDeploy Documentation</a>.</p>
     </div>
   </body>
   </html>
   ```

## 创建运行应用程序的脚本
<a name="tutorials-windows-configure-content-create-scripts"></a>

接下来，您将创建一个用于在 CodeDeploy 目标 Amazon EC2 实例上设置 Web 服务器的脚本。

1. 在保存 `index.html` 文件的相同子文件夹中，使用文本编辑器创建另一个文件。将文件命名为 `before-install.bat`。

   ```
   notepad before-install.bat
   ```

1. 将以下批处理脚本代码添加到该文件中，然后保存文件。

   ```
   REM Install Internet Information Server (IIS).
   c:\Windows\Sysnative\WindowsPowerShell\v1.0\powershell.exe -Command Import-Module -Name ServerManager
   c:\Windows\Sysnative\WindowsPowerShell\v1.0\powershell.exe -Command Install-WindowsFeature Web-Server
   ```

## 添加应用程序规范文件
<a name="tutorials-windows-configure-content-add-appspec-file"></a>

接下来，除了网页和批处理脚本AppSpec 文件之外，您还将添加应用程序规范文件（文件）。该 AppSpec 文件是一个 [YAML](http://www.yaml.org) 格式的文件，用于以下用途： CodeDeploy 
+ 将应用程序修订中的源文件映射到其在实例上的目的地。
+ 指定在部署期间要在实例上运行的脚本。

 AppSpec 文件必须命名`appspec.yml`。它必须放置在应用程序源代码的根文件夹中。

1. 在保存 `index.html` 和 `before-install.bat` 文件的相同子文件夹中，使用文本编辑器创建另一个文件。将文件命名为 `appspec.yml`。

   ```
   notepad appspec.yml
   ```

1. 将以下 YAML 代码添加到该文件中，然后保存该文件。

   ```
   version: 0.0
   os: windows
   files:
     - source: \index.html
       destination: c:\inetpub\wwwroot
   hooks:
     BeforeInstall:
       - location: \before-install.bat
         timeout: 900
   ```

CodeDeploy 将使用此 AppSpec 文件将应用程序源代码根文件夹中的`index.html`文件复制到目标 Amazon EC2 实例上的`c:\inetpub\wwwroot`文件夹。在部署期间， CodeDeploy 将在**BeforeInstall**部署生命周期事件期间在目标 Amazon EC2 实例上运行`before-install.bat`批处理脚本。如果此脚本运行时间超过 900 秒（15 分钟），则 CodeDeploy 会停止部署并将对 Amazon EC2 实例的部署标记为失败。

有关这些设置的更多信息，请参阅 [CodeDeploy AppSpec 文件引用](reference-appspec-file.md)。

**重要**  
此文件中各项之间的空格的位置和数量很重要。如果间距不正确， CodeDeploy 将引发可能难以调试的错误。有关更多信息，请参阅 [AppSpec 文件间距](reference-appspec-file.md#reference-appspec-file-spacing)。