Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Laravel/dynamodb-session/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{yml,yaml}]
indent_size = 2
46 changes: 46 additions & 0 deletions Laravel/dynamodb-session/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
5 changes: 5 additions & 0 deletions Laravel/dynamodb-session/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
* text=auto
*.css linguist-vendored
*.scss linguist-vendored
*.js linguist-vendored
CHANGELOG.md export-ignore
15 changes: 15 additions & 0 deletions Laravel/dynamodb-session/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/node_modules
/public/hot
/public/storage
/storage/*.key
/vendor
.env
.env.backup
.phpunit.result.cache
Homestead.json
Homestead.yaml
npm-debug.log
yarn-error.log

.serverless
.idea
13 changes: 13 additions & 0 deletions Laravel/dynamodb-session/.styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
php:
preset: laravel
disabled:
- unused_use
finder:
not-name:
- index.php
- server.php
js:
finder:
not-name:
- webpack.mix.js
css: true
4 changes: 4 additions & 0 deletions Laravel/dynamodb-session/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
deploy:
composer install --prefer-dist --optimize-autoloader --no-dev
php artisan config:clear
serverless deploy
151 changes: 151 additions & 0 deletions Laravel/dynamodb-session/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
# Laravel 6.* - Use DynamoDB for session or/and cache

## Configuration

### Initial configuration

Read the Bref documentation about Laravel : https://bref.sh/docs/frameworks/laravel.html

### DynamoDB Resource

Look the file `serverless.yml`

#### Resources

First create the resource for your DynamoDB Table :

```yaml
resources: # CloudFormation template syntax from here on.
Resources:
laravelTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: laravelTable #Change with your table name
AttributeDefinitions:
- AttributeName: key #Primary key name used by the Laravel framework
AttributeType: S
KeySchema:
- AttributeName: key
KeyType: HASH
TimeToLiveSpecification:
AttributeName: expires_at #Key used for the expiration date
Enabled: true
ProvisionedThroughput:
ReadCapacityUnits: 1 #Adapt provisioning capacity according to your needs
WriteCapacityUnits: 1
```

#### IAM Role Statements

Declare the `iamRoleStatements` for your DynamoDB Table :

```yaml
provider:
...
iamRoleStatements:
- Effect: "Allow"
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource:
Fn::GetAtt:
- laravelTable
- Arn
```

#### Environment variables

Declare your environment variable

```yaml
provider:
...
environment:
APP_ENV: production
APP_DEBUG: true # set to false when moving to production
LOG_CHANNEL: stderr # this will send logs to CloudWatch automatically
SESSION_DRIVER: dynamodb # used the dynamodb driver to store the session data
VIEW_COMPILED_PATH: /tmp/storage/framework/views
CACHE_DRIVER: dynamodb # eventually, you can used the dynamodb driver to store the cache data
CACHE_STORE: dynamodb # used the dynamodb cache store
DYNAMODB_CACHE_TABLE: laravelTable
```

#### Finale serverless.yml file

```yaml
service: app

provider:
name: aws
region: us-east-1
runtime: provided
iamRoleStatements:
- Effect: "Allow"
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource:
Fn::GetAtt:
- laravelTable
- Arn
environment:
APP_ENV: production
APP_KEY: base64:FQKkzq8bh6GjE5XnTnm8JXgOTqUYMfQ6BNLR234d9lw=
APP_DEBUG: true # set to false when moving to production
LOG_CHANNEL: stderr # this will send logs to CloudWatch automatically
SESSION_DRIVER: dynamodb # to avoid writing sessions to disk
VIEW_COMPILED_PATH: /tmp/storage/framework/views
CACHE_DRIVER: dynamodb
CACHE_STORE: dynamodb
DYNAMODB_CACHE_TABLE: laravelTable

# Exclude files from deployment
package:
exclude:
- node_modules/**
- public/storage
- storage/**
- tests/**
- .env*

plugins:
- ./vendor/bref/bref

functions:
api:
handler: public/index.php
description: ''
timeout: 28 # in seconds (API Gateway has a timeout of 29 seconds)
layers:
- ${bref:layer.php-74-fpm}
events:
- httpApi: '*'

resources: # CloudFormation template syntax from here on.
Resources:
laravelTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: laravelTable
AttributeDefinitions:
- AttributeName: key
AttributeType: S
KeySchema:
- AttributeName: key
KeyType: HASH
TimeToLiveSpecification:
AttributeName: expires_at
Enabled: true
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
```
42 changes: 42 additions & 0 deletions Laravel/dynamodb-session/app/Console/Kernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
//
];

/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
}

/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');

require base_path('routes/console.php');
}
}
55 changes: 55 additions & 0 deletions Laravel/dynamodb-session/app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
//
];

/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
];

/**
* Report or log an exception.
*
* @param \Exception $exception
* @return void
*
* @throws \Exception
*/
public function report(Exception $exception)
{
parent::report($exception);
}

/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Exception
*/
public function render($request, Exception $exception)
{
return parent::render($request, $exception);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\ConfirmsPasswords;

class ConfirmPasswordController extends Controller
{
/*
|--------------------------------------------------------------------------
| Confirm Password Controller
|--------------------------------------------------------------------------
|
| This controller is responsible for handling password confirmations and
| uses a simple trait to include the behavior. You're free to explore
| this trait and override any functions that require customization.
|
*/

use ConfirmsPasswords;

/**
* Where to redirect users when the intended url fails.
*
* @var string
*/
protected $redirectTo = RouteServiceProvider::HOME;

/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
}
Loading