How To Set Up An Amazon Web Services (AWS) EC2 Instance Using PowerShell
Think you can do better? Show us!
Creating an AWS EC2 instance with PowerShell only requires on command; New-EC2Instance. Novices may think that’s all they need but they’d be dead wrong! An AWS EC2 instance requires lots of other resources to run like a network stage, AMI image and so on. The New-EC2Instance command will need to know this information but what if you don’t even have these dependencies created? This video will go through how to not only create an EC2 instance but to better understand the requirements that an EC2 instance needs to be created and how to build out each of those resources with PowerShell as well.
Alright so now let's get started and create an EC 2 instance with powershell well. We're going to create nearly all of the dependencies needed for an EC 2 instance, so we don't even need any kind of dependencies like VPC Internet gateway route or anything like that will go ahead and create all that for you alright so let's get started. Alright so the first thing we have to do is create the network stack and to do that. We first need to create a VPC for this demonstration. I'll be using a network of 10.0.0.0, slash 16, so it pretty big. Network and then to create the VPC we use the new EC 2. VPC command using the Cidr block parameter and we can pass that network to the new EC 2. VPC commands alright so I will go ahead and just do that. And that just creates a basic VPC nothing too fancy there so on 8 and 9, there notice that I'm using the edit EC 2. VPC attribute command. This isn't necessary, but what I'm doing here is I am enabling DNS support and DNS. Hostnames, which allows a WS to assign DNS names and also allow my EC 2 instance on my VPC here to resolve names. So I can go ahead and his run both of those alright so now that I've gotten those. Go and collapse this an next. I want to do the Internet gateway again. This isn't necessary, but if you need to access the EC 2 instance from the Internet. You have to have Internet gateway or in windows terms that default gateway. You need to have some kind of resource out there for your EC 2 instance to hit your VPC and then to go out to the Internet and to do that, we use the new EC 2 Internet Gateway Command. So this is a very simple. There's no parameters at all. We just kind of create one. But to assign that Internet gateway to the VPC. We just create us or EC 2. Instances can get to the Internet. We have to use add EC 2 Internet gateway. No output there. So pretty simple. There Alright, so Next up? We have to create a routing table. For instance, we're going to create a default route so even if you assign the Internet gateway to your VPC. There's no way for your EC 2 instances. To actually get out there needs to be a specific route that we have to create a route table and a route. So we can use the new EC2 route table command here and we will assign it to the RT variable and that's going to create the route table and associate with our VPC next step. We have to create a route inside of that route table and you can see that I'm providing the route table ID that gateway ID and then the destination Cidr block, which is all zeros, which is the default route, which has all traffic. So we're going to route. All traffic from the EC 2 instances connected to this VPC to our Internet gateway. That we just created alright so I'll go ahead and run. These 2 commands to create the route table and the route OK, continuing on Next up? We have to create the subnet so we created the VPC, which is a big chunk of IP addresses. Now we need to assign subnets inside of those VPCs to do that. We first need to know the availability zone because the new EC2 subnet command has to have the availability zone. We can find the availability zones by running get EC 2 availability zones. You can see, there that I've got. A few different available pick, whichever one you like it doesn't really matter. In this thing in this case, and then on line 34 I'm running new EC 2 subnet providing the VPC ID that I created earlier. The Cidr block, which is a smaller network inside of that big VPC, which is 10.0.1.0 slash 24. So it's going to be 255.255.255. Zero and then I am specifying the availability zone of the one that I found there so run this and I'll sign that output to SN. That gets me a subnet object that I can play with and then even if I created the subnet now. I have to associate or register that subnet with the specific route table that I created earlier to do that. I use register. EC2 route table specify they're out table ID. I'm using it from the output of the route table that would create earlier and then I'm using these subnet ID of the subnet that I just created so when run that and return to string nothing. Big there. Alright continuing on we've now created the base network stack. There's a few other things that you can do. But that's pretty much the basics next part is we need to find the AMI to use or the OS image. So I want to create a Windows Server 2016 box. You can create anything like this. But in this case. I'm just going to pick one so I chose Windows Server 2016. You going to need an AMI object and to get that one way to do that is to use the get EC 2 image by name. Command and when you run that you'll have a lot of stuff in here that will have all windows images that you can use so this is a good name to use to get all of your various windows images so because I'm going to choose windows and I want to choose Windows Server 2016 base that is the base one. Obviously we have nano core container. We have a lot of different options, there, but I'm just going to choose the base one so get that. Anna sign that to my AMI variable alright. Once I do that now. I have the whole network stack and I have the AMI available now, I can create. My instance using new EC 2 instance, I could do that by providing the image idea. The AMI that I just got associate public IP. This is not going to work as is. I have to actually set this to false because of the way that I'm creating it and then the instance type T 2. Micro Unfortunately you can't find the instance type via powershell, but you can go on a device documentation an easily. Find the instance type just type in EC2. Instance types and it will give you a big list and then I have to provide the subnet ID. Let's see if this will work, so it did work because it didn't. Give me an error message alright. So now who knows? How long it's going to take some time. This takes awhile specially for the windows images. So now we need to monitor this, if this code were in an automation script. You wouldn't want to do this constantly check and see if this is available over and over and over again. So it's always a good idea to create a function to do this so I've created a function here called wait. EC2 state and basically it just runs get EC2 instant status. Their on line 81 over and over and over again and just make sure that it checks a specific state so for now. I'm just going to add this to my session and then now I can call get EC2 instance. So when I call just get EC2 instance here notice that it does return all of the instance ID's that I have available in my subscription. But to narrow that down. Let's see here so now we have one now I'm just going to pass. Output of that to wait E 2 instant state and you see that I have a desired state of running I can verify that by going over here to my instances, and let's just see fresh this. I want to see what this looks like in the console. OK so now you can see that the instance that we just created ending in 69. Make sure that's the right one. Yes, 769, so that is the right one. Let's just say that this was still stop, but let's just say that it's supposed to be stopped. So when I run this when it stopped notice that it's just going to sit here and hang for a while until it and there's the running state so that's the cool little wait. EC2 instance state that I have created for you, OK now we can actually bring all this together. Into a custom function. I'm not going to go over the function in detail by any means, but I'm just going to hit a few minor things here, so this is the function. This will be available in GitHub with the resources and you see here that I've embedded the way. TC 2 instant. State function that I have in the begin block. It's just exact same function this function becomes portable. I can copy and paste this and what I wrote and that function is still available and inside the process block is where the real magic happens and the difference between this and the way that I demonstrated in the code. As you can see, there that there's a lot of get Commands so line 65, you have get EC2 VPC. Then, on 85 you have get EC2, Internet gateway. It's going to first check and see if each of those objects exist. And if not, then it's going to do it, so there's a lot of different get options first going to get it. First then if it does it so that gives us the opportunity to then run this over and over again and make it item potent which essentially will account for different states that your AWS environment. Maybe in that just that function. Feel free to devour it all. You want later by just getting it downloaded and to show you an example of how this works. Let's hope this works here. Let's actually bring this in to my session this way. And then on me run. This there, you see that creating EC2 instance. There's an option there a subnet has already been created an registered with VPC so it gives you some verbose output of the objects that it needs has already been created so notice that it's already waiting for running state. That means that it went through all that code immediately. Already, an entered running state and it should have created a new VPC so let's just confirm to make sure that that's actually true alright see. Now we have 4 when he had 3 before so this is the VPC that it just created so that's a really cool way to bring all that together and have your own custom. EC2 instance if you need to add some default values to it just a nice and add new functionality to it, you know, and that sort of thing I hope you got a lot out of this video. That has been how to create an AWS EC2 instance with powershell thanks for watching.