Comment puis-je mapper des lettres de lecteur sur les volumes Amazon EBS sous Windows à l'aide de CloudFormation ?

Lecture de 5 minute(s)
0

Je veux mapper des lettres de lecteur vers des volumes Amazon Elastic Block Store (Amazon EBS) sous Windows à l'aide d'AWS CloudFormation.

Brève description

Vous pouvez utiliser un script personnalisé qui s'exécute pendant le démarrage pour mapper des lettres de lecteur vers des volumes Amazon EBS sous Windows à l'aide de CloudFormation.

Solution

1.    Lancez une instance Windows à l'aide de l'Amazon Machine Image (AMI) pour Windows Server 2016 Base ou version ultérieure.

2.    (Facultatif) Créez un lecteur D manuellement, puis placez un fichier de données dans le lecteur D. Pour plus d'informations, consultez la section Rendre un volume Amazon EBS disponible à l'utilisation sur Windows.

3.    Utilisez l'option ShutDown with the Sysprep pour créer une AMI à partir de votre instance. Pour plus d'informations, consultez la section Configurer une instance Windows à l'aide d'EC2Launch.

4.    Créez une pile CloudFormation en fonction du modèle suivant.

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

Remarque : le modèle précédent inclut un script qui crée le disque 2 avec la lettre de lecteur X et le disque 3 avec la lettre de lecteur S.

Le script suivant fait partie du modèle précédent de l'étape 4 et est appelé ici pour l'accent. Le script configure le volume pour utiliser la structure de partition MBR (enregistrement de démarrage principal), formate le volume en tant que volume NTFS, puis attribue une lettre de lecteur.

"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.    Déplacez le script vers la section « files » d'AWS::CloudFormation::Init, puis exécutez le script à l'aide de la section « command ».

Remarque : le disque 0 est toujours le volume racine.

Astuce : l'Explorateur Windows affiche la séquence de lecteur dans l'ordre alphabétique. Pour trouver le numéro de disque attribué à la lettre de disque à partir de votre modèle CloudFormation, exécutez la commande diskmgmt dans l'utilitaire Gestion des disques.

6.    Déployez votre instance, puis connectez-vous à votre instance à l'aide du protocole RDP (Remote Desktop Protocol).

Pour cet exemple, votre instance possède désormais quatre lecteurs. Votre lecteur D contient le fichier de données que vous avez enregistré à l'étape 1.

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

AWS OFFICIEL
AWS OFFICIELA mis à jour il y a 3 ans