dongzhu0327
dongzhu0327
2018-05-07 17:39

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 doufud21086 3年前

    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.
    
    点赞 评论 复制链接分享