Slide 1

Slide 1 text

RUNNING YOUR LARAVEL PROJECT ON AWS LAMBDA

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

WE ARE HIRING!

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

RUNNING YOUR LARAVEL PROJECT ON AWS LAMBDA

Slide 8

Slide 8 text

(coupling yourself to AWS for fun & profit) COUPLING YOURSELF TO AWS

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

WHY?

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

EC2

Slide 14

Slide 14 text

EC2 Data Store

Slide 15

Slide 15 text

EC2 Data Store

Slide 16

Slide 16 text

EC2 Data Store Load Balancer

Slide 17

Slide 17 text

WTF?

Slide 18

Slide 18 text

EC2 Data Store Load Balancer

Slide 19

Slide 19 text

EC2 Data Store Load Balancer EC2 EC2

Slide 20

Slide 20 text

EC2 Data Store Load Balancer EC2 EC2

Slide 21

Slide 21 text

EC2 Data Store Load Balancer EC2 EC2

Slide 22

Slide 22 text

SAY HELLO TO FaaS

Slide 23

Slide 23 text

Think of FaaS as a server that doesn’t exist…

Slide 24

Slide 24 text

… until you need it …

Slide 25

Slide 25 text

… and goes away again once the job is done

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

1 million requests FREE per month

Slide 28

Slide 28 text

$0.20 per million requests thereafter

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

Container

Slide 31

Slide 31 text

Container Bootstrap Code

Slide 32

Slide 32 text

Container Bootstrap Code

Slide 33

Slide 33 text

Container Bootstrap Code Application Code

Slide 34

Slide 34 text

Container Bootstrap Code Application Code

Slide 35

Slide 35 text

Container Bootstrap Code Application Code

Slide 36

Slide 36 text

LAYERS

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

Lambda function Bootstrap PHP Layer Handler

Slide 40

Slide 40 text

Lambda function Handler PHP Layer Bootstrap

Slide 41

Slide 41 text

Lambda function Handler PHP Layer Bootstrap

Slide 42

Slide 42 text

Lambda function Handler PHP Layer Bootstrap

Slide 43

Slide 43 text

Lambda function Handler PHP Layer Bootstrap

Slide 44

Slide 44 text

You can use up to 5 different layers in a Lambda function

Slide 45

Slide 45 text

The total unzipped size of code & layers must be < 250 MB

Slide 46

Slide 46 text

Default account limit of 1,000 concurrent invocations per AWS region

Slide 47

Slide 47 text

Maximum request body size is 6 MB

Slide 48

Slide 48 text

RUNNING PHP ON LAMBDA

Slide 49

Slide 49 text

IS A PITA RUNNING PHP ON LAMBDA

Slide 50

Slide 50 text

Lambda function Handler PHP Layer Bootstrap

Slide 51

Slide 51 text

Lambda function Handler PHP Layer Bootstrap

Slide 52

Slide 52 text

https://bref.sh

Slide 53

Slide 53 text

BREF AIMS TO MAKE RUNNING PHP APPS SIMPLE

Slide 54

Slide 54 text

SIMPLIFY PROBLEMS BY REMOVING CHOICES

Slide 55

Slide 55 text

PROVIDE SIMPLE AND FAMILIAR SOLUTIONS

Slide 56

Slide 56 text

EMPOWER BY SHARING KNOWLEDGE

Slide 57

Slide 57 text

WHAT DOES BREF ACTUALLY DO?

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

SAM DEALS WITH DEPLOYING

Slide 62

Slide 62 text

Lambda function Handler PHP Layer Bootstrap

Slide 63

Slide 63 text

Lambda function Handler PHP Layer Bootstrap

Slide 64

Slide 64 text

API Gateway Lambda function Handler PHP Layer Bootstrap

Slide 65

Slide 65 text

API Gateway Lambda function Handler PHP Layer Bootstrap

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

Hello, World!'; phpinfo(); hello-world.php

Slide 69

Slide 69 text

$ composer require mnapoli/bref

Slide 70

Slide 70 text

$ php vendor/bin/bref init

Slide 71

Slide 71 text

What kind of lambda do you want to create? [0] PHP function [1] HTTP application [2] Console application >

Slide 72

Slide 72 text

What kind of lambda do you want to create? [0] PHP function [1] HTTP application [2] Console application > 1

Slide 73

Slide 73 text

In which AWS region do you want to deploy your application? >

Slide 74

Slide 74 text

In which AWS region do you want to deploy your application? > eu-west-1

Slide 75

Slide 75 text

[OK] Project initialized and ready to test or deploy. The files created were automatically added to git.

Slide 76

Slide 76 text

Hello, World!'; phpinfo(); hello-world.php index.php template.yaml

Slide 77

Slide 77 text

Hello, World!'; phpinfo(); hello-world.php template.yaml

Slide 78

Slide 78 text

AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: '' Resources: MyFunction: Type: AWS::Serverless::Function Properties: FunctionName: 'my-function' Description: '' CodeUri: . Handler: index.php Timeout: 30 # in seconds (API Gateway has a timeout of 30 se Runtime: provided Layers: - ‘arn:aws:lambda:eu-west-1:209497400698:layer:php-73-fp Events: # The function will match all HTTP URLs HttpRoot: Type: Api Properties: Path: / Method: ANY HttpSubPaths: Type: Api Properties: Path: /{proxy+} hello-world.php template.yaml

Slide 79

Slide 79 text

# ... Resources: MyFunction: Type: AWS::Serverless::Function Properties: FunctionName: 'my-function' Description: '' CodeUri: . Handler: index.php Timeout: 30 # in seconds Runtime: provided hello-world.php template.yaml

Slide 80

Slide 80 text

# ... Resources: MyFunction: Type: AWS::Serverless::Function Properties: FunctionName: 'bref-hello-world' Description: '' CodeUri: . Handler: hello-world.php Timeout: 30 # in seconds Runtime: provided hello-world.php template.yaml

Slide 81

Slide 81 text

# ... Layers: - 'arn:aws:lambda:eu-west-1:209497400698:layer:php-73-fpm:1' Events: # The function will match all HTTP URLs HttpRoot: Type: Api Properties: Path: / Method: ANY HttpSubPaths: Type: Api Properties: Path: /{proxy+} Method: ANY # ... hello-world.php template.yaml

Slide 82

Slide 82 text

# ... Layers: - 'arn:aws:lambda:eu-west-1:209497400698:layer:php-73-fpm:1' Events: # The function will match all HTTP URLs HttpRoot: Type: Api Properties: Path: / Method: ANY HttpSubPaths: Type: Api Properties: Path: /{proxy+} Method: ANY # ... hello-world.php template.yaml

Slide 83

Slide 83 text

# ... Layers: - 'arn:aws:lambda:eu-west-1:209497400698:layer:php-73-fpm:1' Events: # The function will match all HTTP URLs HttpRoot: Type: Api Properties: Path: / Method: ANY HttpSubPaths: Type: Api Properties: Path: /{proxy+} Method: ANY # ... hello-world.php template.yaml

Slide 84

Slide 84 text

# ... Layers: - 'arn:aws:lambda:eu-west-1:209497400698:layer:php-73-fpm:1' Events: # The function will match all HTTP URLs HttpRoot: Type: Api Properties: Path: / Method: ANY HttpSubPaths: Type: Api Properties: Path: /{proxy+} Method: ANY # ... hello-world.php template.yaml

Slide 85

Slide 85 text

DEPLOYING

Slide 86

Slide 86 text

$ brew upgrade && brew update $ brew tap aws/tap $ brew install aws-sam-cli

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

$ aws s3 mb s3://bref-hello-world

Slide 89

Slide 89 text

make_bucket: bref-hello-world

Slide 90

Slide 90 text

$ sam package \ --output-template-file .stack.yaml \ --s3-bucket

Slide 91

Slide 91 text

$ sam package \ --output-template-file .stack.yaml \ --s3-bucket bref-hello-world

Slide 92

Slide 92 text

Uploading to cf39403efc50314711b61effd4f5c948 2841371 / 2841371.0 (100.00%) Successfully packaged artifacts and wrote output template to file .stack.yaml.

Slide 93

Slide 93 text

No content

Slide 94

Slide 94 text

Execute the following command to deploy the packaged template aws cloudformation deploy --template-file / Users/neal/www/hello-world/.stack.yaml --stack- name

Slide 95

Slide 95 text

$ aws cloudformation deploy --template-file / Users/neal/www/hello-world/.stack.yaml --stack- name

Slide 96

Slide 96 text

$ aws cloudformation deploy --template-file / Users/neal/www/hello-world/.stack.yaml --stack- name bref-hello-world

Slide 97

Slide 97 text

Waiter encountered a terminal failure state Status: FAILED. Reason: Requires capabilities : [CAPABILITY_IAM]

Slide 98

Slide 98 text

$ sam deploy \ --template-file .stack.yaml \ --capabilities CAPABILITY_IAM \ --stack-name

Slide 99

Slide 99 text

$ sam deploy \ --template-file .stack.yaml \ --capabilities CAPABILITY_IAM \ --stack-name bref-hello-world

Slide 100

Slide 100 text

Failed to create/update the stack. Run the following command to fetch the list of events leading up to the failure aws cloudformation describe-stack-events -- stack-name bref-hello-world

Slide 101

Slide 101 text

$ php vendor/bin/bref deployment \ --region eu-west-1 -- bref-hello-world

Slide 102

Slide 102 text

AWS::Lambda::Function Error occurred while GetObject. S3 Error Code: PermanentRedirect. S3 Error Message: The bucket is in this region: us-east-1.

Slide 103

Slide 103 text

No content

Slide 104

Slide 104 text

No content

Slide 105

Slide 105 text

$ aws s3 mb s3://bref-hello-world --region eu-west-1

Slide 106

Slide 106 text

