leave AWS EC2 instance properly - amazon-ec2

I am new to AWS EC2 instance. I use ssh to connect to the instance and just want to know how to leave the instance properly. Sometimes I just close the terminal but I couldn't connect to it next time. Then I use
shutdown -h now
It works, but this way I need to restart the instance next time. Is any proper way to leave the instance?

Type
exit
if you just want to leave it and keep it running

The EC2 instance runs independently of you connecting to it. Each time you SSH in to the instance you start a new interactive shell. exit or Control-D will close the shell that you have logged in to, and leave the instance running.
If your instance is on-demand, you can shut it down to save costs, but that is an entirely separate operation from logging in and out.

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.

Allow ec2:TerminateInstance only on self?

I have a worker machine running on a spot instance with a dedicated role. I'd like to grant it the permission to run ec2-terminate-instances when it finishes but want it to be able to terminate only itself.
Couldn't find any variable of instance-id or something similar. How do I define that kind of permission?
I've also tried using shutdown -h now but the behaviour with spot instances was a bit weird - it killed the machine (terminated) but kept the spot request as fulfilled (rather than terminated-by-user)
Thanks!

EC2 creates new instance when I terminate previous

I can't seem to find the configuration setting but when I terminate a free tier instance a new one is created a few minutes later. I want to terminate it permanently and not have it restart. I created the instance using the eclipse tools originally if this makes any difference. I have tried stop and terminate and both will create a new instance and leave my other instances in the "terminated" or "stopped" state. Is there a setting that I can configure to leave it turned off?
I figured it out it is the application is configured in elasticbeanstalk. I had to delete the application in ebs and it terminated the application.

AWS seems to start another EC2 instance with same AMI automatically after termination

I have 6 ec2 instances and I want to terminate about 3 of them. I've tried terminating them from the command line using ec2-terminate-instances and also using the AWS mananagement console ( Right click on instance and click on terminate).
The instance stops and goes into terminated state. However after some time another instance is automatically started with the same image of the terminated instance. I can't figure out why this is happening.
When I tried to stop instances and then terminate them separately it seemed to workw fine

How can I launch 10 instances, and tag them at once

I want a single script that can lauch, and tag my instances which I can then use chef to configure them accordingly.
Say my service requires 10 instances, I want to be able to run 10 instances, then tag them according to their role (web, db, app server).
Then once I do that, I can use chef to connect to each one and configure them how i want.
But I'm confused, I know I can launch instances, but how do you wait for them to come online? Do you have to continously loop in some sort of a timer? That seems like a very hacky way to do it!
If you're going to do everything from the outside you do just have to poll to wait for the instance to be ready (which doesn't necessarily mean its ready to use - actual startup completed a little later)
You can also pass user data when you start an instance. Most amis support cloud init, and will interpret the data passed as a shell script if in the right format. That shell script could run chef or do other configuration tasks.

Resources