{"id":320,"date":"2023-05-08T08:23:10","date_gmt":"2023-05-08T08:23:10","guid":{"rendered":"https:\/\/keyshell.net\/blog\/?p=320"},"modified":"2023-05-08T08:23:10","modified_gmt":"2023-05-08T08:23:10","slug":"deploying-a-serverless-laravel-application-in-aws-using-bref","status":"publish","type":"post","link":"https:\/\/keyshell.net\/blog\/2023\/05\/08\/deploying-a-serverless-laravel-application-in-aws-using-bref\/","title":{"rendered":"Deploying a Serverless Laravel Application in AWS using Bref"},"content":{"rendered":"<p>Serverless deployment is a new service that AWS is offering to help face the challenge of scaling applications, expenses etc. Developers who want to decrease their go-to-market time and build lightweight, flexible applications that can be expanded or updated quickly, benefit greatly from serverless computing. It offers greater scalability, more flexibility, and quicker time to release, all at a reduced cost.<\/p>\n<p>Advantages:<\/p>\n<ul>\n<li>No server management is necessary<\/li>\n<li>Only charged for the server space they use<\/li>\n<li>Scalable<\/li>\n<li>Quick deployments and updates are possible<\/li>\n<li>Low latency<\/li>\n<\/ul>\n<p>Disadvantages:<\/p>\n<ul>\n<li>Testing are debugging become difficult<\/li>\n<li>Security concerns<\/li>\n<li>Serverless architectures are not built for long running processes<\/li>\n<li>Performance may be affected<\/li>\n<\/ul>\n<p>Environments used:<\/p>\n<ul>\n<li>Linux machine<\/li>\n<li>AWS IAM user<\/li>\n<li>PHP<\/li>\n<li>Composer<\/li>\n<\/ul>\n<p>You can place the directory containing the application file in whichever location.<\/p>\n<ol>\n<li>Create an AWS account. Then, create an IAM user with programmatic access and get the access key ID and secret access key.<\/li>\n<li>Ensure that the php extensions curl and xml are enabled. You can check using the following command.<\/li>\n<\/ol>\n<pre>php -m | grep xml\r\n\r\nphp -m | grep curl<\/pre>\n<p>If not enabled, install using the command,<\/p>\n<pre>sudo apt-get install php-xml\r\n\r\nsudo apt-get install php-curl<\/pre>\n<p>Enable them in the file \u2018\/etc\/php\/8.1\/cli\/php.ini\u2019 and restart apache using \u2018service apache2 restart\u2019<\/p>\n<ol start=\"3\">\n<li>Now, we have to install and configure the serverless framework as a global dependency.<\/li>\n<\/ol>\n<pre>npm install -g serverless<\/pre>\n<p>Now, we have to add the AWS IAM user credentials using the following commad.<\/p>\n<pre>serverless config credentials --provider aws \u2013key &lt;key&gt;\u00a0 --secret &lt;secret&gt;<\/pre>\n<ol start=\"4\">\n<li>Install the latest version of Laravel and also create new project in it.<\/li>\n<\/ol>\n<pre>composer global require laravel\/installer<\/pre>\n<p>Get the path and add it to the PATH variable.<\/p>\n<pre>composer global config bin-dir \u2013absolute<\/pre>\n<pre>\/root\/.config\/composer\/vendor\/bin (This is the path I obtained). Add it using the below command.<\/pre>\n<pre>echo 'export PATH=\"$PATH:\/root\/.config\/composer\/vendor\/bin\"' &gt;&gt; ~\/.bashrc<\/pre>\n<p>Now, check the laravel version using,<\/p>\n<pre>laravel --version\r\n\r\nLaravel Installer 4.5.0<\/pre>\n<ol start=\"5\">\n<li>To create new app,<\/li>\n<\/ol>\n<pre>laravel new serverless-app<\/pre>\n<p>To have Bref configured with Laravel we also need to install the laravel-bridge component.<\/p>\n<pre>composer require bref\/bref bref\/laravel-bridge --with-all-dependencies<\/pre>\n<ol start=\"6\">\n<li>Now, we have to create a serverless.yaml file.<\/li>\n<\/ol>\n<pre>php artisan vendor:publish --tag=serverless-config<\/pre>\n<p>Following is its contents.<\/p>\n<p>&nbsp;<\/p>\n<pre>service: laravel\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\r\nprovider:\r\n\r\n\u00a0\u00a0\u00a0 name: aws\r\n\r\n\u00a0\u00a0\u00a0 # The AWS region in which to deploy (us-east-1 is the default)\r\n\r\n\u00a0\u00a0\u00a0 region: us-east-1\r\n\r\n\u00a0\u00a0\u00a0 # The stage of the application, e.g. dev, production, staging\u2026 ('dev' is the default)\r\n\r\n\u00a0\u00a0\u00a0 stage: dev\r\n\r\n\u00a0\u00a0\u00a0 runtime: provided.al2\r\n\r\npackage:\r\n\r\n\u00a0\u00a0\u00a0 # Directories to exclude from deployment\r\n\r\n\u00a0\u00a0\u00a0 exclude:\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0- node_modules\/**\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 - public\/storage\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 - resources\/assets\/**\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 - storage\/**\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 - tests\/**\r\n\r\nfunctions:\r\n\r\n\u00a0\u00a0\u00a0 # This function runs the Laravel website\/API\r\n\r\n\u00a0\u00a0\u00a0 web:\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 handler: public\/index.php\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 timeout: 28 # in seconds (API Gateway has a timeout of 29 seconds)\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 layers:\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 - ${bref:layer.php-74-fpm}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 events:\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 -\u00a0\u00a0 httpApi: '*'\r\n\r\n\u00a0\u00a0\u00a0 # This function lets us run artisan commands in Lambda\r\n\r\n\u00a0\u00a0\u00a0 artisan:\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 handler: artisan\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 timeout: 120 # in seconds\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 layers:\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 - ${bref:layer.php-74} # PHP\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 - ${bref:layer.console} # The \"console\" layer\r\n\r\nplugins:\r\n\r\n\u00a0\u00a0\u00a0 # We need to include the Bref plugin\r\n\r\n\u00a0\u00a0\u00a0 - .\/vendor\/bref\/bref<\/pre>\n<p>&nbsp;<\/p>\n<ol start=\"7\">\n<li>Before deploying, we need to clear any configuration change generated on our machine.<\/li>\n<\/ol>\n<pre>php artisan config:cache\r\n\r\nphp artisan config:clear<\/pre>\n<ol start=\"8\">\n<li>In case if your application requires database, you can deploy it in a server or in AWS RDS.<\/li>\n<li>Next we need to change the location of compiled view and a few other things like changing session driver to cookie and log channel to standard error in environment (.env) file.<\/li>\n<\/ol>\n<p>Open .env file and add following:<\/p>\n<pre>CACHE_DRIVER =array\r\nVIEW_COMPILED_PATH =\/tmp\/storage\/framework\/views \r\nSESSION_DRIVER =array \r\nLOG_CHANNEL =stderr<\/pre>\n<ol start=\"10\">\n<li>Next we need to make changes in app service provider so that if compiled view directory is not present then it should recreate it automatically.<\/li>\n<\/ol>\n<p>So copy and paste below section in boot method of app\/Providers\/AppServiceProvider.php<\/p>\n<pre>public function boot()\r\n{\r\n\/\/ Make sure the directory for compiled views exist\r\nif (! is_dir(config('view.compiled'))) {\r\nmkdir(config('view.compiled'), 0755, true);\r\n}\r\n}<\/pre>\n<p>11. After that, we\u2019re ready to deploy.<\/p>\n<pre>serverless deploy<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Serverless deployment is a new service that AWS is offering to help face the challenge of scaling applications, expenses etc. Developers who want to decrease their go-to-market time and build lightweight, flexible applications that can be expanded or updated quickly, benefit greatly from serverless computing. It offers greater scalability, more flexibility, and quicker time to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[1],"tags":[22,21,18,19,20],"class_list":["post-320","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-aws","tag-bref","tag-laravel","tag-php","tag-serverless"],"_links":{"self":[{"href":"https:\/\/keyshell.net\/blog\/wp-json\/wp\/v2\/posts\/320","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/keyshell.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/keyshell.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/keyshell.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/keyshell.net\/blog\/wp-json\/wp\/v2\/comments?post=320"}],"version-history":[{"count":2,"href":"https:\/\/keyshell.net\/blog\/wp-json\/wp\/v2\/posts\/320\/revisions"}],"predecessor-version":[{"id":322,"href":"https:\/\/keyshell.net\/blog\/wp-json\/wp\/v2\/posts\/320\/revisions\/322"}],"wp:attachment":[{"href":"https:\/\/keyshell.net\/blog\/wp-json\/wp\/v2\/media?parent=320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/keyshell.net\/blog\/wp-json\/wp\/v2\/categories?post=320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/keyshell.net\/blog\/wp-json\/wp\/v2\/tags?post=320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}