dongzhu0327 2018-05-07 17:39
浏览 49
已采纳

Boto3 Cli:如何将USERDATA中的变量内容传递给php脚本?

I need to substitute the server name in the user data with the RDS ENDPOINT.

I can get the RDS endpoint but not sure how to substitute that in the php file properly.

Here is how I get the RDS endpoint:

            instances = source.describe_db_instances(DBInstanceIdentifier=db_instance)
            rds_host = instances.get('DBInstances')[0].get('Endpoint').get('Address')

Another way is also:

RDS=aws rds --region ca-central-1 describe-db-instances --query "DBInstances[*].Endpoint.Address"

Next I need to pass the RDS variable content to the php script shown below:

            echo "define('DB_SERVER', $RDS);" >> /var/www/html/dbinfo.php

Here is what I have.

    UserData="""#!/bin/bash

            RDS=aws rds --region ca-central-1 describe-db-instances --query "DBInstances[*].Endpoint.Address"
            echo $RDS > /var/www/html/test.php

            echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php
            cd /var/www/html
            echo "<?php phpinfo(); ?>" > /var/www/html/hello.php
            echo "<?php " >/var/www/html/dbinfo.php          

            echo "define('DB_SERVER', $RDS);" >> /var/www/html/dbinfo.php

            echo "define('DB_USERNAME', 'username');" >> /var/www/html/dbinfo.php
            echo "define('DB_PASSWORD', 'pass');" >> /var/www/html/dbinfo.php
            echo "define('DB_DATABASE', 'dbname');" >> /var/www/html/dbinfo.php
            echo "\$connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD);">> /var/www/html/dbinfo.php
            echo "if (mysqli_connect_errno()) echo "Failed to connect to MySQL: " . mysqli_connect_error();">> /var/www/html/dbinfo.php
            echo "Successfully connected to the RDS instance; $rds_host" >> /var/www/html/dbinfo.php
            echo "?>" >> dbinfo.php
            """

Basically, I need to get the content of rds_host and echo it to the line in the php script where $rds_host is shown.

Also tried the following:

    UserData="""#!/bin/bash
            yum update –y
            yum install httpd php mysql php-mysql git -y
            service httpd start
            chkconfig httpd on
            groupadd www
            usermod -a -G www ec2-user
            chown -R root:www /var/www
            chmod 2775 /var/www
            find /var/www -type d -exec chmod 2775 {} +
            find /var/www -type f -exec chmod 0664 {} +

            echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php
            cd /var/www/html
            echo "<?php phpinfo(); ?>" > /var/www/html/hello.php

            $rds_host='aws rds --region ca-central-1 describe-db-instances --query "DBInstances[*].Endpoint.Address"'
            echo "<?php " >/var/www/html/dbinfo.php          

            echo "define('DB_SERVER', $rds_host);" >> /var/www/html/dbinfo.php

            echo "define('DB_USERNAME', 'username');" >> /var/www/html/dbinfo.php
            echo "define('DB_PASSWORD', 'dbpass');" >> /var/www/html/dbinfo.php
            echo "define('DB_DATABASE', 'dbname');" >> /var/www/html/dbinfo.php
            echo "\$connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD);">> /var/www/html/dbinfo.php
            echo "if (mysqli_connect_errno()) echo "Failed to connect to MySQL: " . mysqli_connect_error();">> /var/www/html/dbinfo.php
            echo "Successfully connected to the RDS instance; $rds_host" >> /var/www/html/dbinfo.php
            echo "?>" >> dbinfo.php
            """

)

and checking the cloud-init-output.log shows:

/var/lib/cloud/instance/scripts/part-001: line 17: =aws rds --region ca-central-1 describe-db-instances --query "DBInstances[*].Endpoint.Address": command not found

Cloud-init v. 0.7.6 finished at Mon, 07 May 2018 23:36:15 +0000. Datasource DataSourceEc2. Up 15.83 seconds

  • 写回答

1条回答 默认 最新

  • doufud21086 2018-05-07 23:07
    关注

    Try something like:

    #!/bin/bash
    yum update –y
    yum install -y httpd24 php70 mysql56-server php70-mysqlnd
    .....
    
    rds_host=$(aws rds --region ca-central-1 describe-db-instances --query "DBInstances[*].Endpoint.Address")
    
    echo "<?php " > /var/www/html/dbinfo.php
    echo "define('DB_SERVER', $rds_host);" >> /var/www/html/dbinfo.php
    
    etc.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥15 pyqt信号槽连接写法
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。