Skip to content

Ec2Launchv2 on Windows: How to use the InitializeVolume during UserData version 1.1

0

I am trying to initialize a volume during UserData using ec2launchv2 and at the same time run a executeScript task with simple content.

---
version: 1.1
tasks:
  - task: initializeVolume
    inputs:
    devices:
    - device: "/dev/sdb"
      letters: ["D"]
      partition: true
      initialize: true
      filesystem: "NTFS"
      label: "DataVolume"
  - task: executeScript
    inputs:
    - frequency: once
      type: powershell
      runAs: localSystem
      content: |-
            try {
                Set-DefaultAWSRegion -Region ${AWS::Region}
                [string]$token = Invoke-RestMethod -Headers @{"X-aws-ec2-metadata-token-ttl-seconds" = "10" } -Method PUT -Uri http://169.254.169.254/latest/api/token
                Set-Variable -name instance_id -value (Invoke-RestMethod -Headers @{"X-aws-ec2-metadata-token" = $token } -Method GET -Uri http://169.254.169.254/latest/meta-data/instance-id)
                Set-ExecutionPolicy Unrestricted -Force
                Enable-WindowsOptionalFeature -Online -FeatureName IIS-URLAuthorization
                Write-Log "Set Time Zone to US Eastern Standard Time"
                Set-TimeZone -Id "Eastern Standard Time"
          }
          catch {
              Write-Log "Error: $_"
              throw $_
          }

It seems that the following is not working

asked a year ago369 views
3 Answers
1
Accepted Answer

The following turn out to be the right formatting

---
version: 1.1
tasks:
  - task: initializeVolume
     inputs:
       initialize: devices
       devices:
       - device: xvdb
          letter: D
          partition: gpt
          name: DataVolume
  - task: executeScript
     inputs:
     - frequency: once
        type: powershell
        runAs: localSystem
        content: |-
             Write-Output "This Workds!"
answered a year ago
EXPERT
reviewed a year ago
1

Hello.

Please try this solution

How you can initialize a volume and run a PowerShell script on a Windows EC2 instance using Ec2LaunchV2 in the UserData section.

Step 1 Simplified UserData Script

  • Use the following UserData script to initialize a volume and execute a PowerShell script. This script assumes that the volume you want to initialize is /dev/sdb and you want to assign it the D: drive letter.
---
version: 1.0
tasks:
  - initializeVolume:
      devices:
        - device: "/dev/sdb"
          letters: ["D"]
          filesystem: "NTFS"
          label: "DataVolume"

  - executeScript:
      powershell: |
        # Set Time Zone to Eastern Standard Time
        Set-TimeZone -Id "Eastern Standard Time"

        # Enable IIS Feature
        Enable-WindowsOptionalFeature -Online -FeatureName IIS-URLAuthorization

Step 2 Key Points

  • Initialize Volume The initializeVolume task will format the /dev/sdb device as NTFS and mount it as the D: drive.

  • Execute PowerShell Script The executeScript task runs a simple PowerShell script to set the time zone and enable the IIS feature.

Step 3 Launch the Instance

  • After setting this as your UserData, launch your Windows EC2 instance. This script will automatically initialize the volume and run the PowerShell commands when the instance starts.

To Verification

  • Check Disk After the instance is up, check Disk Management to verify the D: drive is initialized.

  • Check Features Ensure that IIS URL Authorization is enabled and the time zone is set to Eastern Standard Time.

If you need more information, please go through the Document Link.

https://repost.aws/knowledge-center/ec2-windows-user-data-script

EXPERT
answered a year ago
EXPERT
reviewed a year ago
0
  1. Verify Script Syntax:

Correctness: Ensure the script is syntactically correct, including correct indentation and punctuation. Task Order: Check if the initializeVolume task is executed before the executeScript task, as the script might rely on the initialized volume.

  1. Review Error Messages:

Check Logs: Look for any error messages in the EC2 instance's logs, especially in the cloud-init log. Identify Issues: Analyze the error messages to pinpoint the specific problem.

  1. Test Individually:

Isolate Tasks: Try running each task separately to identify if one of them is causing the issue. Debug Tasks: If a task fails, focus on debugging its specific logic.

  1. Check Volume Initialization:

Verify Device: Ensure the specified device path (/dev/sdb) is correct and the volume is attached to the instance. Partition and Format: Confirm that the volume is partitioned and formatted with the desired filesystem (NTFS in this case).

  1. Validate PowerShell Script:

Syntax: Verify that the PowerShell script is syntactically correct and doesn't contain any errors. Permissions: Check if the script has the necessary permissions to execute the specified commands. Dependencies: Ensure that any required modules or dependencies are installed.

  1. Test with a Simplified Script:

Basic Functionality: Create a simplified script that only performs basic actions (e.g., writing a log message) to isolate potential issues.

  1. Refer to Documentation:

ec2launchv2 User Guide: Consult the official documentation for ec2launchv2 to understand its features and usage: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2launch-v2.html

Cloud-Init Documentation: Refer to the Cloud-Init documentation for information on user data processing and execution: https://cloudinit.readthedocs.io/

PowerShell Documentation: Consult the PowerShell documentation for guidance on PowerShell syntax, commands, and error handling: https://learn.microsoft.com/en-us/powershell/

EXPERT
answered a year ago
EXPERT
reviewed a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.