{"id":1127,"date":"2012-02-21T15:33:26","date_gmt":"2012-02-21T23:33:26","guid":{"rendered":"http:\/\/www.redwireservices.com\/?p=1127"},"modified":"2014-05-02T10:36:47","modified_gmt":"2014-05-02T17:36:47","slug":"assign-aws-elastic-ip-eip-with-windows-powershell-seattle-wa","status":"publish","type":"post","link":"https:\/\/www.redwireservices.com\/assign-aws-elastic-ip-eip-with-windows-powershell-seattle-wa","title":{"rendered":"Assign AWS Elastic IP (EIP) With Windows Powershell"},"content":{"rendered":"

\"\"<\/a><\/p>\n

After many Amazon Web Services Elastic Compute Cloud<\/a> (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<\/code>, which is very limiting).<\/p>\n

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<\/a> installed before running this script.<\/p>\n

Without further ado, here is the script (you may download it as well: AWS-EIP.ps1<\/a>).<\/p>\n

\r\n<#\r\n    .SYNOPSIS\r\n    Simple script to safely assign AWS Elastic IPs (EIPs) at Windows Boot.\r\n    \r\n    .DESCRIPTION\r\n    Script first checks to see if the IP is already assigned, and if so aborts\r\n    returning a non-zero code.  If assginement is successful, zero (0) is returned.\r\n    \r\n    NOTE:  Script must be updated to include instanceID, EIP, security credentials,\r\n        and ServiceURL for your particular Region.\r\n    \r\n    .NOTES\r\n        File Name :  AWS-EIP.ps1\r\n        Author    :  Nick Webb - nickw at redwireservices dot com\r\n        Version   :  0.2 - 02\/21\/2012\r\n        \r\n    .LINK\r\n        http:\/\/www.redwireservices.com\r\n    \r\n    .EXAMPLE\r\n    \r\n    AWS-EIP.ps1\r\n#>\r\n\r\n\r\n# User modifyable items: #################################################\r\n\r\n# Update to the AWS SDK Path on your system, if not default\r\nAdd-Type -Path "C:Program Files (x86)AWS SDK for .NETbinAWSSDK.dll"\r\n\r\n# Update the following lines, as needed:\r\n$accessKeyID="your access key ID"\r\n$secretAccessKey="your secret access key"\r\n$instanceID="i-your instance id"\r\n$instanceEIP="x.x.x.x"\r\n\r\n# Uncomment ONE of the following, which applies to your region\r\n# US-West N. California:\r\n$ServiceURL="https:\/\/ec2.us-west-1.amazonaws.com"\r\n\r\n# US-West Oregon\r\n#$ServiceURL="https:\/\/ec2.us-west-2.amazonaws.com"\r\n\r\n# US-East (Standard)\r\n#$ServiceURL="https:\/\/ec2.us-east-1.amazonaws.com"\r\n# END User configurable options ##########################################\r\n\r\n$config=New-Object Amazon.EC2.AmazonEC2Config\r\n$config.ServiceURL = $ServiceURL\r\n$client=[Amazon.AWSClientFactory]::CreateAmazonEC2Client($accessKeyID,$secretAccessKey,$config)\r\n\r\n# See if the IP is already assigned somewhere\r\n$request = New-Object -TypeName Amazon.EC2.Model.DescribeAddressesRequest\r\n[void]$request.WithPublicIp($instanceEIP)\r\n\r\n$result = New-Object -TypeName Amazon.EC2.Model.DescribeAddressesResponse\r\ntry {\r\n    $result = $client.DescribeAddresses($request)\r\n} \r\ncatch {\r\n    echo "Failed to validate EIP $instanceEIP, ensure that it is allocated and associated with your account.  Aborting."\r\n    exit 2\r\n}\r\n\r\n# See if an instanceID is already assigned to this EIP\r\n$xml=$result.ToXML()\r\n$assignedInstanceID = [string]($xml.DescribeAddressesResponse.DescribeAddressesResult.Address.InstanceId)\r\n\r\n# Run this block if an Instance already has this EIP associated to it... just in case changes would \r\n# result in downtime (i.e. if we are launching a test system from a cloned production one).\r\nif ($assignedInstanceID) {\r\n  echo "Address $instanceEIP already assigned to: $assignedInstanceID, aborting."\r\n  exit 1\r\n}\r\n\r\n# If we get here, the IP is free and clear, go ahead and associate it.\r\n$request = New-Object -TypeName Amazon.EC2.Model.AssociateAddressRequest\r\n[void]$request.WithInstanceId($instanceID)\r\n[void]$request.WithPublicIp($instanceEIP)\r\n\r\n$result = $client.AssociateAddress($request)\r\nif ($result) {\r\n  echo "Address $instanceEIP assigned to $instanceID successfully."\r\n  exit 0\r\n}\r\nelse {\r\n  echo "Failed to assign $instanceEIP to $instanceID."\r\n  exit 3\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

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 … Continue reading →<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[31,3,11],"tags":[],"_links":{"self":[{"href":"https:\/\/www.redwireservices.com\/wp-json\/wp\/v2\/posts\/1127"}],"collection":[{"href":"https:\/\/www.redwireservices.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.redwireservices.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.redwireservices.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.redwireservices.com\/wp-json\/wp\/v2\/comments?post=1127"}],"version-history":[{"count":12,"href":"https:\/\/www.redwireservices.com\/wp-json\/wp\/v2\/posts\/1127\/revisions"}],"predecessor-version":[{"id":1404,"href":"https:\/\/www.redwireservices.com\/wp-json\/wp\/v2\/posts\/1127\/revisions\/1404"}],"wp:attachment":[{"href":"https:\/\/www.redwireservices.com\/wp-json\/wp\/v2\/media?parent=1127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.redwireservices.com\/wp-json\/wp\/v2\/categories?post=1127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.redwireservices.com\/wp-json\/wp\/v2\/tags?post=1127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}