Search code examples
jsonamazon-web-servicestemplatesaws-cloudformationlamp

Using AWS CloudFormation to launch AutoScaling servers with latest Apache and PHP?


I would like to launch a LAMP stack with Autoscaled EC2 instances through CloudFormation template. Whenever I try it, stack is built successfully but when I go onto the website, it gives me a message that app does not support older versions of PHP.

Now, when I searched for this problem online, I found one answer that suggested installing newer versions of httpd and php through yum. I tried that directly on the EC2 linux terminal and it worked, I could now access the website successfully. The thing is, I want these updated versions to be installed directly in the template. For this purpose I replaced this code within AutoScaling::LaunchConfiguration:

       "setup" : {
        "packages" : {
            "yum" : {
              "nfs-utils"       : [],
              "httpd"           : [],
              "php"             : [],
              "mysql"           : []
            }
        },

with:

       "setup" : {
        "packages" : {
            "yum" : {
              "nfs-utils"       : [],
              "httpd24"         : [],
              "php72"           : [],
              "mysql"           : []
            }
        },

This causes the stack to fail to create AutoScalingGroup which has the LaunchConfiguration as param, with an error stating "Received 0 SUCCESS signal". Here's my Properties of LaunchConfiguration:

    "Properties": {
    "AssociatePublicIpAddress" : true,
    "ImageId" : { "Fn::FindInMap" : [ "AWSRegionArch2AMI", { "Ref" : "AWS::Region" },
              { "Fn::FindInMap" : [ "AWSInstanceType2Arch", { "Ref" : "InstanceType" }, "Arch" ] } ] },
    "InstanceType"   : { "Ref" : "InstanceType" },
    "SecurityGroups" : [ {"Ref" : "WebServerSecurityGroup"} ],
    "KeyName"        : { "Ref" : "KeyName" },
    "IamInstanceProfile" : { "Ref" : "CloudWatchPutMetricsInstanceProfile" },
    "UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
            "#!/bin/bash -xe\n",
            "yum update -y aws-cfn-bootstrap\n",

            "/opt/aws/bin/cfn-init -v ",
            "         --stack ", { "Ref" : "AWS::StackName" },
            "         --resource LaunchConfig ",
            "         --configsets app_install ",
            "         --region ", { "Ref" : "AWS::Region" }, "\n",

            "/opt/aws/bin/cfn-signal -e $? ",
            "         --stack ", { "Ref" : "AWS::StackName" },
            "         --resource WebServerGroup ",
            "         --region ", { "Ref" : "AWS::Region" }, "\n"
    ]]}}
  }

Any ideas why it doesn't work? Any suggestions what I can do to actually install newest httpd and php directly through the template? Should I remove packages section and just put script for installing httpd and php directly in the UserData?


Solution

  • I managed to sort it out myself. I used Ubuntu 18.04 AMI instead of Linux as it supports latest versions of PHP and Apache.

    Also had to change the "UserData" a bit:

            "UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
            "#!/bin/bash -xe\n",
            "apt-get update -y\n",
            "apt-get install -y python-pip\n",
            "mkdir -p /opt/aws/bin\n",
            "python /usr/lib/python2.7/dist-packages/easy_install.py --script-dir /opt/aws/bin https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz\n",
            "ln -s /usr/local/lib/python2.7/dist-packages/aws_cfn_bootstrap-1.4-py2.7.egg/init/ubuntu/cfn-hup /etc/init.d/cfn-hup \n",
            "systemctl daemon-reload \n",
    
            "chmod 700 /etc/init.d/cfn-hup \n",
            "chown root:root /etc/init.d/cfn-hup \n",
            "update-rc.d cfn-hup defaults \n",
            "update-rc.d cfn-hup enable \n",
    
            "/opt/aws/bin/cfn-init -v ",
            "         --stack ", { "Ref" : "AWS::StackName" },
            "         --resource LaunchConfig ",
            "         --configsets app_install ",
            "         --region ", { "Ref" : "AWS::Region" }, "\n",
    
            "/opt/aws/bin/cfn-signal -e $? ",
            "         --stack ", { "Ref" : "AWS::StackName" },
            "         --resource WebServerGroup ",
            "         --region ", { "Ref" : "AWS::Region" }, "\n"
        ]]}}
    

    and because Ubuntu uses apt instead of yum, this is how I included packages:

              "setup" : {
            "packages" : {
                "apt" : {
                  "nfs-common"    : [],
                  "apache2"       : [],
                  "php"           : [],
                  "php-mysql"     : [],
                  "mysql-common"  : []
                }
            },