gary
gary

Reputation: 167

Attach available EBS volume when new instance gets launched for windows server

I am using below script to re-attach the "available" volumes when the instance starts but somehow it's not working.I have attached snippet of my cloud formation template. Tq

Under LaunchConfiguration I am creating the EBS volumes
      BlockDeviceMappings:
        - DeviceName: /dev/sda1
          Ebs:
            VolumeType: gp2            
            VolumeSize: '100'
        - DeviceName: /dev/sdb
          Ebs:
            DeleteOnTermination: "false"            
            VolumeSize: '50'
            VolumeType: gp2                 
        - DeviceName: /dev/sdc
          Ebs:
            DeleteOnTermination: "false"            
            VolumeSize: '50'
            VolumeType: gp2       


Here I am calling those volume to reattach
    UserData: !Base64 
        'Fn::Join':
          - ''
          - - |
              <script>
            - 'cfn-init.exe -v -c config -s '
            - !Ref 'AWS::StackId'
            - ' -r ServerLaunchConfig'
            - ' --region '
            - !Ref 'AWS::Region'
            - |+

            - |
              </script>
               |
              <powershell>
                    "$instanceId = Invoke-RestMethod -Uri http://169.254.169.254/latest/meta-data/instance-id \n";
        $available = Get-EC2Volume -Filter @{ Name="status"; Values="available" }
        Foreach ($instance in $available) {
	ec2-attach-volume --instance-id $instanceId /dev/sdb --device vol-VVVVVVVV
	ec2-attach-volume --instance-id $instanceId /dev/sdc --device vol-VVVVVVVV
               }
              </powershell>

Upvotes: 0

Views: 512

Answers (1)

Matt Houser
Matt Houser

Reputation: 36073

I see the following problems:

  1. You're not referencing the EBS volume IDs in your ec2-attach-volume commands.
  2. You should only attach one EBS volume per time through your loop (you're attaching twice)
  3. You should iterate through devices as well as volumes: first time through, use /dev/sdb, next time through /dev/sdc, etc.

Upvotes: 1

Related Questions