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.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题