AWS Lambda & Symfony Bref - Deploying Symfony 4 website - php

I would like deploy my Symfony 4 website thanks to AWS Lambda and Symfony Bref, I followed this docs :
https://bref.sh/docs/frameworks/symfony.html
https://www.vertcitron.fr/node-lambda-serverless/
but I have this error :
My serverless.yaml file :
service: bref-symfony
package:
exclude:
- node_modules/**
- venv/**
provider:
name: aws
region: eu-west-3
runtime: provided
environment:
# Symfony environment variables
APP_ENV: prod
plugins:
- ./vendor/bref/bref
functions:
website:
handler: public/index.php
timeout: 30 # in seconds (API Gateway has a timeout of 30 seconds)
layers:
- ${bref:layer.php-73-fpm}
events:
- http: 'ANY /'
- http: 'ANY /{proxy+}'
console:
handler: bin/console
timeout: 120 # in seconds
layers:
- ${bref:layer.php-73} # PHP
- ${bref:layer.console} # The "console" layer
What is the problem please ?

It looks like you need to cut back on what is included in your app image. Make sure you are running prod-only dependencies on composer:
composer inst --no-dev -o
You also can add something like this to your serverless.yaml to only include the files you need to run your Symfony app:
package:
exclude:
- '*'
- '**'
include:
- 'bin/**'
- 'config/**'
- 'public/index.php'
- 'src/**'
- 'var/cache/prod/**'
- 'translations/**'
- 'vendor/**'
- '.env'

Related

How to access docker image on AzureDevops?

I want to create a Docker image of a Laravel Application on Azure Devops and deploy it to a GCR, I commited a project created with laravel 5.5 that works with Docker on local. I thought that if I created a Build pipeline on Azure Devops It would take docker-compose.yml but it failed, so i tried using azure-pipeline.yml and i was able to create an image (still don't know where is it placed). I would like to know how can I access docker image the way i do in local using a command line. is that possible?
this is the yaml file
trigger:
- my_build
resources:
- repo: self
variables:
tag: '$(Build.BuildId)'
GOOGLE_PROYECT_ID: "deft-clarity-myGCPry"
GOOGLE_PROYECT_NAME: "Dashboard LW"
GOOGLE_APPLICATION_CREDENTIALS: '$(admin_gcr_myGCPry.secureFilePath)'
GOOGLE_CLOUD_KEYFILE_JSON: '$(admin_gcr_myGCPry.secureFilePath)'
stages:
- stage: Build
displayName: Build mypr img
jobs:
- job: Build_My_Pry_Image
displayName: Job Build Dashboard Image
pool:
vmImage: ubuntu-latest
steps:
- task: Docker#2
displayName: Build an image
inputs:
command: build
dockerfile: '$(Build.SourcesDirectory)/.docker/Dockerfile'
tags: |
$(tag)
- task: DownloadSecureFile#1
name: admin_gcr_361712
displayName: Download Service Account
inputs:
secureFile: 'admin-gcr-mygcpry.json'
- bash: 'echo $GOOGLE_PROYECT_ID'
- bash: 'gcloud --version'
name: "version"
- bash: 'gcloud auth activate-service-account admin-gcr#deft-clarity-myGCPry.iam.gserviceaccount.com --key-file=$GOOGLE_APPLICATION_CREDENTIALS'
name: "Activate_Service"
- bash: 'gcloud config set proyect deft-clarity-myCGPry'
- bash: 'gcloud config list'
- bash: 'gcloud compute network list' ```

Lambda trigger a function itself

Hello I have a laravel app with serverless architecture. I'm getting an error:
cURL error 28: Failed to connect to fnhxdorrd22l.execute-api.ap-southeast-1.amazonaws.com port 443 after 7502 ms: Connection timed out (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for https://fnhxdorrdl22.execute-api.ap-southeast-1.amazonaws.com/oauth/token
Is there any configuration need for this or any inbound rules in order to call a function inside a function? BTW, it's working if it's a simple call or request without any call or trigger to other route or third parties.
Serverless.yml
service: laravel
provider:
name: aws
# The AWS region in which to deploy (us-east-1 is the default)
region: ap-southeast-1
# The stage of the application, e.g. dev, production, staging… ('dev' is the default)
stage: dev
profile: serverless
runtime: provided.al2
lambdaHashingVersion: 20201222321
package:
# Directories to exclude from deployment
patterns:
- '!node_modules/**'
- '!public/storage'
- '!resources/assets/**'
- '!storage/**'
- '!tests/**'
- 'storage/oauth-private.key'
- 'storage/oauth-public.key'
functions:
# This function runs the Laravel website/API
web:
handler: public/index.php
timeout: 28 # in seconds (API Gateway has a timeout of 29 seconds)
vpc:
securityGroupIds:
- sg-042d6942052649ad59b0bc0
subnetIds:
- subnet-2c1464319824244
- subnet-474851e914424e4
- subnet-4424429f48129d7
layers:
- ${bref:layer.php-80-fpm}
events:
- httpApi: '*'
# This function lets us run artisan commands in Lambda
artisan:
handler: artisan
timeout: 120 # in seconds
layers:
- ${bref:layer.php-80} # PHP
- ${bref:layer.console} # The "console" layer
A Lambda function configured to run in a VPC does not get a public IP address ever (regardless of the VPC public IP settings). The API Gateway URL is a public URL on the Internet. That API Gateway URL doesn't exist inside the VPC. In order for the Lambda function to make a connection to that URL the function has to be configured to run in a private VPC subnet that has a route to a NAT Gateway.

How to install GD Library? Laravel at AWS Lambda with Bref

When using Intervention\Image in laravel on lambda
The following error has occurred.
By the way, it works in the local environment.
I have to add gd.
[2021-08-17 10:37:18] DEV.ERROR: GD Library extension not available with this PHP installation.
{"exception":"[object] (Intervention\Image\Exception\NotSupportedException(code: 0):
GD Library extension not available with this PHP installation.
at /var/task/vendor/intervention/image/src/Intervention/Image/Gd/Driver.php:19)
What I looked up
https://bref.sh/docs/environment/php.html#extensions
https://github.com/brefphp/extra-php-extensions
Deployment method
We are deploying to lambda using the sls command.
sls deploy --stage dev
Based on the investigation, the following is implemented
composer require bref/extra-php-extensions
Added below
serverless.yml
plugins:
- ./vendor/bref/bref
- ./vendor/bref/extra-php-extensions #add
functions:
# This function runs the Laravel website/API
web:
image:
name: laravel
events:
- httpApi: '*'
# This function lets us run artisan commands in Lambda
artisan:
handler: artisan
timeout: 120 # in seconds
layers:
- ${bref:layer.php-80}
- ${bref:layer.console}
- ${bref-extra:gd-php-80} #add
Even if the above settings are added and deployed, they are not updated. .. why?
enviroment
Laravel Framework 8.33.1
PHP 7.4.3
bref
serverless
I'm sorry if English is strange.
Put the layers into web "tag".
plugins:
- ./vendor/bref/bref
- ./vendor/bref/extra-php-extensions #add
functions:
# This function runs the Laravel website/API
web:
image:
name: laravel
layers:
- ${bref-extra:gd-php-80} #add
events:
- httpApi: '*'
# This function lets us run artisan commands in Lambda
artisan:
handler: artisan
timeout: 120 # in seconds
layers:
- ${bref:layer.php-80}
- ${bref:layer.console}
Then add the folder php/conf.d inside put a file with extension .ini. For example php.ini. In it just put:
extension=gd

Internal server error after download on s3 bucket from aws lambda , PHP

I created aws lambda function based on laravel framework using bref. Link: https://bref.sh
this is my code to download using media library spartie
$mediaItem = Media::where('model_id',$id)->where('model_type','App\MODEL')->first();
return $mediaItem;
My configuration is
AWS_ACCESS_KEY_ID=XXXXXXXXXXXXXXXXXX
AWS_SECRET_ACCESS_KEY=XXXXX+XXXXXXXX
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=my-bucket
When I download file locally all work perfectly, but when I try to download on s3 in lambda function I got error:
{ "message" : "Internal server error" }
I use this package aws:
league/flysystem-aws-s3-v3 ~1.0
my serverless.yaml
service: serverless-api
provider:
name: aws
region: eu-central-1
runtime: provided
environment:
APP_ENV: production
iamRoleStatements:
- Effect: Allow
Action:
- s3:*
Resource: 'arn:aws:s3:::bucket/*'
plugins:
- ./vendor/bref/bref
package:
exclude:
- node_modules/**
- public/storage
- storage/**
- tests/**
- .env
functions:
website:
handler: public/index.php
timeout: 28 # in seconds (API Gateway has a timeout of 29 seconds)
layers:
- arn:aws:lambda:eu-central-1:209497400698:layer:php-72-fpm:23
events:
- http: 'ANY /'
- http: 'ANY /{proxy+}'
artisan:
handler: artisan
timeout: 120 # in seconds
layers:
- arn:aws:lambda:eu-central-1:209497400698:layer:php-72:22
- arn:aws:lambda:eu-central-1:209497400698:layer:console:22
also i assign role for s3 , But I have no idea how it's the problem .
thank you .

Sonata block http cache handler sonata.cache.noop not defined

I'm trying to set up sonata block cache but when I setup the http_cache handler I'm getting an exception even when the service seems to be there.
My current test setup is the following
sonata_cache:
caches:
predis:
servers:
- {host: %redis_host%, port: %redis_port%, database: 2}
apc:
token: %secret% # token used to clear the related cache
prefix: %domain% # prefix to ensure there is no clash between instances
# servers:
# - { domain: %domain%, ip: 127.0.0.1, port: 80 }
timeout:
RCV: { sec: 5, usec: 0 }
SND: { sec: 5, usec: 0 }
sonata_block:
http_cache:
# handler: sonata.cache.noop
listener: true
I can see the multiple services that I enabled on the sonata_cache.caches
$ app/console debug:container sonata.cache.noop
[container] Information for service sonata.cache.noop
Service Id sonata.cache.noop
Class Sonata\Cache\Adapter\Cache\NoopCache
Tags
- sonata.cache ()
[...]
If I change the sonata_block setup to:
sonata_block:
http_cache:
handler: sonata.cache.noop
listener: true
I got the following exception
InvalidArgumentException in ContainerBuilder.php line 796 "The service
definition "sonata.cache.noop" does not exist."
Does anyone had a similar issue?
Try to replace :
handler: sonata.cache.noop
to :
handler: sonata.block.cache.handler.noop

Categories