2

I just had a thought that instead of including the CakePHP library along with the application source code for the different applications I run in AWS, would it be possible to move that core library to an AWS S3 store and include it after turning allow_url_include on in the PHP configs. Upon reading this answer here - including a remote file in PHP, I've heard it's a bad practice, but if it's do with loading non sensitive data, would that still apply?

Has anyone else done this type of arrangement? The only reason I'm asking is I have about 5-6 CakePHP applications all running the same library and find it cumbersome to upload it everytime I do a deployment. The actual application specific code is around 3-4 MB, but with the library it increases to 13MB which I need to upload every time.

Any other suggestions which can achieve the same result would also be much appreciated.

Thanks in advance!

Prathamesh Datar
  • 375
  • 1
  • 4
  • 20
  • It's smart in my mind to bundle dependencies with the app at build time. Not deploy time, and not runtime. Certainly not on every request. Automate the builds if this is inconvenient. Shared dependencies must be versions carefully too work reliably over the long term. Instead build a unified build process. Then cache dependencies opportunistically and you'll get the best of both worlds. – erik258 Dec 19 '17 at 23:13

2 Answers2

1

Use Jenkins or CodePipeline to automate builds and deploys. At build time, include all code dependencies and build a deployment package, then deploy the package using CodeDeploy. Each application would follow the same deployment process.

This is a very standard method for deploying web applications with external dependencies.

Rodrigo Murillo
  • 13,080
  • 2
  • 29
  • 50
0

I guess the main concern I was having was to having to keep uploading my software package with the library each time I was making changes to my application knowing that the library itself which was responsible for nearly 80-90% of the total size of the upload package was never changing. That is why I wanted to know if there was a way to load this library into my final application in the cloud without having to actually upload it.

So at first, I pondered on whether I could store the library in S3 and load it from there. But as @Dan Farrell pointed out, that will mean the application would have to load the library with every request which is certainly not the right way to go.

Eventually after doing some research and hands on with Composer I was able to achieve what I wanted to do. AWS will automatically load dependencies using composer if that configuration is presented in the root folder of the application package being uploaded. Since I could add the library as a dependency to the application along with other dependencies, and AWS would run the dependency downloads on deployment it meant that my application packages were light and only contained my domain specific code. So my final composer.json file was like -

{
    "require": {
        "cakephp/cakephp": "2.10.*",
        "aws/aws-sdk-php": "2.*",
        "stripe/stripe-php": "^5.8",
        "facebook/graph-sdk": "^5.6"
    },
    "config": {
        "vendor-dir": "./app/Vendor/"
    }
}

In terms of my application code, the only thing I needed to change was the location of the Cake Library. Since I had configured the composer to load dependencies into the app/Vendor folder, I had to change the path to the library in the index.php file in app/webroot.

But surprisingly enough, the CakePHP authors have already provisioned this. There is the initial declaration of the path which is used by default when the library is inline with the app folder. That just needs to be commented out - See below -

/**
* The absolute path to the "cake" directory, WITHOUT a trailing DS.
*
* Un-comment this line to specify a fixed path to CakePHP.
* This should point at the directory containing `Cake`.
*
* For ease of development CakePHP uses PHP's include_path. If you
* cannot modify your include_path set this value.
*
* Leaving this constant undefined will result in it being defined in Cake/bootstrap.php
*
* The following line differs from its sibling
* /lib/Cake/Console/Templates/skel/webroot/index.php
*/
//define('CAKE_CORE_INCLUDE_PATH', ROOT .DS. 'lib'); <-- Comment this line out

The next couple of lines will set the library path to the path where composer would have downloaded the same -

/**
* This auto-detects CakePHP as a composer installed library.
* You may remove this if you are not planning to use composer (not recommended, though).
*/
$vendorPath = ROOT . DS . APP_DIR . DS . 'Vendor' . DS . 'cakephp' . DS . 'cakephp' . DS . 'lib';
$dispatcher = 'Cake' . DS . 'Console' . DS . 'ShellDispatcher.php';
if (!defined('CAKE_CORE_INCLUDE_PATH') && file_exists($vendorPath . DS . $dispatcher)) {
    define('CAKE_CORE_INCLUDE_PATH', $vendorPath);
}

And that's how I saved having to upload 7.2MB every time down to 2.6MB which for an internet connection with low upload speeds has meant an improvement in the time required to upload a new application version with the same output.

Hope this helps.

Prathamesh Datar
  • 375
  • 1
  • 4
  • 20