问题
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...
- Verify the cfn tools are installed
- Look in the c:\cfn subdirectory - there's a path called logs floating around there somewhere - review that.
- Look under program files\amazon\ec2tools - there's a log directory in there. Check to make sure that it's running the script
- 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
- I made sure termination protection was turned on (this caused me to lose all my work once)
- Launched into a new Windows instance
- I customized the instance by doing things like installing windows features I needed and installing an .MSI that I needed.
- I turned on Windows Updates and cleared the recycle bin
- I ran
EC2ConfigService Settings
from Start -> All Programs - I checked "Enable UserData execution for next service start (automatically enabled at SysPrep) eg.
<script></script> or <powershell></powershell>
" on the General tab. - Make sure you set a proper password policy on the Image tab. I chose Random.
- Click Apply and then click Shutdown with SysPrep
- 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