AWS - Register ELB Instance

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

Registers an instance with an 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 to use when authenticating 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 add the instance to

Registration Check Interval

registrationCheckInterval = 10

The number of seconds to wait before checking to see if the Instance has properly registered with the ELB

Maximum Registration Checks

maxRegistrationCheckCount = 10

The maximum number of registration checks to perform before the step fails.

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 2 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 = ""
$registrationCheckInterval = $OctopusParameters['registrationCheckInterval']
$maxRegistrationCheckCount = $OctopusParameters['maxRegistrationCheckCount']

# 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
}

# Register the current EC2 instance
Write-Host "Registering instance $instanceId with $elbName."
try
{
	Register-ELBInstanceWithLoadBalancer -LoadBalancerName $elbName -Instance $instanceId
	Write-Host "Instance Registered, waiting for registration to complete."
	
	$instanceState = (Get-ELBInstanceHealth -LoadBalancerName $elbName -Instance $instanceId).State
	Write-Host "Current State: $instanceState"
	
	$checkCount = 0
	
	Write-Host "Retry Parameters:"
	Write-Host "Maximum Checks: $maxRegistrationCheckCount"
	Write-Host "Check Interval: $registrationCheckInterval"
	
	While ($instanceState -ne "InService" -and $checkCount -le $maxRegistrationCheckCount)
	{	
		$checkCount += 1
		
		# Wait a bit until we check the status
		Write-Host "Waiting for $registrationCheckInterval seconds for instance to register"
		Start-Sleep -Seconds $registrationCheckInterval
		
		if ($checkCount -le $maxRegistrationCheckCount)
		{
			Write-Host "$checkCount/$maxRegistrationCheckCount Attempts"
		}
		
		$instanceState = (Get-ELBInstanceHealth -LoadBalancerName $elbName -Instance $instanceId).State
		
		Write-Host "Current instance state: $instanceState"
	}
	
	if ($instanceState -eq "InService")
	{
		Write-Host "Registration complete."
	}
	else
	{
		Write-Error -Message "Failed to register instance: $instanceState"
		Exit 1
	}
}
catch
{
	Write-Error -Message "Failed to Register 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": "cc1ae822-3dcf-4041-a790-69a1267552c2",
  "Name": "AWS - Register ELB Instance",
  "Description": "Registers an instance with an ELB",
  "Version": 4,
  "ExportedAt": "2015-04-30T13:25:21.128+00:00",
  "ActionType": "Octopus.Script",
  "Author": "DudeSolutions",
  "Parameters": [
    {
      "Name": "ec2ClientId",
      "Label": "AWS EC2 Client Id",
      "HelpText": "The client id to use when authenticating 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 add the instance to",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "registrationCheckInterval",
      "Label": "Registration Check Interval",
      "HelpText": "The number of seconds to wait before checking to see if the Instance has properly registered with the ELB",
      "DefaultValue": "10",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "maxRegistrationCheckCount",
      "Label": "Maximum Registration Checks",
      "HelpText": "The maximum number of registration checks to perform before the step fails.",
      "DefaultValue": "10",
      "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 2 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$registrationCheckInterval = $OctopusParameters['registrationCheckInterval']\n$maxRegistrationCheckCount = $OctopusParameters['maxRegistrationCheckCount']\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# Register the current EC2 instance\nWrite-Host \"Registering instance $instanceId with $elbName.\"\ntry\n{\n\tRegister-ELBInstanceWithLoadBalancer -LoadBalancerName $elbName -Instance $instanceId\n\tWrite-Host \"Instance Registered, waiting for registration to complete.\"\n\t\n\t$instanceState = (Get-ELBInstanceHealth -LoadBalancerName $elbName -Instance $instanceId).State\n\tWrite-Host \"Current State: $instanceState\"\n\t\n\t$checkCount = 0\n\t\n\tWrite-Host \"Retry Parameters:\"\n\tWrite-Host \"Maximum Checks: $maxRegistrationCheckCount\"\n\tWrite-Host \"Check Interval: $registrationCheckInterval\"\n\t\n\tWhile ($instanceState -ne \"InService\" -and $checkCount -le $maxRegistrationCheckCount)\n\t{\t\n\t\t$checkCount += 1\n\t\t\n\t\t# Wait a bit until we check the status\n\t\tWrite-Host \"Waiting for $registrationCheckInterval seconds for instance to register\"\n\t\tStart-Sleep -Seconds $registrationCheckInterval\n\t\t\n\t\tif ($checkCount -le $maxRegistrationCheckCount)\n\t\t{\n\t\t\tWrite-Host \"$checkCount/$maxRegistrationCheckCount Attempts\"\n\t\t}\n\t\t\n\t\t$instanceState = (Get-ELBInstanceHealth -LoadBalancerName $elbName -Instance $instanceId).State\n\t\t\n\t\tWrite-Host \"Current instance state: $instanceState\"\n\t}\n\t\n\tif ($instanceState -eq \"InService\")\n\t{\n\t\tWrite-Host \"Registration complete.\"\n\t}\n\telse\n\t{\n\t\tWrite-Error -Message \"Failed to register instance: $instanceState\"\n\t\tExit 1\n\t}\n}\ncatch\n{\n\tWrite-Error -Message \"Failed to Register 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-register-elb-instance.json",
  "Website": "/step-templates/cc1ae822-3dcf-4041-a790-69a1267552c2",
  "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