With the AWS Tools for PowerShell, you can manage Amazon Elastic Compute Cloud (EC2), the service for virtual machines (EC2 instances) in Amazon's cloud. Using Amazon's PowerShell tools and the AWS API, we can create, manage, and deploy EC2 instances (VMs) from our console.

Please read my post about the AWS Tools for PowerShell where I talk about the minimum requirements to access AWS. To install the AWS Tools for PowerShell, open up a PowerShell console and run:

Install-Package -Name AWSPowerShell

Before we dive into the actual code, let's make sure you understand the important acronyms when dealing with VMs in AWS. Amazon has structured most of their services with a clever and simple naming convention. Typically, these services and semantics have three-letter acronyms. For example, EC2 stands for "Elastic Compute Cloud."

The next acronym you should be familiar with is AMI. AMI stands for "Amazon Machine Image." An AMI is a template, or if you are familiar with the Microsoft Deployment Toolkit (MDT) or the System Center Configuration Manager you may know the term as a "gold image." An AMI is essentially a standard OS image that either Amazon, a third party, or yourself provides.

Searching AMIs with Get-EC2ImageByName ^

With the help of Get-EC2ImageByName you can search the available AMIs. The cmdlet only has a few parameters, but if you are searching by a specific name, it's fast and extremely useful. You can use this command to view the available parameters:

Get-EC2ImageByName –Parameter *
Displaying the available parameters of the Get EC2ImageByName cmdlet

Displaying the available parameters of the Get EC2ImageByName cmdlet

To find an AMI, you can use IntelliSense:

AWS Tools for PowerShell also have IntelliSense to provide you a list of images you have access to

AWS Tools for PowerShell also have IntelliSense to provide you a list of images you have access to

Creating an EC2 instance with New-EC2Instance ^

Once you have identified an AMI, you can use the New-EC2Instance cmdlet to create the EC2 instance.

Let's say we want to use the WINDOWS_2016_BASE AMI. We could either save the output from Get-EC2ImageByName into a variable or pipe the selection directly to New-EC2Instance. Below I show you the commands for the first option:

$AMIProps = @{
    'Name'       = 'WINDOWS_2016_CORE' # Name of the EC2 Image Name
    'Region'     = 'us-east-1'
    'AccessKey'  = 'MY_AWS_ACCESS_KEY'
    'SecretKey'  = 'MY_AWS_SECRET_KEY'
}
$AMI = Get-EC2ImageByName @AMIProps
$EC2Props = @{
    'ImageId'      = $AMI.ImageId # The selected AMI ImageId
    'MinCount'     = 1            # Minimum AMIs we should create is 1
    'MaxCount'     = 1            # Maximum AMIs we should create is 1
    'InstanceType' = 't2.micro'   # EC2 Instance Size
    'Region'       = 'us-east-1'
    'AccessKey'    = 'MY_AWS_ACCESS_KEY'
    'SecretKey'    = 'MY_AWS_SECRET_KEY'
}
New-EC2Instance @EC2Props

If we wanted to go the other route and pipe our selection into New-EC2Instance, we could do so like this:

$AMIProps = @{
    'Name'       = 'WINDOWS_2016_CORE' # Name of the EC2 Image Name
    'Region'     = 'us-east-1'
    'AccessKey'  = 'MY_AWS_ACCESS_KEY'
    'SecretKey'  = 'MY_AWS_SECRET_KEY'
}
$EC2Props = @{
    'MinCount'     = 1            # Minimum AMIs we should create is 1
    'MaxCount'     = 1            # Maximum AMIs we should create is 1
    'InstanceType' = 't2.micro'   # EC2 Instance Size
    'Region'       = 'us-east-1'
    'AccessKey'    = 'MY_AWS_ACCESS_KEY'
    'SecretKey'    = 'MY_AWS_SECRET_KEY'
}
$NewEC2Instance = Get-EC2ImageByName @AMIProps | New-EC2Instance @EC2Props

Please note that you may not see your EC2 instance in a running state for a few minutes. If you wanted, you could write a quick little loop to determine when your EC2 instance is running by using the Get-EC2Instance cmdlet.

while((Get-EC2InstanceStatus -InstanceId $NewEC2Instance.InstanceId | Select-Object -Property instance-state-name) -ne 'running')
{
    $outputStr = "EC2 Instance with ID of {0} is currently {2}" -f `
                    $($NewEC2Instance.InstanceId), `
                    $(Get-EC2InstanceStatus -InstanceId $NewEC2Instance.InstanceId | Select-Object -Property instance-state-name)
    Write-Output -InputObject $outputStr
}

Creating a custom AMI with New-EC2Image ^

If you wanted to create your own custom AMI, you could do this with the New-EC2Image cmdlet. This cmdlet allows you to capture a new AMI from an EC2 instance. You would do this if you needed a customized image with your own applications. You first have to specify a few parameters in a hash table, and you can use the New-EC2Image cmdlet to create the AMI:

$NewAMIProps = @{
    'InstanceId'  = $NewEC2Instance.InstanceId                        # The Instance ID of the EC2 Instance we want to capture
    'Name'        = 'my_new_win_server_2016_ami_for_prod'             # The name of the new AMI you are creating
    'Description' = 'A custom Windows Server 2016 AMI for production' # The description of the new AMI you are creating
    'Region'      = 'us-east-1'
    'AccessKey'   = 'MY_AWS_ACCESS_KEY'
    'SecretKey'   = 'MY_AWS_SECRET_KEY'
}
New-EC2Image @NewAMIProps

As soon as you run this, AWS will shut down and reboot the instance before creating the AMI. If you do not want to reboot the EC2 instance, you have to add -NoReboot $True to your parameters. You now have a brand-new customized AMI you can deploy in the same way as an image that Amazon provided.

Subscribe to 4sysops newsletter!

I have only scratched the surface of what you can do with the AWS Tools for PowerShell and EC2 instances. As always, I recommend that you use the built-in PowerShell help system. Alternatively, you access the documentation of the EC2 cmdlets online.

0 Comments

Leave a reply

Please enclose code in pre tags

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2021

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account