I often have to turn on instances or register them with load balancers. I had some trouble finding documentation on the AWS PowerShell modules but in the end I was able to get everything done via scripts.
Here are some examples that might help you out.
If you are not using IAM roles then you will need to pass credentials to the AWS powershell module to use. This is how you accomplish that (using least privileged IAM credentials I hope).
1 2 3 4 |
Import-Module AWSPowerShell Set-AWSCredentials -AccessKey AccessKeyGoesHere -SecretKey PasswordSecretKeyGoesHere Set-DefaultAWSRegion us-east-1 |
Query for EC2 instances by name
1 2 |
$EC2Instance1 = Get-EC2Instance -Filter @{name='tag:Name'; values= "ServerName1"} $EC2Instance2 = Get-EC2Instance -Filter @{name='tag:Name'; values= "ServerName2"} |
Power on an instance
1 |
$EC2Instance1.Instances | Start-EC2Instance |
Convert your already queried instance objects into objects that can be used with Elastic Load Balancers…
1 2 3 4 |
$ELBInstance1 = New-Object Amazon.ElasticLoadBalancing.Model.Instance $ELBInstance1.InstanceId = $EC2Instance1 .Instances.InstanceID $ELBInstance2 = New-Object Amazon.ElasticLoadBalancing.Model.Instance $ELBInstance2.InstanceId = $EC2Instance1 .Instances.InstanceID |
Use your ELB Instance objects to add or remove them from ELBs
1 2 3 |
Remove-ELBInstanceFromLoadBalancer -LoadBalancerName "PutYourLoadBalancerNameHere" -Instances @($ELBInstance1) -Force Register-ELBInstanceWithLoadBalancer -LoadBalancerName "PutYourLoadBalancerNameHere" -Instances @($ELBInstance1, $EC2Instance2) |