amazon-web-servicespowershellipelastic-ipeip

PowerShell Script to add EIP to instance via User Data


I am fairly new to PowerShell!

What I am trying to do: We have an auto-scaling group connecting to a 3rd party and they are only capable of IP whitelisting (I've suggested domain whitelisting to them), as such this is problem to auto-scaling instances. I am trying to assign free (unallocated) EIPs to auto-scaled instances.

I found this one: https://gist.github.com/cleydson/ff70493ef37cff03669e21ed37d90a8b But I am not sure if there are any dependencies I need to install to the instance before I can start using it.

From what I've read so far, I need to:

  1. Assign an IAM Role to the EC2 instance that allows it to perform the necessary EIP tasks
  2. Put the powershell script inside an accessible directory in the instance
  3. User Data script to call the powershell script inside the instance

Once all is working, I can then bake it into an AMI for use in auto-scaling.

Anyone can lead me further into the right direction? Thanks in advance!

EDIT UPDATE: The script I found above was working properly when I ran inside the a PowerShell window inside the instance itself. So I made an AMI of the instance now, and have made a new Launch Config for the ASG.

My problem now is the user data doesn't seem to be called whenever a new instance is created. Below is my user data script:

<script>
PowerShell -ExecutionPolicy Bypass -Command c:\scripts\setEIP.ps1 "EIP1,EIP2,EIP3"
</script>
<persist>true</persist>

I put placers in for the EIPs. Any thoughts as to why the user data isn't called?


Solution

  • I finally solved it!

    The reason why it was not being called was:

    enter image description here

    Initially "Enable UserData execution for next service start" was unchecked. This is something to do with the EC2Config service installed inside the instance itself.

    To summarize the solution:

    1. Script above (https://gist.github.com/cleydson/ff70493ef37cff03669e21ed37d90a8b) was used and saved into the instance.
    2. UserData execution was enabled inside the instance (C:\Program Files\Amazon\Ec2ConfigService\Ec2ConfigServiceSettings.exe)
    3. UserData script to call the script inside, using -Command instead of the more popular -File

    <script>
    PowerShell -ExecutionPolicy Bypass -Command c:\scripts\setEIP.ps1 "EIP1,EIP2,EIP3"
    </script>
    <persist>true</persist>