如何在 Windows 中使用 CloudFormation 将驱动器号映射到 Amazon EBS 卷?

4 分钟阅读
0

我想要使用 AWS CloudFormation 在 Windows 中将驱动器号映射到 Amazon Elastic Block Store (Amazon EBS) 卷。

简短描述

您可以使用在启动期间运行的自定义脚本,通过 CloudFormation 在 Windows 中将驱动器号映射到 Amazon EBS 卷。

解决方法

1.    使用用于 Windows Server 2016 Base 或更高版本的 Amazon Machine Image (AMI) 启动 Windows 实例

2.    (可选)手动创建 D 驱动器,然后将数据文件放在 D 驱动器中。要了解更多信息,请参阅使 Amazon EBS 卷可在 Windows 上使用。

3.    使用 ShutDown with the Sysprep (使用 Sysprep 关闭) 选项,从您的实例创建 AMI。要了解更多信息,请参阅使用 EC2Launch 配置 Windows 实例

4.    根据以下模板创建 CloudFormation 堆栈。

Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "AWS CloudFormation template AV Group, Launch Config for EC2",
  "Resources": {
    "DBServer": {
      "Type": "AWS::EC2::Instance",
      "Metadata" : {
        "AWS::CloudFormation::Init" : {
          "config" : {
          "files" : {
            "c:\\cfn\\cfn-hup.conf" : {
            "content" : { "Fn::Join" : ["", [
              "[main]\n",
              "stack=", { "Ref" : "AWS::StackId" }, "\n",
              "region=", { "Ref" : "AWS::Region" }, "\n"
              ]]}
            },
            "c:\\cfn\\hooks.d\\cfn-auto-reloader.conf" : {
            "content": { "Fn::Join" : ["", [
              "[cfn-auto-reloader-hook]\n",
              "triggers=post.update\n",
              "path=Resources.DBServer.Metadata.AWS::CloudFormation::Init\n",
              "action=cfn-init.exe -v -s ", { "Ref" : "AWS::StackId" },
                " -r DBServer",
                " --region ", { "Ref" : "AWS::Region" }, "\n"
            ]]}
            },
            "c:\\cfn\\hooks.d\\drives.diskpart.txt" : {
              "content": { "Fn::Join" : ["", [
                "select disk 2\n",
                "attributes disk clear readonly\n",
                "clean\n",
                "online disk\n",
                "convert gpt\n",
                "create partition primary\n",
                "format quick fs=ntfs label=","\"","ORATEMP","\"","\n",
                "assign letter=X\n",
                "select disk 3\n",
                "attributes disk clear readonly\n",
                "clean\n",
                "online disk\n",
                "convert gpt\n",
                "create partition primary\n",
                "format quick fs=ntfs label=","\"","ORADATA","\"","\n",
                "assign letter=S\n",
                "select disk 1\n",
                "online disk\n"
              ]]}},
              "c:\\cfn\\hooks.d\\renamedrives.ps1" : {
                "content": { "Fn::Join" : ["", [
                  "diskpart /s c:\\cfn\\hooks.d\\drives.diskpart.txt\n"
              ]]}}
          },
          "commands" : {
            "1-rename-drives" : {
              "command" : "powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -File c:\\cfn\\hooks.d\\renamedrives.ps1",
              "waitAfterCompletion" : "0"
            },
            "2-signal-success" : {
              "command" : { "Fn::Join" : [ "", [
                "cfn-signal.exe -e %ERRORLEVEL% \"",
                { "Fn::Base64" : { "Ref" : "WindowsServerWaitHandle" }},
                "\""]]
              }
            }
          },
          "services" : {
            "windows" : {
            "cfn-hup" : {
              "enabled" : "true",
              "ensureRunning" : "true",
              "files" : ["c:\\cfn\\cfn-hup.conf", "c:\\cfn\\hooks.d\\cfn-auto-reloader.conf"]
            }
            }
          }
          }
        }
      },
      "Properties": {
        "ImageId": "ami-01xxxxxxxxxx",
        "InstanceType": "m4.xlarge",
        "KeyName": "xxxxxxxx",
        "BlockDeviceMappings": [
          {"DeviceName": "xvdg","Ebs":{"VolumeSize":"20"}},
          {"DeviceName": "xvdm","Ebs":{"VolumeSize":"20"}}
        ],
        "Tags": [ {
          "Key": "Name",
          "Value": {"Fn::Join": ["-", ["DB",{"Ref": "AWS::StackName"}]]}
        }],
        "UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
          "<powershell>\n",
          "cfn-init.exe -v -s ", { "Ref" : "AWS::StackId" },
          " -r DBServer",
          " --region ", { "Ref" : "AWS::Region" }, "\n",
          "</powershell>"
        ]]}}
      }
    },
    "WindowsServerWaitHandle" : {
      "Type" : "AWS::CloudFormation::WaitConditionHandle"
    },
    "WindowsServerWaitCondition" : {
      "Type" : "AWS::CloudFormation::WaitCondition",
      "DependsOn" : "DBServer",
      "Properties" : {
        "Handle" : {"Ref" : "WindowsServerWaitHandle"},
        "Timeout" : "1800"
      }
    }
  }
}

**注意:**上述模板包含一个脚本,该脚本可使用驱动器号 X 创建磁盘 2,使用驱动器号 S 创建磁盘 3。

以下脚本是步骤 4 中所示模板的一部分,在此处标注以示强调。该脚本可使用主启动记录 (MBR) 分区结构配置卷,将卷格式化为 NTFS 卷,然后分配驱动器号。

"select disk 2\n",
"attributes disk clear readonly\n",
"clean\n",
"online disk\n",
"convert gpt\n",
"create partition primary\n",
"format quick fs=ntfs label=","\"","ORATEMP","\"","\n",
"assign letter=X\n",
"select disk 3\n",
"attributes disk clear readonly\n",
"clean\n",
"online disk\n",
"convert gpt\n",
"create partition primary\n",
"format quick fs=ntfs label=","\"","ORADATA","\"","\n",
"assign letter=S\n",
"select disk 1\n",
"online disk\n"

5.    将脚本移到 AWS::CloudFormation::Init 的文件部分,然后使用命令部分运行脚本。

**注意:**Disk0 始终为根卷。

**提示:**Windows 资源管理器按字母顺序显示驱动器序列。要从 CloudFormation 模板查找已分配给盘符的磁盘编号,请在磁盘管理实用工具中运行 diskmgmt 命令。

6.    部署您的实例,然后使用远程桌面协议 (RDP) 连接到您的实例

在本示例中,您的实例现在具有四个驱动器。您的 D 驱动器具有在步骤 1 中保存的数据文件。

Drive D          =====> Drive with the data file
Drive S          =====> This is the Disk 3 
Drive X          =====> This is the Disk 2

AWS 官方
AWS 官方已更新 3 年前
没有评论