When provisioning an AWS EC2 fleet either manually or via a launch configuration it is very useful to be able to determine if the instance(s) are both operational and the Elastic load balancer is correctly redirecting HTTP requests and spreading the load evenly amongst the active EC2 instances.
The best way to achieve this is to have the instance “self provision” the functionality via userdata or via a launch configuration that can be injected when the instance is created. This code can be stored in an S3 bucket and copied to the instance on start-up.
PowerShell handles the automation process for you via UserData or by a launch configuration and does the following:
1) Install-WindowsFeature etc… – Installs IIS and all subcomponents. This can take some time – approx. 5 minutes per instance.
2) wget https://<bucketname>.s3.amazonaws.com/aspxless.zip -outfile c:\inetpub\wwwroot\aspxless.zip – Gets the zipped code from the S3 bucket and copies it locally to the /inetpub folder within the EC2 instance.
[System.IO.Compression.ZipFile]::ExtractToDirectory(“c:\inetpub\wwwroot\aspxless.zip”, “c:\inetpub\wwwroot\aspxless”) – Unzips the code into a separate folder and makes it ready for use under the \wwwroot\aspxless folder.
Note: Make sure that the S3 bucket where the zip file is stored has the “Action s3:GetObject” set otherwise you will get a permission denied error. You can secure this via an IAM role (Highly recommended) for better security but for the purposes of the demo the “*” permission will suffice.
The code within the default.aspx page queries the instance metadata and returns the following:
- Public Host Name
- Public IP
- Instance Type
- Availability Zone
You would then query the instance data by the following url: http://<ELB_End_Point_Or_PublicIP>/aspxless/default.aspx
The complete code for the walkthrough above can be found from here: https://github.com/AaronSaikovski/AWS_Less