UserData Script is not executing on Amazon Ec2 Instance - amazon-ec2

i am facing some problem with userdata execution on windows AMI, trying to execute the userdata like, opening notepad start notepad.exe i had tried to execute this its not working,this script is getting copied to instance but when i tried to connect to instance the notepad didn't open..Any one had faced the similar problem?

Userdata is only used when an instance is launched for the first time. It can be used to configure or run process on your instances when you first launch it. This is useful if you have a lot of instances you need to setup at once.
Userdata scripts do not run when you connect, but you could use it to add scripts to windows startup.

Related

Running bash script on GCP VM instance programmatically

I've read multiple posts on running scripts on GCP VMs but unfortunately could not find an answer that would satisfy my needs.
I have a Go application and I'm looking for a way to run a bash script on a VM instance programatically.
I'm using a Google Cloud Golang SDK which allows me to fetch VM instance info. Unfortunately SDK does not contain a functionality that allows running a bash script on a specific instance(unlike an Azure Cloud SDK for example).
Options I've found:
Google Cloud Compute SDK has an option to set a startup script, that
will run every time an instance is restarted.
Add instance-level public SSH key. Establish an SSH connection and
run a script using Go SSH client.
Problems:
Obviously startup script will require an instance reboot and this is not possible in my use case.
SSH might be also problematic, in case instance is not running SSH
daemon or SSH port is not open. Also, SSH daemon config does not
permit root login by default(PermitRootLogin might be false), thus
script might be running on a non privileged user, making this option not
suitable either.
I should probably note that I am not authorised to change configuration of those VMs (for example change ssh daemon conf to permit root login), I can just use a token based authentication to access them, preferably through SDK, though other options are also possible as long as I am not exposing the instance to additional risks.
What options do I have? Is this even doable? Am I missing something?
Thanks!
As said by Kolban, there is no such API to trigger from outside a bash inside the VM. The best solution is to deploy a webserver (a REST API) that call the bash and to expose it (externally or internally).
But you can also cheat. You can create a daemon on your VM that you run with a startup script and that listen a custom metadata; let's say check it every seconds.
When the metadata is updated, the daemon can perform actions. You can imagine that the metadata contain the script to run with the parameters. At the end of the run, the metadata is cleaned by the daemon.
So now, to run your bash, call the setMetadata Api. It's not out of the box, but you can have something similar of what you expected.
Think of GCP as providing the virtual machine infrastructure such as compute, memory, disk and networking. What runs when the machine boots is between you and the machine image. I am hearing you say that you want to run a bash script within the VM. That is outside of the governance of GCP. GCP will only affect the operation and existence of the environment. If what you want to happen is run a script within the VM programatically you will need to run some form of demon inside the VM that can be signaled to run such a script. This could be a web server such as flask or express, it could be your SSH server or it could be some other technology you choose.
The core thing I think you were looking for was some GCP API that, when called, would run a script within the Compute Engine. I'm going to say that there is no such API.

AWS - UserData is not executed for instance created from custom image

I tested with simple PowerShell script mkdir C:\tempdir
When I specify it as UserData for instance launched from Amazon image, everything works fine. If I launch instance with specified UserData and make image of that instance, UserData is not executed for instances created from my custom image.
What is interesting, "C:\Program Files\Amazon\Ec2ConfigService\Scripts\UserScript.ps1" contains UserData of initial launch (before image was created), although, it should contain data specified during last launch.
More interesting, even UserScript.ps1 is present, it is not executed (it should create text file but text file is not present).
Here is UserData from second launch
<powershell>
mkdir C:\tempdir
</powershell>
User data scripts only run once when the instance is launched (created not started up). They are then disabled on the instance. So when when you grab an AMI from that instance and launch a new one, user data has already been disabled.
Enable UserData
To allow it run again when you launch a new one from an AMI you created you must turn user data support back on at the EC2ConfigService Settings utility by checking the UserData box (before grabbing the AMI).
Persist Enabling UserData
Now after this if you reboot the instance again then the UserData will run and again disable itself. To disable this functionality too you will need to add <persist>true</persist> to the instance's UserData which will keep the checkbox in the same state (on if you turn it on).
More info:
http://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2-instance-metadata.html
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html
User-data scripts is not running on my custom AMI, but working in standard Amazon linux
Amazon EC2 custom AMI not running bootstrap (user-data)
I had the same problem. Here's what solved it
Launch an instance from your image if you haven't already
RDP into it
Open powershell, and run this C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1 –Schedule
If you had a custom password for the instance, it will have been reset in the previous step. If you want it back, open powershell and run the following (assuming your username is Administrator net user Administrator "your_new_password" (if you didn't have a custom password, and were just using one generated by a .pem file, then you can ignore this step)
Now go into the AWS console (i.e in the browser) and create an AMI from the instance
Ensure your user data is of following form (the persist/true part is essential).
<powershell>
# Some powershell code here
</powershell>
<persist>true</persist>
That's it. Your userdata powershell script will now run next time your instance is launched.

How to create a ec2 instance in cloudfomration from the AMI of the instance which is also created by cloudformation

I have created a EC2 Instance using the cloud formation script and in that process I have executed around 20 commands in "AWS::CloudFormation::Init". This is a windows instance.
After that, I created a image from this and tried to create another EC2 instance using this image with couple of commands I wanted to be executed in "AWS::CloudFormation::Init".
This is giving me problem. The instance after getting created is not running the new commands which I specified in the template. But trying to run the commands which I specified while creating the old EC2Instance from which image was taken. This is through sysprep process which was given in one of the docs.
Is there any way to execute only the new commands and leaving out the old commands when the new image is created. I tried many alternatives. It is either executing old commands or none at all.
Have you stopped your instance before creating your image ? (like advised here).

can not login to custom ami

I am trying to initiate an instance that is found here...
https://aws.amazon.com/amis/aws-tools
The instance is launched but when I try to login, I get the following message:
ssh -i oct9.pem root#ec2-50-16-125-42.compute-1.amazonaws.com
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
If I launch a new instance using the built-in wizard, It works as expected with the same .pem key.
This AMI was working as expected till recently. I have used it before for a few instances. I would like to use this because it has several utilities pre-installed.
When you produce a new image from a running instance, you end up getting locked out of the running instance. I'm not sure why, but you can then re-launch a new instance from the image you just created.
It's unclear whether or not this is the issue you're running into, though.

Amazon AMI Windows instance + "user-data"?

is it possible to send a "user-data" to the Windows instance at the loading? I know that amazon allow to send it to *nix-based instance, but I can't find any information for Windows.
Thanks for the help,
Cyril
Amazon updated EC2Config on Windows AMIs on April 11, 2012 to support scripting through user-data for batch scripts and in May 2012 to support powershell scripts.
<script></script> tags will create and execute a batch file.
<powershell></powershell> tags will create and execute a powershell script.
Note that by default it only runs at instance initialization, so if you want it to execute each time you boot, you have to run the EC2ConfigServiceSettings and tell it to allow this always.
I am not aware of a direct way about it. But, you can create a start-up script inside your instance, that will allow you reading user-data each time you reboot your system. Inside your user-data, you can configure what's going to run only once or every single time your instance loads.

Resources