AWS - Deregister ELB Instance

Octopus.Script exported 2015-04-30 by DudeSolutions belongs to ‘AWS’ category.

Removes the current instance from the ELB

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

AWS EC2 Client Id

ec2ClientId

The client id used to authenticate with AWS

AWS EC2 Client Secret

ec2Credentials

The client secret used to authenticate with AWS

AWS ELB Name

elbName

The name of the AWS ELB to remove the instance from

AWS EC2 Region

ec2Region = us-east-1

The region in which the ELB lives

Script body

Steps based on this template will execute the following PowerShell script.

# Part 1 of 2
# Part 1 Deregisters an EC2 instance from an ELB
# Part 2 Registers an EC2 instance with an ELB and waits for it to be InService

$ec2Region = $OctopusParameters['ec2Region']
$ec2User = $OctopusParameters['ec2ClientId']
$ec2Credentials = $OctopusParameters['ec2Credentials']
$elbName = $OctopusParameters['elbName']
$instanceId = ""

# Load EC2 credentials (not sure if this is needed when executed from an EC2 box)
try
{
	Write-Host "Loading AWS Credentials..."
	Import-Module AWSPowerShell
	Set-AWSCredentials -AccessKey $ec2User -SecretKey $ec2Credentials
	Set-DefaultAWSRegion $ec2Region
	Write-Host "AWS Credentials Loaded."
}
catch
{
	Write-Error -Message "Failed to load AWS Credentials." -Exception $_.Exception
	Exit 1
}

# Get EC2 Instance
try
{
	$response = Invoke-RestMethod -Uri "http://169.254.169.254/latest/meta-data/instance-id" -Method Get
	if ($response)
	{
		$instanceId = $response
	}
	else
	{
		Write-Error -Message "Returned Instance ID does not appear to be valid"
		Exit 1
	}
}
catch
{
	Write-Error -Message "Failed to load instance ID from AWS." -Exception $_.Exception
	Exit 1
}

