Getty Images/iStockphoto

How to create an EC2 instance using PowerShell

PowerShell has practical integrations that provide users with cross-platform capabilities. Get to know prerequisites and resources to create an EC2 instance using PowerShell.

While many IT teams use AWS Management Console or AWS Command Line Interface, or CLI, to manage AWS operations, others turn to PowerShell.

With AWS Management Console, admins use a GUI to control AWS resources. While a GUI is a good way to learn AWS, it requires a lot of manual work to manage the AWS platform via a GUI. This can be a challenge when admins start to automate AWS deployments.

With CLI, admins can control AWS resources from the command line and write automation scripts. PowerShell is also a command-line tool, but when compared to AWS CLI, it can provide some useful integrations and cross-platform capabilities.

Follow this tutorial to learn how to launch an EC2 instance using PowerShell, as well as prerequisites and needed dependencies.

Local prerequisites

AWS has several PowerShell packages you can use to manage your account, such as the following:

  • AWS.Tools. A modularized version of AWS Tools for PowerShell. This can be installed on a Windows-based computer, as well as on a Linux or macOS computer.
  • AWSPowerShell.NetCore. The single, large-module version of AWS Tools for PowerShell. This can be installed on a Windows-based computer, as well as on a Linux or macOS computer.
  • AWSPowerShell. The legacy, single, large-module version of AWS Tools for PowerShell. This can only be installed on a Windows-based computer.

In the following examples, we use the AWS.Tools module on Windows. The main advantage of AWS.Tools is that you don't have to install all of the PowerShell modules to manage your AWS account. While this does add some complexity, as you need to plan ahead to have the modules you need, it saves you space and time. Be sure to also follow the prerequisite instructions to get your account, Identity and Access Management user and access keys configured.

Install the required modules with the following command:

Install-AWSToolsModule AWS.Tools.EC2,AWS.Tools.SimpleSystemsManagement

If you get an error stating the Install-AWSToolsModule command does not exist, make sure you've already installed the AWS installer module with the following:

Install-Module -Name AWS.Tools.Installer

Create the dependencies

Before you can create an EC2 instance, you need to have several resources already created. If you have them already, reference the Get- commands to retrieve the appropriate IDs.

To create the minimum setup to deploy and access an EC2 instance, you need a Virtual Private Cloud (VPC) with a subnet, some other networking components and the EC2 instance itself.

Launch a VPC

If you already have a VPC, look up the VPC ID with the following command:

Get-EC2Vpc -Region us-west-2

To create a new VPC, first, define a subnet in Classless Inter-Domain Routing (CIDR) notation:

$vpcCidr = ''

Then, create the VPC using the New-EC2Vpc command:

$vpc = New-EC2Vpc -CidrBlock $vpcCidr

Out of the box, an AWS VPC does not have any DNS configured, so you need to enable DNS on the VPC, which is passed on to any EC2 instances inside it:

Edit-EC2VpcAttribute -VpcId $vpc.VpcId -EnableDnsSupport $true

You might want to resolve your EC2 instance hostnames publicly. You can enable that with the following:

Edit-EC2VpcAttribute -VpcId $vpc.VpcId -EnableDnsHostnames $true

Deploy network resources

Next, you need to add a couple network resources to ensure the new EC2 instance has access to the internet and that you can connect to it. These resources include an internet gateway, a route to the route table and a subnet inside of the VPC subnet.

An internet gateway is what enables a VPC to communicate with the internet. First, create one:

$internetGateway = New-EC2InternetGateway

Then, associate it to the VPC:

Add-EC2InternetGateway -InternetGatewayId $internetGateway.InternetGatewayId –VpcId $vpc.VpcId

If you want the nodes on the network, including the EC2 instance, to know how to route to the internet, you need a route table and route. Create the route table, and associate it with the VPC:

$routeTable = New-EC2RouteTable -VpcId $vpc.VpcId

Then, add a default route to it:

New-EC2Route -GatewayId $internetGateway.InternetGatewayId -RouteTableId $routeTable.RouteTableId -DestinationCidrBlock ''

Since the default route is the only route you are adding to the route table, all traffic goes through the internet gateway. You can modify this rule or add further rules. Modifications are a better option if you want more control over what traffic is sent over the internet gateway or if you want to route other traffic elsewhere.

To create a subnet inside of the VPC's subnet, first, find an Availability Zone to create it inside of:

Get-EC2AvailabilityZone -Region us-west-2 | ft RegionName,State,ZoneName

This displays all the zones in a Region. In this case, it references the Region us-west-2 as seen in Figure 1:

Use a command to see availability zones
Figure 1

Create the subnet:

$subnet = New-EC2Subnet -VpcId $vpc.VpcId -CidrBlock ''  AvailabilityZone 'us-west-2a'

Then, register the subnet with the routing table:

Register-EC2RouteTable -RouteTableId $routeTable.RouteTableId -SubnetId $subnet.SubnetId

Create an EC2 instance

The easiest way to create an EC2 instance is with Amazon Machine Images (AMIs). These enable you to get a VM up and running quickly. To see the AMIs that the Region supports, use Get-SSMLatestEc2Image. If you want to find Windows images, you can do so with the following:

Get-SSMLatestEC2Image -Path ami-windows-latest -Region us-west-2

Or you can look at Linux images:

Get-SSMLatestEC2Image -Path ami-amazon-linux-latest -Region us-west-2

From the list, select the image that fits your needs. For this example, we select Amazon's Linux 2022 with the 5.15 kernel:

$ami = Get-SSMLatestEC2Image -Path ami-amazon-linux-latest -Region us-west-2 -ImageName 'al2022-ami-minimal-kernel-5.15-x86_64'

To select an EC2 instance type, use the Get-Ec2InstanceType command. You get a lot of output. For example, in Figure 2, the us-west-2 Region has 506 instance types:

Get-Ec2InstanceType command
Figure 2

You can filter by memory and CPUs. As an example, here's how you can find every instance type with at most 4 GB of memory and two CPUs:

Get-Ec2InstanceType -Region us-west-2 | `
    Select-Object InstanceType, @{Name = 'CPUs'; Expression = { $_.VCpuInfo.DefaultVCpus } } `
    @{Name = 'MemoryGB'; Expression = { $_.MemoryInfo.SizeInMiB / 1024 } } | `
        Where-Object { $_.CPUs -le 2 -and $_.MemoryGB -le 4 } | `
            Sort-Object InstanceType | `
                Format-Table InstanceType,CPUs,MemoryGB

To adjust that command to your specific resource needs, change the Where-Object expression.

The command has simplified output that looks similar to Figure 3:

Simplified output for EC2 instances
Figure 3

From the list, select the t2.micro, which is in the AWS free tier. With the selected EC2 instance type, create the EC2 instance:

$newEC2Splat = @{
    Region            = 'us-west-2'
    ImageId           = $ami
    AssociatePublicIp = $false
    InstanceType      = 't2.micro'
    SubnetId          = $subnet.SubnetId
New-Ec2Instance @newEC2Splat

The output looks similar to Figure 4:

Output in PowerShell after creating an EC2 instance
Figure 4

Now, you can find the instance with Get-Ec2InstanceStatus, pictured in Figure 5:

Find the instance with Get-Ec2InstanceStatus
Figure 5

To delete that instance, retrieve the instance ID with the previous command, and then use Remove-EC2Instance:

Remove-EC2Instance -InstanceId i-0b684c72317a9e9d2 -Region us-west-2

You are asked for confirmation and see an object summarizing the changed states, as in Figure 6:

PowerShell summarized status
Figure 6

Dig Deeper on Cloud provider platforms and tools