$ sam package \ --output-template-file .stack.yaml \ --s3-bucket bref-hello-world

Slide 107

Slide 107 text

$ sam deploy \ --template-file .stack.yaml \ --capabilities CAPABILITY_IAM \ --stack-name bref-hello-world

Slide 108

Slide 108 text

Successfully created/updated stack - bref-hello-world

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

No content

Slide 111

Slide 111 text

API Gateway Lambda function Handler PHP Layer Bootstrap

Slide 112

Slide 112 text

No content

Slide 113

Slide 113 text

No content

Slide 114

Slide 114 text

No content

Slide 115

Slide 115 text

No content

Slide 116

Slide 116 text

RECAP

Slide 117

Slide 117 text

Looked how FaaS can simplify architecture & save money

Slide 118

Slide 118 text

Installed AWS CLI

Slide 119

Slide 119 text

Added Bref to an existing project

Slide 120

Slide 120 text

Explored the template file

Slide 121

Slide 121 text

Created a deployment bucket

Slide 122

Slide 122 text

Created a deployment bucket in the correct region

Slide 123

Slide 123 text

Packaged & deployed the application

Slide 124

Slide 124 text

Explored the stack

Slide 125

Slide 125 text

Tested the PHP application running on Lambda

Slide 126

Slide 126 text

REFACTORING A FULLY FEATURED WEBSITE

Slide 127

Slide 127 text

No content

Slide 128

Slide 128 text

No content

Slide 129

Slide 129 text

No content

Slide 130

Slide 130 text

No content

Slide 131

Slide 131 text

REMEMBER

Slide 132

Slide 132 text

The filesystem is read-only

Slide 133

Slide 133 text

(apart from /tmp)

Slide 134

Slide 134 text

The total unzipped size of code & layers must be < 250 MB

Slide 135

Slide 135 text

$ composer require mnapoli/bref

Slide 136

Slide 136 text

$ php vendor/bin/bref init

Slide 137

Slide 137 text

What kind of lambda do you want to create? [0] PHP function [1] HTTP application [2] Console application > 1

Slide 138

Slide 138 text

In which AWS region do you want to deploy your application? > eu-west-1

Slide 139

Slide 139 text

# ... Resources: WebApplication: Type: AWS::Serverless::Function Properties: FunctionName: ‘bref-kittyfinder' Description: '' CodeUri: . Handler: public/index.php Timeout: 30 # in seconds Runtime: provided MemorySize: 1024 app config database bootstrap template.yaml public resources routes storage

Slide 140

Slide 140 text

# ... Resources: WebApplication: Type: AWS::Serverless::Function Properties: FunctionName: ‘bref-kittyfinder' Description: '' CodeUri: . Handler: public/index.php Timeout: 30 # in seconds Runtime: provided MemorySize: 1024 app config database bootstrap template.yaml public resources routes storage

Slide 141

Slide 141 text

# ... Resources: WebApplication: Type: AWS::Serverless::Function Properties: FunctionName: ‘bref-kittyfinder' Description: '' CodeUri: . Handler: public/index.php Timeout: 30 # in seconds Runtime: provided MemorySize: 1024 app config database bootstrap template.yaml public resources routes storage

Slide 142

Slide 142 text

$ aws s3 mb s3://bref-kitty-finder-bucket

Slide 143

Slide 143 text

$ sam package \ --output-template-file .stack.yaml \ --s3-bucket bref-kitty-finder

Slide 144

Slide 144 text

$ sam deploy \ --template-file .stack.yaml \ --capabilities CAPABILITY_IAM \ --stack-name bref-kitty-finder

Slide 145

Slide 145 text

Failed to create/update the stack. Run the following command to fetch the list of events leading up to the failure aws cloudformation describe-stack-events -- stack-name bref-kitty-finder

Slide 146

Slide 146 text

Unzipped size must be smaller than 155930325 bytes

Slide 147

Slide 147 text

$ composer install --optimize-autoloader --no-dev $ rm -rf node_modules

Slide 148

Slide 148 text

