0

I am launching a ubuntu EC2 instance with my PHP code and passing user data to it. I don't own that instance.. but they provide user level access to it.

Firstly i tried to run a particular job by passing user data, but it did not seem to work.

I made it very simple and send "touch test.txt" as user data. The instance was launched properly but the file was not created as should be i guess.

Am I missing any step? I checked that file was not created using ssh 'ing to the instance. One thing was i was not in root user at instance. Will that effect anything?

<?php
error_reporting(-1);
header("Content-type: text/plain; charset=utf-8");
require_once '../sdk.class.php';
// Instantiate the class
$ec2 = new AmazonEC2();
$userdata2="touch hi".".txt";
echo $userdata2;
$response = $ec2->run_instances('ami-5f21e236', 1, 1,
array('KeyName' => 'radix2'),
array('UserData' => $userdata2),
array('InstanceType' => 'm1.small')
);
var_dump($response->isOK());
?>

Thanks for the help Eric

4
  • You'll get better answers over on ServerFault. Commented Jan 21, 2012 at 18:21
  • 1
    @ceejayoz Leaving a comment like that without mentioning migration encourages cross-posting, which is not desired. Commented Jan 21, 2012 at 22:12
  • "copy-pasting a question across sites with no changes is considered abusive behavior". So please don't do that. Don't cross-post unless you've carefully tailored your question for each site. If you post, don't get a good answer, and think your question would do better on another site, flag it and ask to have it migrated. Commented Jan 21, 2012 at 22:25
  • sorry i am new to this.. the guy above said to post this to ServerFault so i did.. won't happen again Commented Jan 22, 2012 at 14:10

1 Answer 1

2

If you are trying to run your code as a user-data script, then you need to start the user data with the two characters #!

For example, your touch command might be:

#!/bin/bash
touch /tmp/hello-world.txt

I'm not a PHP programmer, but I think this might work based on my research:

#!/usr/bin/php
<?php
[...your code here...]
?>

Here's the article where I introduced the concept of user-data scripts:

http://alestic.com/2009/06/ec2-user-data-scripts

This has been incorporated into Ubuntu's cloud-init package which is used by a number of AMI distros including Ubuntu and Amazon Linux.

cloud-init has some more flexible ways of specifying things at startup, but the shabang (#!) method is a simple way to run any script at first boot.

Sign up to request clarification or add additional context in comments.

2 Comments

Few things i tried as you said.. $userdata2="#!/bin/bash touch /tmp/hello-world."."txt"; and also putting #!/usr/bin/php before starting php code.. but none combination of them worked. I wanted to know the userdata passed to EC2 is run in bash? if so my userdata should work.. but i cannot see any file being created as i said, checking by ssh.
You'll need to include the newlines just like you would in a command line script. You might want to create the file, test it, then pitas the file in as a user data file (instead of a user data string)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.