Search code examples
amazon-web-servicespowershellamazon-ec2powershell-3.0powershell-4.0

Get UserData and save it in text file


I am working on script where I have to save ec2 instance 'UserData' and save it to text file.

Condition 1. I don't want to login to each instance and pull from there as instance may not in 'running' state. So i need to do it without login to the ec2 instance

Condition 2. I need to do in from powershell script.

I found the powershell cmdlets "(Get-EC2InstanceAttribute -InstanceId $tid -Attribute userData).UserData" where $tid is instance id. but I don't know the format of output this commands give. AWS CLI also gives this kind of format.


Solution

  • The "weird" format you are getting is base64 encoding of the user data.

    Can't help with powershell, but if this is any help, in linux you could get the human readable user data as follows:

    user_data=$(aws ec2 describe-instance-attribute \
                --instance-id i-xxxxxxxxxxxxxx \
                --attribute userData \
                --query 'UserData' \
                --output text)
    
    base64 -d <<< ${user_data}
    

    Something similar should be doable in Windows.