# Deregister the current EC2 instance
Write-Host "Deregistering instance $instanceId from $elbName"
try
{
	Remove-ELBInstanceFromLoadBalancer -LoadBalancerName $elbName -Instance $instanceId -Force
	Write-Host "Instance deregistered"
}
catch
{
	Write-Error -Message "Failed to deregister instance." -Exception $_.Exception
	Exit 1
}

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "bcb05502-ae9a-48a0-a153-1cb3057efb61",
  "Name": "AWS - Deregister ELB Instance",
  "Description": "Removes the current instance from the ELB",
  "Version": 2,
  "ExportedAt": "2015-04-30T13:24:16.921+00:00",
  "ActionType": "Octopus.Script",
  "Author": "DudeSolutions",
  "Parameters": [
    {
      "Name": "ec2ClientId",
      "Label": "AWS EC2 Client Id",
      "HelpText": "The client id used to authenticate with AWS",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "Sensitive"
      }
    },
    {
      "Name": "ec2Credentials",
      "Label": "AWS EC2 Client Secret",
      "HelpText": "The client secret used to authenticate with AWS",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "Sensitive"
      }
    },
    {
      "Name": "elbName",
      "Label": "AWS ELB Name",
      "HelpText": "The name of the AWS ELB to remove the instance from",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "ec2Region",
      "Label": "AWS EC2 Region",
      "HelpText": "The region in which the ELB lives",
      "DefaultValue": "us-east-1",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptBody": "# Part 1 of 2\n# Part 1 Deregisters an EC2 instance from an ELB\n# Part 2 Registers an EC2 instance with an ELB and waits for it to be InService\n\n$ec2Region = $OctopusParameters['ec2Region']\n$ec2User = $OctopusParameters['ec2ClientId']\n$ec2Credentials = $OctopusParameters['ec2Credentials']\n$elbName = $OctopusParameters['elbName']\n$instanceId = \"\"\n\n# Load EC2 credentials (not sure if this is needed when executed from an EC2 box)\ntry\n{\n\tWrite-Host \"Loading AWS Credentials...\"\n\tImport-Module AWSPowerShell\n\tSet-AWSCredentials -AccessKey $ec2User -SecretKey $ec2Credentials\n\tSet-DefaultAWSRegion $ec2Region\n\tWrite-Host \"AWS Credentials Loaded.\"\n}\ncatch\n{\n\tWrite-Error -Message \"Failed to load AWS Credentials.\" -Exception $_.Exception\n\tExit 1\n}\n\n# Get EC2 Instance\ntry\n{\n\t$response = Invoke-RestMethod -Uri \"http://169.254.169.254/latest/meta-data/instance-id\" -Method Get\n\tif ($response)\n\t{\n\t\t$instanceId = $response\n\t}\n\telse\n\t{\n\t\tWrite-Error -Message \"Returned Instance ID does not appear to be valid\"\n\t\tExit 1\n\t}\n}\ncatch\n{\n\tWrite-Error -Message \"Failed to load instance ID from AWS.\" -Exception $_.Exception\n\tExit 1\n}\n\n# Deregister the current EC2 instance\nWrite-Host \"Deregistering instance $instanceId from $elbName\"\ntry\n{\n\tRemove-ELBInstanceFromLoadBalancer -LoadBalancerName $elbName -Instance $instanceId -Force\n\tWrite-Host \"Instance deregistered\"\n}\ncatch\n{\n\tWrite-Error -Message \"Failed to deregister instance.\" -Exception $_.Exception\n\tExit 1\n}",
    "Octopus.Action.Script.Syntax": "PowerShell"
  },
  "Category": "AWS",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/aws-deregister-elb-instance.json",
  "Website": "/step-templates/bcb05502-ae9a-48a0-a153-1cb3057efb61",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADNQTFRF////9o0R/eLD/Nu0/erS95Qg+bhr95sv/vHh+r96/vjw+bFc/NSl+KI++82W+saI+KpNeDqM1wAAA41JREFUeNrsnG2XazAURiuo0Cr//9feliIvR3DvXJFZe3+a6XpW5+xWEpyY2w0AAAAAAAAAAAAAAAAAAADgf1J0bda/9N70q83a3enzUHWVjbR1sW0xp6sd6fPI72VmUt3zA+kymD6N5vnIBMrHsxHTjsUXOX0e+iVaTNU5Q0A/Q+k+4oAp+ixMbw6A4rGVVjGHR92ulNXWuTAlBNJN/FFyr5yy3qN9rawmF9IxR4hqX4U1WMplmGtruVBDuiuswbKkzaGhX+cfXsqbZlXXv0dsYR13nw9fLenGXD7f6U5Ony4yTpzyZLNMUcpMr0xNzfwdRRMR1/LP2cqMctNqKx1LZFydm2U022ueEtLL6HbHfmSRYRn4HDXaXyzU4XRkkZWK/+JlRBBBBBFEEEEEEUQQQQQRRBBB5B9uYJc7SyuLw+nI7R2ptKWJcywd18Utza0rnM4iN66M6qzS5E93Lf1zLaviUL/ISs/Nt6W00DEyuRgiP2Yxvrd15z/Y26ncG76jy1Ta5jEy/L0p/VMWy33woVm8UYN1Y9fqKrzfZ5iedtaV34+kNxHak2Wg2SSkY7djx/bQWkNP6nkE0lH3Lyx7D1aak1Z1erWJ+U130Vz0Sude7mZqv995nW7mZxJd27Sg5XQppuMdWY3xl1XXOge8MasWjZfund0KbvrkE9fK7OPNne+2U9YEWX3nemtSbvLv6LJ7gZ9X45yBl9ZxrZ9d3vjT8rz62tOsny7jXkpYPX9jQmvF8yF55TdaslGviZy1vAmfoTobsZztGNEv7qZZSr/6HRc/0yzlb3HiKhURRBBBBBFEEEEEEUQQQQQRRBD5XSLav38tllbVzeH02Ww/UWA+6XgsHdXFKc2vK5Quoz/duVRnlrb26crpizzXOVU3l2Zb5Pfe+d1OX8ViqW7qH9gt51K44bukr2XxrW54vMaoy7mxa/cgvPRVKcQG7uOCD58HLQLt3r17Iy6AqjYeDG7TUenWW+p9Ot/IOF/lwuHV1nk6o8M469PWXhtr+0BeX/x7Ue40W3xacfb2gXFxUZcX8TYB3Kyfp+GThsjKti2zgZuMiLshxW3gpiQyrn/DXhR/i1NqIte5pkUEEUQQQQQRRBBBBBFEEEEEEUR+g4jQUZBEqjqFO9mOiyeShoXvYoukZOG4GCLpWZgu83/vTNRidhlE0rYAAAAAAAAAAAAAAAAAAACAZPkjwAAMDi+bsnPP/wAAAABJRU5ErkJggg==",
  "$Meta": {
    "Type": "ActionTemplate"
  }
}

History

Page updated on Thursday, April 30, 2015