How To Start And Stop Amazon Web Services (AWS) EC2 Instances Using PowerShell

Starting and stopping AWS EC2 instances with PowerShell is a pretty simple task. Using the Start-EC2Instance and Stop-EC2Instance commands, it can easily be done. However, when part of a larger automation script, things get trickier. At that point, you must be cognizant of the state of the instances as it’s starting or stopping. To manage the state, we can use the Get-EC2InstanceStatus command. This video will show you how to not only start and stop EC2 instances with PowerShell but it’ll also show you how to create PowerShell code to manage EC2 instance state using a useful Wait-EC2Instance custom function! Prerequisites include: - An AWS account - Root user or IAM user created that can start/stop instances - Already authenticated to AWS in a PowerShell session
Let's look at how to stop and start an aws instance with powerShell Alright. So the first thing I'm going to do is I'm going to use the get. EC 2 instance command and you see here. I already have an instance. I would like to manipulate so first off. Let me go ahead and just run get EC 2 instance and this will put the instance object inside of that instance variable. OK, so once I have that, then I can dive down into it and see with some of the output is and then you can see, there that the instances property has the instance ID instance type and a lot of other information about that. Particular instance, one way to start an EC 2 instance. That's already stopped is to pass the instance ID to the instance ID parameter on the start. EC 2 instance command. So I need to get the instance ID by itself, so to do that I'll just go ahead and. Create an instance ID variable there and then now you can see that the instance ID is just simply. The instance ID that I've been playing with Alright. So now I will go ahead and run start. EC 2 instance and that I will pass it to the instance ID that I'm working with and then you see, there that it brings back some information. So now I'd like to check and see what the status is. But as you notice. It's not giving me anything back there is a get EC 2 instant statuses. Supposed to get the status of the current instance in supposed to be if it stopped if it started and that sort of thing. But if I just wait for a minute there. Now you notice that it did actually come back. So now we get some information about the instance status, however. Looking down through the output there, I can't really tell you what state it is. I see some information. But I don't see what state is talking about. However, now notice that we do see a instance state property, there, we can. Narrow that down by just looking at that instance state and now you can see that the name is running so that means my instance is running so great, we've got the instance started so now let's go ahead and stop it. One way we can stop in EC 2 instance by using these stop EC 2 instance command. We can do that by just passing the instance ID like we did up there. But we can also use the pipeline so notice that I have instance and I'm passing it to stop EC 2 instance and essentially does the same thing I'm just using the? Pipeline. Alright so now we check and see what it is. And then notice that it doesn't come up anymore because it's not in the running State Alright. So let's just assume it's going to be down right now Alright. So now now that we've started one, an stopped one. Let's do a bunch of them at once. There's a couple of ways to do this. The first off there is on line 44 where we can use the filter parameter. The filter parameter is a common parameter and a lot of different AWS powerShell commands and I've included a link here to give you some more. Information about using that filter that get EC 2 instance commandlet help actually has a really good breakdown of the filter so in this instance here on 44. I'm using the filter. I'm specifying the name and the value. It's a hashtable always has a name and a value key and then for the name I’m specifying I want to see the instance ID and for the value. I am specifying the value that I want this will match as many different EC 2 instances as I have matching that criteria but. For now notice that I just did that and then now I'm starting the EC 2 instance the same general thing instance here could have been one or 50 different machines. It just whatever matches that filter criteria. So now that you have seen how we can do multiple ones with the filter. I can also do multiples with the pipeline as well. So on 47 and 48. I can simply run get EC2 instance pipe everything to start EC 2 instance or everything to stop EC 2 instance and it will. Essentially start or stop all the EC 2 instances that I have it's another good way to which is the pipeline alright so next step is that waiting thing I put a section here to dive into how we can figure out the state better because previously we had to just keep running that get EC 2 instance status over and over again and automation scripts. This isn't going to be interactive. We need a way to do this automatically so to do that again, you're going to use the get. EC 2 instance status passes. The instance ID that I have and then I'm going to look at instance state so before like before we see that it is running. However, we need a flag just a simple string say up or down running or not so we can drill down into this and then now you can see that that name that code is removed, but now we have a value of running and fianlly. Get down farther now. We just have a single string of running so now we have a single label. To indicate if it's going to be stopped or running. OK, so now let me just make sure that all my instances are stopped so notice that I had to there. It's going to go through and stop all of the instances that I currently have in my AWS account. Our next step. Here online 68 is we kind of need to fix that issue that I call it where get EC 2. Instance status doesn't work for stopped instances. It really needs to work for instance, is in the stop state and in the running state or any state for that matter to force get EC 2 instance to include. All of the instances. We just use the include all instance parameter an pass at a value of a boolean true. So when I do this now, you can see it return. Both of my EC 2 instances that I have regardless of the state. They were both stop because I stopped him online 64. So now that that is returning all of'em so now that we've got a piece of code that can return. The status of all of our EC 2 instances. Let's now create some code 2. Automatically check for that, So what I've done here, starting on line 72. I have a desired state right now, the desire state is running so I want to know when a particular instance is running and then 73, retry interval, which is going to retry every 5 seconds. So whatever that value is for each write interval. It's going to retry keep retrying checking checking checking to see if it's in that desired state and then online 74 there. There's 1 were using that. Get EC 2 instance status were including all instances and then passing the instance ID to true to make sure we're getting everything drilling down end like it did before with dot instance. State dot named value that just gets us. The running or stopped and then I'm saying if it's not equal to the desire state then go down in the 75 to the right host and start sleep. This is going to keep running and running and running and running while it is not equal to my desired state and then line 76 that's important one. That start sleep because if you don't have a delay in here in this while loop. It will continue as fast as powerShell can possibly do it, which would not be a good idea for you, or for AWS you'll probably get a nice email saying? What do you do in hitting our API so much but anyway, so let's try that so now let's see what the status is so everything right now should be stopped so. Right now, I want the desire state is running so I will go ahead and run. This piece of code and now you can see that it's saying waiting for instance, to reach the state of running. It's going to sit here forever forever. We don't have a time out. Yet on this. But it's just going to see her forever. So now let me go ahead and start this with the management console, so I believe so I'll go over here and now you can see. I have my instance ID's here, so let's go ahead and just start this one. From the console. Alright so now it's in the state of pending it's going to eventually go into running. So now we just wait a minute here and I'm not touching anything I promise I promise. I'm not touching anything. I'm just going to wait here and hopefully if this works. It will stop saying waiting for our instance to reach the state of running and while law that was pretty quick, so notice that it stopped the state of the script until that state matched the desired state that we want so now you notice that it is in running. OK so now we have the code to properly do that. Let's now swap it up a little bit. So now let's say that I want to wait until it is stopped so I'll put the desired state to stop. And then I will create a function for this. I highly recommend you create a function for anything and these weight functions are really good to do, I mean? I've created white functions were just about anything? And here's a good one. You can use for EC 2 instances. This function is called wait. EC 2 instance state and then you can see here that I built it that it has 3 parameters instance desired state an retry interval desired state is obviously what we just worked and I'm using validate set parameter. Validation attribute here of running and stop so I can use tab completion. I can show you that in a minute and then I have a user configurable retry interval so if I want to check for 5 seconds that will do it by default. If I don't say anything. I could do 10 or 15 or whatever and then on line 96 there. The instance, I've chosen to create with pipeline support so that allows me to pass. The output of get EC 2 instance directly to this and it will know what it is and then the code inside from 10 wait to 112 is exactly the same as it was before. OK so now let's go ahead and bring this into my session, so I can use it and let's see here get EC 2 instance. Let's see here we're going to use this one, so right now. It is running so let's just go ahead and grab just this one for now, instance ID. Keep that instance ID I'm saying the desired state is running, however, since I have that validate set in there, I could say running. Let me go down here and show you I could say way East to instance desired state. Running or stop see I had the option here, which is really nice. I have that option. So now it is currently running so let's just see what happens when I say the desired state is running if everything is working out well. It should just automatically release accessed back to the console. And there, it does and we got some verbose output because it did run get EC 2. Instance status and I write back to the console alright so let's go ahead and stop this. Alright, it's in a state of stopping so now since it's not in that desired state of running it just going to. Try and try and try and try until it's over. You may want to have set a time out for this that would be a good powershell practice for you to set a time up for this to see. So this doesn't last forever, but I think this is a really good example of creating that function will just wait and wait and wait until it's actually started so that's a really good way to create that powershell function from it, so that has been how to start and stop EC 2 instances with powershell thanks.