$ composer install --optimize-autoloader --no-dev $ rm -rf node_modules $ rm -rf .idea/* $ rm -rf .git/*

Slide 149

Slide 149 text

cp -Rf ../bref-kitty-finder/* . \ cp ../bref-kitty-finder/.env . \ && rm -rf node_modules \ && composer install --optimize-autoloader --no-dev \ && sam package --output-template-file .stack.yaml --s3-bucket bref-kitty-finder-bucket \ && sam deploy --template-file .stack.yaml --stack-name bref-kitty-finder --capabilities CAPABILITY_IAM

Slide 150

Slide 150 text

Successfully created/updated stack - bref-kitty-finder

Slide 151

Slide 151 text

No content

Slide 152

Slide 152 text

CLOUDWATCH HAS YOUR LOGS

Slide 153

Slide 153 text

$ sam logs --name bref-kitty-finder

Slide 154

Slide 154 text

"NOTICE: PHP message: PHP Fatal error: Uncaught ErrorException: file_put_contents(/var/task/ storage/framework/views/ a048d5da056b113da279d23ae842d71b174d2ae6.php): failed to open stream: Read-only file system in /var/task/vendor/laravel/framework/src/ Illuminate/Filesystem/Filesystem.php:122"

Slide 155

Slide 155 text

# ... Globals: Function: Environment: Variables: # Laravel environment variables APP_STORAGE: '/tmp' Resources: WebApplication: # ... app config database bootstrap template.yaml public resources routes storage

Slide 156

Slide 156 text

useStoragePath($_ENV['APP_STORAGE'] ?? $app->storagePath()); app config database bootstrap app.php public resources routes storage

Slide 157

Slide 157 text

useStoragePath($_ENV['APP_STORAGE'] ?? $app->storagePath()); app config database bootstrap public resources routes storage app.php

Slide 158

Slide 158 text

# ... VIEW_COMPILED_PATH=/tmp/storage/framework/views # We cannot store sessions to disk: if you don't need sessions (e.g. API # then use `array`, else store sessions in database or cookies SESSION_DRIVER=array # Logging to stderr allows the logs to end up in Cloudwatch LOG_CHANNEL=stderr # ... app config database bootstrap .env public resources routes storage

Slide 159

Slide 159 text

# ... VIEW_COMPILED_PATH=/tmp/storage/framework/views # We cannot store sessions to disk: if you don't need sessions (e.g. API # then use `array`, else store sessions in database or cookies SESSION_DRIVER=array # Logging to stderr allows the logs to end up in Cloudwatch LOG_CHANNEL=stderr # ... app config database bootstrap .env public resources routes storage

Slide 160

Slide 160 text

# ... VIEW_COMPILED_PATH=/tmp/storage/framework/views # We cannot store sessions to disk: if you don't need sessions (e.g. API # then use `array`, else store sessions in database or cookies SESSION_DRIVER=array # Logging to stderr allows the logs to end up in Cloudwatch LOG_CHANNEL=stderr # ... app config database bootstrap .env public resources routes storage

Slide 161

Slide 161 text

# ... VIEW_COMPILED_PATH=/tmp/storage/framework/views # We cannot store sessions to disk: if you don't need sessions (e.g. API # then use `array`, else store sessions in database or cookies SESSION_DRIVER=array # Logging to stderr allows the logs to end up in Cloudwatch LOG_CHANNEL=stderr # ... app config database bootstrap .env public resources routes storage

Slide 162

Slide 162 text

Slide 163

Slide 163 text

Slide 164

Slide 164 text

No content

Slide 165

Slide 165 text

No content

Slide 166

Slide 166 text

No content

Slide 167

Slide 167 text

php extension=pdo_mysql conf.d php.ini app config database public resources routes storage bootstrap

Slide 168

Slide 168 text

CONNECTING THE DATABASE

Slide 169

Slide 169 text

API Gateway Lambda function Handler PHP Layer Bootstrap

Slide 170

Slide 170 text

API Gateway Lambda function Handler PHP Layer Bootstrap

Slide 171

Slide 171 text

API Gateway API Gateway Data Store Lambda function Handler PHP Layer Bootstrap

Slide 172

Slide 172 text

API Gateway VPC API Gateway Data Store Lambda function Handler PHP Layer Bootstrap

Slide 173

Slide 173 text

# ... Globals: Function: Environment: Variables: # Laravel environment variables APP_STORAGE: '/tmp' DATABASE_URL: ‘mysql://db_user:db_pass@...’ # ... app config database bootstrap template.yaml public resources routes storage

Slide 174

Slide 174 text

No content

Slide 175

Slide 175 text

No content

Slide 176

Slide 176 text

# ... Globals: Function: Environment: Variables: # Laravel environment variables APP_STORAGE: '/tmp' DATABASE_URL: ‘{{resolve:ssm:kittyfinder-database-url:1}}' # ... app config database bootstrap template.yaml public resources routes storage

Slide 177

Slide 177 text

# ... Globals: Function: Environment: Variables: # Laravel environment variables APP_STORAGE: '/tmp' DATABASE_URL: ‘{{resolve:ssm:kittyfinder-database-url:1}}' # ... app config database bootstrap template.yaml public resources routes storage

Slide 178

Slide 178 text

No content

Slide 179

Slide 179 text

CONTENT DELIVERY NETWORK

Slide 180

Slide 180 text

$ composer require arubacao/asset-cdn

Slide 181

Slide 181 text

$ composer require league/flysystem-aws-s3-v3

Slide 182

Slide 182 text

$ aws s3 mb s3://kittyfinder-site-assets

Slide 183

Slide 183 text

app config database bootstrap filesystems.php [ // ... /* |----------------------------------------------------------------- | Assets CDN |----------------------------------------------------------------- | | This is where compiled static assets will be served from */ 'asset-cdn' => [ 'driver' => 's3', 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION', 'us-east-1'), 'bucket' => env(‘AWS_CDN_BUCKET'), ], ], ]; public resources routes storage

