I am using brief https://bref.sh/docs/frameworks/laravel to deploy in AWS Lambda my Laravel application (I build only one api without a frontend)
I am using in my composer.json the following versions
"laravel/framework": "10.22.0",
"bref/bref": "^2.1",
"bref/laravel-bridge": "^2.1",
My lambda dependencies are the following:
My serverless.yml
file looks like:
service: api
provider:
name: aws
region: eu-central-1
logRetentionInDays: 7
environment:
LARAVEL_STORAGE_PATH: /tmp/storage
# rest of env are loaded via the plugin and passed fine to the lambda config. No worries here
custom:
serviceName: 'api' # the name of this service in AWS SSM store
package:
# Files and directories to exclude from deployment
patterns:
- '!node_modules/**'
- '!public/storage'
- '!resources/assets/**'
- '!storage/**'
- '!tests/**'
- '!.env'
- '!build/**'
- '!dev-tools/**'
- '!ext-config/**'
- '!queries/**'
functions:
# This function runs the Laravel website/API
web:
handler: public/index.php
vpc:
securityGroupIds:
- ${ssm:/${self:provider.stage}/${self:provider.region}/applications/${self:custom.serviceName}/lambda_security_group}
subnetIds: ${ssm:/${self:provider.stage}/${self:provider.region}/infra/vpc_private_subnets}
runtime: php-82-fpm
timeout: 28 # in seconds (API Gateway has a timeout of 29 seconds)
events:
- httpApi: '*'
# This function lets us run artisan commands in Lambda
artisan:
handler: artisan
runtime: php-82-console
timeout: 720 # in seconds
events:
- schedule:
rate: rate(1 minute)
input: '"schedule:run"'
plugins:
- ./vendor/bref/bref # lambda layer allowing laravel to work within aws serverless
- serverless-lift # provides aws functionalities for creating the sqs queue
- serverless-dotenv-plugin # loads .env file into serverless variables
I can deploy totally fine with
php artisan config:clear
serverless deploy --stage development
After the lambda is available through AWS API Gateway I can
// 20230922174906
// https://6888rrxjik.execute-api.eu-central-1.amazonaws.com/api
{
"message": "Internal Server Error"
}
What I troubleshooted so far
I looked into cloudwatch logs and do see that the lambda times-out
I studied brief docs related to connections to databases and reasons for timeout.
I could not see any reason of the failure because as mentioned I have same vpc_id and same subnets for both the database and redis, also lambda has same ones. In addition my database is publicly accessible.
I also checked my AWS configurations and I have one Internet gateway
already connected to my VPC
which is used in both redis,rds,lambda. I notice I don't have any NAT Gateway
in my AWS account.
In addition I cannot see more detailed log in the stack trace to understand where this time is spent and what happens.
Any idea how to troubleshoot further and achieve the goal of having the lambda functional ?
UPDATE:
I got it working by doing the following
- ./vendor/bref/extra-php-extensions # for enabling php extensions via bref
runtime: php-82-fpm
layers:
- ${bref-extra:redis-php-82}
If I remove this I get again timeout. Probably because the redis related code is not available
LARAVEL_STORAGE_PATH=/tmp/storage
as you cannot write elsewhere in the lambda.After those changes I can access the api that was timing out and I do confirm that
I am testing if I can make api calls to other public apis/domains now when my lambda is in public subnets
The conclusion is:
So for the lambda to have internet access you need one of the following solutions:
PS: I am not sure if having the lambda in public subnet has any seccurity implication. The lambda itself has no api but is served through the aws api gateway