¿Cómo puedo asignar letras de unidad a volúmenes de Amazon EBS en Windows mediante CloudFormation?

5 minutos de lectura
0

Quiero asignar las letras de la unidad a los volúmenes de Amazon Elastic Block Store (Amazon EBS) en Windows mediante AWS CloudFormation.

Breve descripción

Puede usar un script personalizado que se ejecute durante el arranque para asignar letras de unidad a los volúmenes de Amazon EBS en Windows con CloudFormation.

Resolución

1.    Lance una instancia de Windows con la imagen de máquina de Amazon (AMI) para Windows Server 2016 Base o posterior.

2.    (Opcional) Cree una unidad D manualmente y, a continuación, coloque un archivo de datos en la unidad D. Para obtener más información, consulte Hacer que un volumen de Amazon EBS esté disponible para su uso en Windows.

3.    Use la opción Apagar con Sysprep para crear una AMI a partir de la instancia. Para obtener más información, consulte Configurar una instancia de Windows utilizando EC2Launch.

4.    Cree una pila de CloudFormation basada en la siguiente plantilla.

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"
      }
    }
  }
}

Nota: La plantilla anterior incluye un script que crea el disco 2 con la letra de unidad X y el disco 3 con la letra de unidad S.

La siguiente secuencia de comandos forma parte de la plantilla que se muestra en el paso 4 y se muestra aquí para darle énfasis. El script configura el volumen con la estructura de particiones del registro de arranque maestro (MBR), formatea el volumen como volumen NTFS y, a continuación, asigna una letra de unidad.

"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.    Mueva el script a la sección de archivos de AWS::CloudFormation::Init y, a continuación, ejecútelo mediante la sección de comandos.

Nota: Disk0 siempre es el volumen raíz.

Consejo: El Explorador de Windows muestra la secuencia de unidades en orden alfabético. Para encontrar el número de disco asignado a la letra de disco de su plantilla de CloudFormation, ejecute el comando diskmgmt en la utilidad de administración de discos.

6.    Despliegue su instancia y, a continuación, conéctese a la instancia mediante el Protocolo de escritorio remoto (RDP).

Para este ejemplo, la instancia ahora tiene cuatro unidades. La unidad D contiene el archivo de datos que guardó en el paso 1.

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

OFICIAL DE AWS
OFICIAL DE AWSActualizada hace 3 años