简体   繁体   English

Amazon EC2无法处理用户数据php

[英]Amazon EC2 not working with user data php

I am launching a ubuntu EC2 instance with my PHP code and passing user data to it. 我正在使用我的PHP代码启动一个ubuntu EC2实例并将用户数据传递给它。 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. 我把它变得非常简单,并将“touch test.txt”作为用户数据发送。 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. 我检查过该文件是不是使用ssh实现的。 One thing was i was not in root user at instance. 有一点是我不是root用户。 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 谢谢Eric的帮助

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: 例如,您的touch命令可能是:

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

I'm not a PHP programmer, but I think this might work based on my research: 我不是PHP程序员,但我认为这可能基于我的研究:

#!/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 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. 这已被纳入Ubuntu的cloud-init软件包,该软件包被许多AMI发行版(包括Ubuntu和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. cloud-init在启动时有一些更灵活的方法来指定事物,但shabang(#!)方法是在第一次启动时运行任何脚本的简单方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM