Testimonials
  • Our project required a deep knowledge of hardware design, expertise in virtual server configurations, and the ability to quickly understand how to plug into our existing back-up/recovery processes. It was immediately evident that you had the experience and skills to pull it off.

Assign AWS Elastic IP (EIP) With Windows Powershell

After many Amazon Web Services Elastic Compute Cloud (AWS EC2) projects involving Linux, I’m finally working on a Windows EC2 project. With that we need a variety of maintenance scripts, such as assigning a static IP at boot (Elastic IP or EIP). Much to my surprise, there are very few examples for using native Windows scripting to perform this work (other than tired, old, cmd.exe, which is very limiting).

So, with a very basic understanding of PowerShell, I’ve setout to fill this gap… stay tuned for other AWS PS scripts soon, such as taking EBS Snapshots. Make sure you have the AWS .NET SDK installed before running this script.

Without further ado, here is the script (you may download it as well: AWS-EIP.ps1).

<#
    .SYNOPSIS
    Simple script to safely assign AWS Elastic IPs (EIPs) at Windows Boot.
    
    .DESCRIPTION
    Script first checks to see if the IP is already assigned, and if so aborts
    returning a non-zero code.  If assginement is successful, zero (0) is returned.
    
    NOTE:  Script must be updated to include instanceID, EIP, security credentials,
        and ServiceURL for your particular Region.
    
    .NOTES
        File Name :  AWS-EIP.ps1
        Author    :  Nick Webb - nickw at redwireservices dot com
        Version   :  0.2 - 02/21/2012
        
    .LINK
        https://www.redwireservices.com
    
    .EXAMPLE
    
    AWS-EIP.ps1
#>


# User modifyable items: #################################################

# Update to the AWS SDK Path on your system, if not default
Add-Type -Path "C:Program Files (x86)AWS SDK for .NETbinAWSSDK.dll"

# Update the following lines, as needed:
$accessKeyID="your access key ID"
$secretAccessKey="your secret access key"
$instanceID="i-your instance id"
$instanceEIP="x.x.x.x"

# Uncomment ONE of the following, which applies to your region
# US-West N. California:
$ServiceURL="https://ec2.us-west-1.amazonaws.com"

# US-West Oregon
#$ServiceURL="https://ec2.us-west-2.amazonaws.com"

# US-East (Standard)
#$ServiceURL="https://ec2.us-east-1.amazonaws.com"
# END User configurable options ##########################################

$config=New-Object Amazon.EC2.AmazonEC2Config
$config.ServiceURL = $ServiceURL
$client=[Amazon.AWSClientFactory]::CreateAmazonEC2Client($accessKeyID,$secretAccessKey,$config)

# See if the IP is already assigned somewhere
$request = New-Object -TypeName Amazon.EC2.Model.DescribeAddressesRequest
[void]$request.WithPublicIp($instanceEIP)

$result = New-Object -TypeName Amazon.EC2.Model.DescribeAddressesResponse
try {
    $result = $client.DescribeAddresses($request)
} 
catch {
    echo "Failed to validate EIP $instanceEIP, ensure that it is allocated and associated with your account.  Aborting."
    exit 2
}

# See if an instanceID is already assigned to this EIP
$xml=$result.ToXML()
$assignedInstanceID = [string]($xml.DescribeAddressesResponse.DescribeAddressesResult.Address.InstanceId)

# Run this block if an Instance already has this EIP associated to it... just in case changes would 
# result in downtime (i.e. if we are launching a test system from a cloned production one).
if ($assignedInstanceID) {
  echo "Address $instanceEIP already assigned to: $assignedInstanceID, aborting."
  exit 1
}

# If we get here, the IP is free and clear, go ahead and associate it.
$request = New-Object -TypeName Amazon.EC2.Model.AssociateAddressRequest
[void]$request.WithInstanceId($instanceID)
[void]$request.WithPublicIp($instanceEIP)

$result = $client.AssociateAddress($request)
if ($result) {
  echo "Address $instanceEIP assigned to $instanceID successfully."
  exit 0
}
else {
  echo "Failed to assign $instanceEIP to $instanceID."
  exit 3
}
This entry was posted in LinkedIn, Off The Wire, Tech Tips. Bookmark the permalink.

6 Responses to Assign AWS Elastic IP (EIP) With Windows Powershell

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.