How do I execute UserData content in a Windows EC2 instance

跟風遠走 提交于 2019-12-12 16:56:37

问题


I'm having a lot of trouble trying to get a Cloud Formation template running correctly with auto scaling. I have the following resource:

"LaunchConfig" : {
  "Type" : "AWS::AutoScaling::LaunchConfiguration",
  "Properties" : {
    "KeyName" : { "Fn::FindInMap" : [ "EnvironmentToKeyName", { "Ref" : "Environment" }, "KeyName" ] },
    "ImageId" : { "Fn::FindInMap" : [ "AWSRegionToAMI", { "Ref" : "AWS::Region" }, "AMI" ] },
    "SecurityGroups" : [ "neat_spi" ],
    "InstanceType" : { "Ref" : "InstanceType" },
    "UserData" : { "Fn::Base64" : {
        "Fn::Join" : ["", [ "<script>", "call c:\\chef\\boot.bat", { "Ref" : "Environment" }, "</script>"]]
      }
    }
  }
}

But it doesn't look like this script is running at all when the machine is started. How do I make sure it runs? Or how do I figure out why it isn't running?

I can't find any logging anywhere on how to see if this was run or not.


回答1:


After the VM boots, you can run remote commands on it with WinRM. Here's an example client:

https://github.com/WinRb/WinRM

Make sure WinRM is enabled in your Windows AMI and that the correct port is open in the security group.




回答2:


Couple of things to look for...

  1. Verify the cfn tools are installed
  2. Look in the c:\cfn subdirectory - there's a path called logs floating around there somewhere - review that.
  3. Look under program files\amazon\ec2tools - there's a log directory in there. Check to make sure that it's running the script
  4. In the directory above there's a folder called scripts - you should see something that looks like userdata.bat - try running that to confirm it works.

Troubleshooting CloudFormation and Userdata is time consuming - and can be costly - because you get billed for an hour every time you start and stop the instance. Something that works to great advantage for Amazon. (Bring on per minute pricing please)




回答3:


I had a heck of a time getting the scripts to run on my AMI as well. It worked when I used a standard Amazon AMI, but it didn't work with my custom build AMIs.

I then found this link Create a Standard Amazon Machine Image Using Sysprep.

To summarize, I

  1. I made sure termination protection was turned on (this caused me to lose all my work once)
  2. Launched into a new Windows instance
  3. I customized the instance by doing things like installing windows features I needed and installing an .MSI that I needed.
  4. I turned on Windows Updates and cleared the recycle bin
  5. I ran EC2ConfigService Settings from Start -> All Programs
  6. I checked "Enable UserData execution for next service start (automatically enabled at SysPrep) eg. <script></script> or <powershell></powershell>" on the General tab.
  7. Make sure you set a proper password policy on the Image tab. I chose Random.
  8. Click Apply and then click Shutdown with SysPrep
  9. When the EC2 instance is finally stopped, you can create an image from the AWS console.

This new AMI was able to run my UserData scripts properly.



来源:https://stackoverflow.com/questions/17663478/how-do-i-execute-userdata-content-in-a-windows-ec2-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!