Slide 184

Slide 184 text

app config database bootstrap filesystems.php [ // ... /* |----------------------------------------------------------------- | Assets CDN |----------------------------------------------------------------- | | This is where compiled static assets will be served from */ 'asset-cdn' => [ 'driver' => 's3', 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION', 'us-east-1'), 'bucket' => env(‘AWS_CDN_BUCKET'), ], ], ]; public resources routes storage

Slide 185

Slide 185 text

env('USE_CDN', false), 'cdn_url' => 'https://cdn.kittyfinder.net', 'filesystem' => [ 'disk' => 'asset-cdn', 'options' => [ 'ACL' => 'public-read', 'CacheControl' => 'max-age=31536000, public’, // ... ], 'files' => [ 'include' => [ 'paths' => [ 'js', 'css' ], // ... app config database bootstrap asset-cdn.php public resources routes storage

Slide 186

Slide 186 text

# ... Globals: Function: Environment: Variables: # Laravel environment variables APP_STORAGE: '/tmp' DATABASE_URL: ‘{{resolve:ssm:kittyfinder-database-url:1}}' USE_CDN: true # ... app config database bootstrap template.yaml public resources routes storage

Slide 187

Slide 187 text

app config database bootstrap public resources routes storage views layouts app.blade.php

Slide 188

Slide 188 text

app config database bootstrap public resources routes storage views layouts app.blade.php

Slide 189

Slide 189 text

No content

Slide 190

Slide 190 text

BRINGING YOUR OWN DOMAIN NAME

Slide 191

Slide 191 text

No content

Slide 192

Slide 192 text

https://lzfret3054.execute-api.eu-west-1.amazonaws.com/Prod/

Slide 193

Slide 193 text

https://lzfret3054.execute-api.eu-west-1.amazonaws.com/Prod/

Slide 194

Slide 194 text

No content

Slide 195

Slide 195 text

No content

Slide 196

Slide 196 text

No content

Slide 197

Slide 197 text

No content

Slide 198

Slide 198 text

No content

Slide 199

Slide 199 text

No content

Slide 200

Slide 200 text

No content

Slide 201

Slide 201 text

env('USE_CDN', false), 'cdn_url' => 'https://cdn.kittyfinder.net', 'filesystem' => [ 'disk' => 'asset-cdn', 'options' => [ 'ACL' => 'public-read', 'CacheControl' => 'max-age=31536000, public’, // ... ], 'files' => [ 'include' => [ 'paths' => [ 'js', 'css', 'vendor' ], // ... app config database bootstrap asset-cdn.php public resources routes storage

Slide 202

Slide 202 text

app config database bootstrap public resources routes storage views vendor nova layout.blade.php

Slide 203

Slide 203 text

No content

Slide 204

Slide 204 text

No content

Slide 205

Slide 205 text

app->environment('production')) { URL::forceScheme('https'); } } } app config database Providers public resources routes storage bootstrap AppServiceProvider.php

Slide 206

Slide 206 text

# ... Globals: Function: Environment: Variables: # Laravel environment variables APP_STORAGE: '/tmp' DATABASE_URL: ‘{{resolve:ssm:kittyfinder-database-url:1}}' USE_CDN: true APP_ENV: production # ... app config database bootstrap template.yaml public resources routes storage

Slide 207

Slide 207 text

No content

Slide 208

Slide 208 text

API Gateway Data Store Lambda function Handler PHP Layer Bootstrap

Slide 209

Slide 209 text

Lambda function Handler PHP Layer Bootstrap API Gateway Data Store Sessions

Slide 210

Slide 210 text

Lambda function Handler PHP Layer Bootstrap API Gateway Data Store Sessions

Slide 211

Slide 211 text

Lambda function Handler PHP Layer Bootstrap Handler PHP Layer Bootstrap API Gateway Data Store Sessions Sessions

Slide 212

Slide 212 text

API Gateway Data Store Sessions Sessions Handler PHP Layer Bootstrap Lambda function Handler PHP Layer Bootstrap

Slide 213

Slide 213 text

$ php artisan session:table

Slide 214

Slide 214 text

# ... Globals: Function: Environment: Variables: # Laravel environment variables APP_STORAGE: '/tmp' DATABASE_URL: ‘{{resolve:ssm:kittyfinder-database-url:1}}' USE_CDN: true APP_ENV: production SESSION_DRIVER: database # ... app config database bootstrap template.yaml public resources routes storage

Slide 215

Slide 215 text

$ php artisan migrate

Slide 216

Slide 216 text

CONSOLE COMMANDS

Slide 217

Slide 217 text

Resources: WebApplication: # ... Console: Type: AWS::Serverless::Function Properties: FunctionName: 'bref-kittyfinder-console' CodeUri: . Handler: artisan Runtime: provided Layers: # PHP runtime - 'arn:aws:lambda:eu-west-1:209497400698:layer:php-73:1' # Console layer - 'arn:aws:lambda:eu-west-1:209497400698:layer:console:1' app config database bootstrap template.yaml public resources routes storage

Slide 218

Slide 218 text

app config database bootstrap Resources: WebApplication: # ... Console: Type: AWS::Serverless::Function Properties: FunctionName: 'bref-kittyfinder-console' CodeUri: . Handler: artisan Runtime: provided Layers: # PHP runtime - 'arn:aws:lambda:eu-west-1:209497400698:layer:php-73:1' # Console layer - 'arn:aws:lambda:eu-west-1:209497400698:layer:console:1' template.yaml public resources routes storage

Slide 219

Slide 219 text

app config database bootstrap template.yaml public resources routes storage Resources: WebApplication: # ... Console: Type: AWS::Serverless::Function Properties: FunctionName: 'bref-kittyfinder-console' CodeUri: . Handler: artisan Runtime: provided Layers: # PHP runtime - 'arn:aws:lambda:eu-west-1:209497400698:layer:php-73:1' # Console layer - 'arn:aws:lambda:eu-west-1:209497400698:layer:console:1'

Slide 220

Slide 220 text

$ php vendor/bin/bref cli bref-kittyfinder-console -- \ migrate --force

Slide 221

Slide 221 text

$ php vendor/bin/bref cli bref-kittyfinder-console -- \ migrate --force

Slide 222

Slide 222 text

$ php vendor/bin/bref cli bref-kittyfinder-console -- \ migrate --force

Slide 223

Slide 223 text

Migrating: 2019_06_06_163613_create_sessions_table Migrated: 2019_06_06_163613_create_sessions_table

Slide 224

Slide 224 text

No content

Slide 225

Slide 225 text

API Gateway Data Store Lambda function Handler PHP Layer Bootstrap

Slide 226

Slide 226 text

API Gateway Data Store Lambda function Handler PHP Layer Bootstrap

Slide 227

Slide 227 text

API Gateway Data Store File Store Lambda function Handler PHP Layer Bootstrap

Slide 228

Slide 228 text

# ... AWS_BUCKET=kittyfinder-uploads # ... app config database bootstrap .env public resources routes storage

Slide 229

Slide 229 text

/** * Get the fields displayed by the resource. * * @param \Illuminate\Http\Request $request * @return array */ public function fields(Request $request) { return [ // ... Avatar::make('Avatar', 'profile_pic')->disk('s3'), // ... ]; } app config database bootstrap Nova Kitty.php User.php public resources routes storage

Slide 230

Slide 230 text

app config database bootstrap Nova Kitty.php User.php /** * Get the fields displayed by the resource. * * @param \Illuminate\Http\Request $request * @return array */ public function fields(Request $request) { return [ // ... Avatar::make('Avatar')->disk('s3'), // ... ]; } public resources routes storage

Slide 231

Slide 231 text

No content

Slide 232

Slide 232 text

No content

Slide 233

Slide 233 text

app config database bootstrap public resources routes storage views home.blade.php

Slide 234

Slide 234 text

app config database bootstrap public resources views home.blade.php routes storage

Slide 235

Slide 235 text

No content

Slide 236

Slide 236 text

UPLOADING NEW IMAGES OF KITTIES

Slide 237

Slide 237 text

Pre-signed URLs

Slide 238

Slide 238 text

Client Server Amazon S3

Slide 239

Slide 239 text

Client Server Amazon S3 Get Pre-Signed URL

Slide 240

Slide 240 text

Client Server Amazon S3 Get Pre-Signed URL Get Pre-Signed URL

Slide 241

Slide 241 text

Client Server Amazon S3 Get Pre-Signed URL Get Pre-Signed URL One-time use URL

Slide 242

Slide 242 text

Client Server Amazon S3 Get Pre-Signed URL Get Pre-Signed URL One-time use URL One-time use URL

Slide 243

Slide 243 text

Client Server Amazon S3 Get Pre-Signed URL Get Pre-Signed URL One-time use URL One-time use URL PUT file to One-time use URL 200 OK

Slide 244

Slide 244 text

Client Server Amazon S3 Get Pre-Signed URL Get Pre-Signed URL One-time use URL One-time use URL PUT file to One-time use URL 200 OK Update record

Slide 245

Slide 245 text

Client Server Amazon S3 Get Pre-Signed URL Get Pre-Signed URL One-time use URL One-time use URL PUT file to One-time use URL 200 OK Update record Delete previous file

Slide 246

Slide 246 text

$ php artisan nova:field kittyfinder/s3-filepicker

Slide 247

Slide 247 text

/** * Get the fields displayed by the resource. * * @param \Illuminate\Http\Request $request * @return array */ public function fields(Request $request) { return [ ID::make()->sortable(), Text::make('Name') ->sortable() ->rules('required', 'max:255'), Avatar::make(‘Avatar', 'profile_pic'), Text::make('Favourite Toy'), Trix::make('Bio'), BelongsTo::make('User') ]; } app config database bootstrap public resources routes storage Nova Kitty.php User.php nova-components

Slide 248

Slide 248 text

/** * Get the fields displayed by the resource. * * @param \Illuminate\Http\Request $request * @return array */ public function fields(Request $request) { return [ ID::make()->sortable(), Text::make('Name') ->sortable() ->rules('required', 'max:255'), S3FilePicker::make('Image', 'profile_pic'), Text::make('Favourite Toy'), Trix::make('Bio'), BelongsTo::make('User') ]; } app config database bootstrap public resources routes storage Nova Kitty.php User.php nova-components

Slide 249

Slide 249 text

class ApiController extends Controller { /** * Get a pre-signed key * * @return Response */ public function preSignedUrl(Request $request): Response { if (null === $request->user()) { throw new UnauthorizedHttpException("You're not allowed her } return response()->json([ 'url' => $this->generatePreSignedUrl($request), 'filename' => $this->getNewFilename($request), 'path' => 'https://' . env('AWS_BUCKET') . '.s3-eu-west-1.amazonaws.com/' . $this->getNewFilename($request) ]); } // ... app config database bootstrap public resources routes storage Http ApiController.php Controllers nova-components

Slide 250

Slide 250 text

class ApiController extends Controller { /** * Get a pre-signed key * * @return Response */ public function preSignedUrl(Request $request): Response { if (null === $request->user()) { throw new UnauthorizedHttpException("You're not allowed her } return response()->json([ 'url' => $this->generatePreSignedUrl($request), 'filename' => $this->getNewFilename($request), 'path' => 'https://' . env('AWS_BUCKET') . '.s3-eu-west-1.amazonaws.com/' . $this->getNewFilename($request) ]); } // ... app config database bootstrap public resources routes storage Http ApiController.php Controllers nova-components

Slide 251

Slide 251 text

class ApiController extends Controller { // ... private function generatePreSignedUrl(Request $request): string { $s3Client = new S3Client([ 'region' => env('AWS_DEFAULT_REGION'), 'version' => 'latest', ]); $cmd = $s3Client->getCommand('PutObject', [ 'Bucket' => env('AWS_BUCKET'), 'Key' => $this->getNewFilename($request), 'ContentType' => $request->get('filetype') ]); $response = $s3Client->createPresignedRequest( $cmd, '+20 minutes’ ); return (string)$response->getUri(); } // ... app config database bootstrap public resources routes storage Http ApiController.php Controllers nova-components

Slide 252

Slide 252 text

class ApiController extends Controller { // ... private function generatePreSignedUrl(Request $request): string { $s3Client = new S3Client([ 'region' => env('AWS_DEFAULT_REGION'), 'version' => 'latest', ]); $cmd = $s3Client->getCommand('PutObject', [ 'Bucket' => env('AWS_BUCKET'), 'Key' => $this->getNewFilename($request), 'ContentType' => $request->get('filetype') ]); $response = $s3Client->createPresignedRequest( $cmd, '+20 minutes’ ); return (string)$response->getUri(); } // ... app config database bootstrap public resources routes storage Http ApiController.php Controllers nova-components

Slide 253

Slide 253 text

class ApiController extends Controller { // ... private function generatePreSignedUrl(Request $request): string { $s3Client = new S3Client([ 'region' => env('AWS_DEFAULT_REGION'), 'version' => 'latest', ]); $cmd = $s3Client->getCommand('PutObject', [ 'Bucket' => env('AWS_BUCKET'), 'Key' => $this->getNewFilename($request), 'ContentType' => $request->get('filetype') ]); $response = $s3Client->createPresignedRequest( $cmd, '+20 minutes’ ); return (string)$response->getUri(); } // ... app config database bootstrap public resources routes storage Http ApiController.php Controllers nova-components

Slide 254

Slide 254 text

class ApiController extends Controller { // ... private function generatePreSignedUrl(Request $request): string { $s3Client = new S3Client([ 'region' => env('AWS_DEFAULT_REGION'), 'version' => 'latest', ]); $cmd = $s3Client->getCommand('PutObject', [ 'Bucket' => env('AWS_BUCKET'), 'Key' => $this->getNewFilename($request), 'ContentType' => $request->get('filetype') ]); $response = $s3Client->createPresignedRequest( $cmd, '+20 minutes’ ); return (string)$response->getUri(); } // ... app config database bootstrap public resources routes storage Http ApiController.php Controllers nova-components

Slide 255

Slide 255 text

// ... app config database nova-components bootstrap public resources routes storage resources/js/components S3FilePicker FormField.vue

Slide 256

Slide 256 text

app config database nova-components bootstrap public resources routes storage resources/js/components S3FilePicker FormField.vue /** * Update the field's internal value. */ async fileChange(event) { let path = event.target.value let fileName = path.match(/[^\\/]*$/)[0] this.fileName = fileName this.file = this.$refs.fileField.files[0] await axios.get('/nova/presignedurl?' + 'filename=' + this.fileName + '&filetype=' + this.file.type ).then(async response => { let options = { headers: { "Content-Type": this.file.type } }; await axios.put(response.data.url, this.file, options).then(result document.getElementById(‘previewImage') .setAttribute(‘src', response.data.path); this.value = response.data.filename }); }) }

Slide 257

Slide 257 text

app config database nova-components bootstrap public resources routes storage resources/js/components S3FilePicker FormField.vue /** * Update the field's internal value. */ async fileChange(event) { let path = event.target.value let fileName = path.match(/[^\\/]*$/)[0] this.fileName = fileName this.file = this.$refs.fileField.files[0] await axios.get('/nova/presignedurl?' + 'filename=' + this.fileName + '&filetype=' + this.file.type ).then(async response => { let options = { headers: { "Content-Type": this.file.type } }; await axios.put(response.data.url, this.file, options).then(result document.getElementById(‘previewImage') .setAttribute(‘src', response.data.path); this.value = response.data.filename }); }) }

Slide 258

Slide 258 text

app config database nova-components bootstrap public resources routes storage resources/js/components S3FilePicker FormField.vue /** * Update the field's internal value. */ async fileChange(event) { let path = event.target.value let fileName = path.match(/[^\\/]*$/)[0] this.fileName = fileName this.file = this.$refs.fileField.files[0] await axios.get('/nova/presignedurl?' + 'filename=' + this.fileName + '&filetype=' + this.file.type ).then(async response => { let options = { headers: { "Content-Type": this.file.type } }; await axios.put(response.data.url, this.file, options).then(result document.getElementById(‘previewImage') .setAttribute(‘src', response.data.path); this.value = response.data.filename }); }) }

Slide 259

Slide 259 text

No content

Slide 260

Slide 260 text

No content

Slide 261

Slide 261 text

No content

Slide 262

Slide 262 text

No content

Slide 263

Slide 263 text

No content

Slide 264

Slide 264 text

No content

Slide 265

Slide 265 text

No content

Slide 266

Slide 266 text

$ composer require nealio82/kittyfinder-s3filepicker

Slide 267

Slide 267 text

No content

Slide 268

Slide 268 text

RECAP

Slide 269

Slide 269 text

Added Bref to an existing site

Slide 270

Slide 270 text

Fixed assets by moving them to a CDN

Slide 271

Slide 271 text

Centralised session storage

Slide 272

Slide 272 text

Added Bref’s console layer in order to run artisan commands

Slide 273

Slide 273 text

Used pre-signed URLs to send file uploads directly to the CDN

Slide 274

Slide 274 text

No content

Slide 275

Slide 275 text

PERFORMANCE

Slide 276

Slide 276 text

No content

Slide 277

Slide 277 text

VPC API Gateway Data Store Lambda function Handler PHP Layer Bootstrap

Slide 278

Slide 278 text

source: https://medium.freecodecamp.org/lambda-vpc-cold-starts-a-latency-killer-5408323278dd

Slide 279

Slide 279 text

source: https://medium.freecodecamp.org/lambda-vpc-cold-starts-a-latency-killer-5408323278dd

Slide 280

Slide 280 text

source: https://medium.freecodecamp.org/lambda-vpc-cold-starts-a-latency-killer-5408323278dd

Slide 281

Slide 281 text

No content

Slide 282

Slide 282 text

SAVINGS

Slide 283

Slide 283 text

No content

Slide 284

Slide 284 text

DOES IT SCALE?

Slide 285

Slide 285 text

Custom runtime based upon Bref’s PHP-FPM layer

Slide 286

Slide 286 text

Uses Phalcon PHP framework

Slide 287

Slide 287 text

Added AWS application load balancer in-front of Lambda

Slide 288

Slide 288 text

Uses heavy caching - responses served from Cloudfront where possible

Slide 289

Slide 289 text

40 million requests per day

Slide 290

Slide 290 text

90 ms average response time

Slide 291

Slide 291 text

2,500 concurrent invocations at peak times

Slide 292

Slide 292 text

25% cost saving compared to EC2*

Slide 293

Slide 293 text

GOING FURTHER

Slide 294

Slide 294 text

QUEUES & EVENTS

Slide 295

Slide 295 text

What kind of lambda do you want to create? [0] PHP function [1] HTTP application [2] Console application >

Slide 296

Slide 296 text

What kind of lambda do you want to create? [0] PHP function [1] HTTP application [2] Console application > 0

Slide 297

Slide 297 text

https://github.com/stechstudio/ laravel-bref-bridge

Slide 298

Slide 298 text

ROLLING YOUR OWN

Slide 299

Slide 299 text

https://github.com/stechstudio/ bref-extensions

Slide 300

Slide 300 text

No content

Slide 301

Slide 301 text

serverless framework

Slide 302

Slide 302 text

bref/bref

Slide 303

Slide 303 text

JOIN US

Slide 304

Slide 304 text

WE ARE HIRING!

Slide 305

Slide 305 text

https://bref.sh/docs/community

Slide 306

Slide 306 text

@nealio82 https://bref.sh/docs/community