, "WebServerInstance2": { "Type": "AWS::EC2::Instance", "Metadata": { "AWS::CloudFormation::Init": { "configSets": { "InstallAndRun": [ "Install", "Configure" ] }, "Install": { "packages": { "yum": { "mysql": [], "mysql-server": [], "mysql-libs": [], "httpd": [], "php": [], "php-mysql": [] } }, "files": { "/var/www/html/index.php": { "content": { "Fn::Join": [ "", [ "\n", " \n", " AWS CloudFormation PHP Sample\n", " \n", " \n", " \n", "

Welcome to the AWS CloudFormation PHP Sample

\n", "

\n", " \";\n", " print date(\"g:i A l, F j Y.\");\n", " ?>\n", "

\n", " \";\n", " }\n", " else\n", " {\n", " print \"Server = \" . $hostname . \"
\";\n", " }\n", " // Get the instance-id of the intance from the instance metadata\n", " curl_setopt($curl_handle,CURLOPT_URL,'http://169.254.169.254/latest/meta-data/instance-id');\n", " $instanceid = curl_exec($curl_handle);\n", " if (empty($instanceid))\n", " {\n", " print \"Sorry, for some reason, we got no instance id back
\";\n", " }\n", " else\n", " {\n", " print \"EC2 instance-id = \" . $instanceid . \"
\";\n", " }\n", " $Database = \"localhost\";\n", " $DBUser = \"", { "Ref": "DBUser" }, "\";\n", " $DBPassword = \"", { "Ref": "DBPassword" }, "\";\n", " print \"Database = \" . $Database . \"
\";\n", " $dbconnection = mysql_connect($Database, $DBUser, $DBPassword)\n", " or die(\"Could not connect: \" . mysql_error());\n", " print (\"Connected to $Database successfully\");\n", " mysql_close($dbconnection);\n", " ?>\n", "

PHP Information

\n", "

\n", " \n", " \n", "\n" ] ] }, "mode": "000600", "owner": "apache", "group": "apache" }, "/tmp/setup.mysql": { "content": { "Fn::Join": [ "", [ "CREATE DATABASE ", { "Ref": "DBName" }, ";\n", "GRANT ALL ON ", { "Ref": "DBName" }, ".* TO '", { "Ref": "DBUser" }, "'@localhost IDENTIFIED BY '", { "Ref": "DBPassword" }, "';\n" ] ] }, "mode": "000400", "owner": "root", "group": "root" }, "/etc/cfn/cfn-hup.conf": { "content": { "Fn::Join": [ "", [ "[main]\n", "stack=", { "Ref": "AWS::StackId" }, "\n", "region=", { "Ref": "AWS::Region" }, "\n" ] ] }, "mode": "000400", "owner": "root", "group": "root" }, "/etc/cfn/hooks.d/cfn-auto-reloader.conf": { "content": { "Fn::Join": [ "", [ "[cfn-auto-reloader-hook]\n", "triggers=post.update\n", "path=Resources.WebServerInstance.Metadata.AWS::CloudFormation::Init\n", "action=/opt/aws/bin/cfn-init -v ", " --stack ", { "Ref": "AWS::StackName" }, " --resource WebServerInstance2 ", " --configsets InstallAndRun ", " --region ", { "Ref": "AWS::Region" }, "\n", "runas=root\n" ] ] }, "mode": "000400", "owner": "root", "group": "root" } }, "services": { "sysvinit": { "mysqld": { "enabled": "true", "ensureRunning": "true" }, "httpd": { "enabled": "true", "ensureRunning": "true" }, "cfn-hup": { "enabled": "true", "ensureRunning": "true", "files": [ "/etc/cfn/cfn-hup.conf", "/etc/cfn/hooks.d/cfn-auto-reloader.conf" ] } } } }, "Configure": { "commands": { "01_set_mysql_root_password": { "command": { "Fn::Join": [ "", [ "mysqladmin -u root password '", { "Ref": "DBRootPassword" }, "'" ] ] }, "test": { "Fn::Join": [ "", [ "$(mysql ", { "Ref": "DBName" }, " -u root --password='", { "Ref": "DBRootPassword" }, "' >/dev/null 2>&1 /dev/null 2>&1