Reading User Data and Metadata on EC2

When creating a new Linux EC2 instance on AWS you’re able to pass extra data to the server to be used late in the boot sequence. This can be in the form of a simple bash script, or the URL(s) of a number of scripts. Or the data could simple by a list of parameters you need that instance to know. Full details on the sorts of things you can pass via the “User Data” parameter can be found on Ubuntu Website or details for the AWS build AMI can be found here. (Not all images can be passed User Data, but the standard ones by Amazon can be along with a range of community ones.)

Now if you pass a script or the URL to a script as User Data to your new instance then this will get run for you, and you don’t need to worry about it. However if you pass it a list of parameters (for example you may pass it the Endpoint URI for your RDS instance so the DB settings for your apps can be set up correctly) you need to know how to read the User Data. Along with reading the User Data you may also want to read in Metadata about your new instance, such as its AMI-ID or the public hostname of your instance. Reading the Metadata is done by querying a simple API:

The base URI of all the query’s is: http://169.254.169.254/

Continue reading

Connecting to an Amazon RDS via PHP

If your trying to connect from an Amazon EC2 instance to an RDS instance in PHP and finding that the connection keeps timing out (i.e. you get a mysql 2003 error). try using the IP address for the RDS instance instead of the hostname (or ‘Endpoint’ as Amazon calls it).

So instead of using:

$link = mysql_connect('mydbinstance.c7hszkfowzmc.us-east-1.rds.amazonaws.com:3306', 'mysql_user', 'mysql_password');

Use something like this (obviously replacing the IP address below with the IP of your RDS):

$link = mysql_connect('77.44.77.11:3306', 'mysql_user', 'mysql_password');