Don’t forget to change the region name in the above commands!
Once that’s done, you’ll see the layers in the ‘Lambda’ ‘Layers’ page. Copy the ARN from there into your serverless .yml file, and away you go!
Don’t forget to enable the extension – this is done by creating a ‘php.ini’ file at /php/conf.d/php.ini (in your code-base), with the following;
If you want to make sure it works, include the following in your PHP file and it’ll output a full list of info about the PHP environment, including a section on imagick if it’s been enabled & setup properly.
The aim here is to use AWS Cognito to authenticate users on your Symfony app, using oAuth2 so all the auth happens externally on AWS Cognito.
I’m not storing user data locally with this — it just makes sure that they’re valid users. Groups functionality would need to be added separately if required (it’s referenced in the MyBuilder article and has instructions on how groups can be obtained).
I’ve used the knpuniversity bundle as I tried to get HWI OAuth package to work but there wasn’t any providers already written up to support Cognito.
Composer packages used;
So – to get it going, follow the KNPUniversity instructions on their git-hub account.
From there, use the following for the Authenticator class;
class CognitoAuthenticator extends SocialAuthenticator
public function __construct(ClientRegistry $clientRegistry, RouterInterface $router)
$this->clientRegistry = $clientRegistry;
$this->router = $router;
public function supports(Request $request)
// continue ONLY if the current ROUTE matches the check ROUTE
return $request->attributes->get('_route') === 'connect_cognito_check';
public function getCredentials(Request $request)
// this method is only called if supports() returns true
* @return OAuth2Client
private function getClient()
public function getUser($credentials, UserProviderInterface $userProvider)
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
$targetUrl = $this->router->generate('app_default_index');
return new RedirectResponse($targetUrl);
// or, on success, let the request continue to be handled by the controller
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
$message = strtr($exception->getMessageKey(), $exception->getMessageData());
return new Response($message, Response::HTTP_FORBIDDEN);
* Called when authentication is needed, but it's not sent.
* This redirects to the 'login'.
* @param Request $request
* @param AuthenticationException|null $authException
* @return RedirectResponse
public function start(Request $request, AuthenticationException $authException = null)
return new RedirectResponse(
// might be the site, where users choose their oauth provider
My ‘SecurityCognitoController’ class looks like this;
class SecurityCognitoController extends AbstractController
* Link to this controller to start the "connect" process
* @Route("/security/connect-cognito", name="connect_cognito_start")
public function connectAction(ClientRegistry $clientRegistry)
// will redirect to AWS Cognito!
->getClient('cognito') // key used in config/packages/knpu_oauth2_client.yaml
* After going to Facebook, you're redirected back here
* because this is the "redirect_route" you configured
* in config/packages/knpu_oauth2_client.yaml
* @Route("/security/cognito/check", name="connect_cognito_check")
public function connectCheckAction(Request $request, ClientRegistry $clientRegistry)
// ** if you want to *authenticate* the user, then
// leave this method blank and create a Guard authenticator
And the following config was used in the ‘pnpu_oauth2_client.yaml’ file;
# configure your clients as described here: https://github.com/knpuniversity/oauth2-client-bundle#configuration
client_id: '<your client id here>'
client_secret: '<your client secret here>'
region: <your region here>
cognitoDomain: <your cognito domain here>
A few things are above;
client id; This is found in the ‘App integration’ -> ‘App client settings’ page
client_secret: This is in the ‘General settings’ -> ‘App clients’ page, and generated when the ‘app client’ is added
region: the region your Cognito user-pool is in
scope: I’ve just used email, but if you want you can expand it to capture other information as well.
redirect_route: In my case I set it to ‘connect_cognito_check’ … this endpoint is used to receive the ‘OK’ from AWS Cognito that your user has been authenticated and pass back a code which internally is used to retrieve the actual account info of the person which was authenticated.
In AWS Cognito, in your ‘App client’ you’ve setup, make sure you have the following settings;
In AWS Cognito, in your ‘App client’ you’ve setup, make sure you have the following settings;
Thanks to the following sites which helped get this far;
Warning: This is still a work in progress — i’ve tried to get as far as I could to but ran out of time.
The current error is the following, which I think is due to the GD extension using config from a later version of PHP, rather than the version Bref uses;
PHP Warning: PHP Startup: Unable to load dynamic library 'gd' (tried: /opt/bref/lib/php/extensions/no-debug-zts-20180731/gd (/opt/bref/lib/php/extensions/no-debug-zts-20180731/gd: cannot open shared object file: No such file or directory), /opt/bref/lib/php/extensions/no-debug-zts-20180731/gd.so (/opt/bref/lib/php/extensions/no-debug-zts-20180731/gd.so: undefined symbol: executor_globals)) in Unknown on line 0
Basically the following needs to be done to get GD added as a layer (and thus, extension) available to PHP, within AWS Lambda (using PHP Bref).
Compile GD as a ‘bundle’ (including all required libraries as part of it).
Zip up the gd.so bundle file, along with any other libraries it requires This is unzipped in the /opt dir in lambda, so the directory structure in the Zip file should be setup in whatever way makes it all work
Publish the zip file as a layer, which is then included after the bref layer in lambda
I followed this guide for the commands;
Download and compile the following required libraries;
libwebp (GD compiles, but then complains the library isn’t there when you try to use the GD PHP extension if you don’t include this library) libxpm (GD doesn’t seem to compile without it, and it’s not included using the AWS AMI by default)
Follow the instructions on each of their respective websites to build, and run the ‘./configure’ command with ‘–with-prefix=/opt/…’ (replacing ‘…’ with the name of the library)
Download PHP from the php.net site, (get the same version of PHP as Bref uses).
Unzip, and go into the /modules/gd dir (in your unzipped PHP dir)
Run the following to generate a ./configure command and make files
The following configures and compiles the GD extension, followed by ‘make test’ to check the extension is working.
It references the ‘libwebp’ and ‘libxpm’ directories which are already compiled and output in the /opt/… dir (using ./configure –with-prefix=/opt/.. command)
sudo ./configure --with-php-config=/usr/bin/php-config --with-gd --with-freetype-dir=/usr --with-jpeg-dir=/usr --with-xpm-dir=/opt/libxpm/ --with-png-dir=/usr --with-webp-dir=/opt/libwebp/
Zip up the gd bundle
For me, I include the following in the zip file;
libwebp (GD compiles, but then complains the library isn’t there when you try to use the GD PHP extension if you don’t include this library)
libxpm (GD doesn’t seem to compile without it, and it’s not included using the AWS AMI by default)
bref/lib/php/extensions/no-debug-zts-20180731/gd.so This is my compiled GD extension, in this directory (to coincide with the extensions dir bref uses — found using phpinfo(); )
The following command creates a ‘gd.zip’ file with the above libraries & files;
For this example, we’ll use ap-southeast-2 as the region (as all my scripts are written using it)
IMPORTANT: If you decide to use a different region (eg. one closer to home/your users), make sure you use the same region for S3 as well as the runtime (mentioned later). Otherwise you’ll run into permission issues you’ll never be able to solve!
2) Jump into the project dir (eg. my-project), and install bref using composer;
4) Create a deploy.sh executable file to save yourself some typing;
# Package up your files and send it to an S3 bucket you're going to use; sam package --output-template-file .stack-symfony.yaml --s3-bucket symfony-lambda --region=ap-southeast-2
# Deploy (using cloud-formation, which will create your lambda function, etc) sam deploy --template-file .stack-symfony.yaml --stack-name symfony-lambda --capabilities CAPABILITY_IAM --region=ap-southeast-2
5) Create a cloudformation template file (we’ll call this ‘.stack-symfony.yaml’ … if you want to call it something different, put in the new name in the above command instead);
Runtimes (basically the PHP executable which is needed by Lambda (PHP isn’t built into lambda, so we supply it as a ‘layer’). See; https://bref.sh/docs/runtimes/
In the file above, add in your s3 bucket name, and replace the ‘layers’ mentioned above with the later from your region (if you’ve decided to use a different one). See; https://bref.sh/docs/runtimes/
6) Run your ./deploy.sh script and follow the prompts if there’s any issues.
I ran into multiple permission issues which were all easily solved by giving access to the user i’d created for this project.
See the Symfony bref guide for more details if you get stuck; https://bref.sh/docs/frameworks/symfony.html
This article is of course free, but there’s some on-going costs you’ll need to consider with Lambda. The few websites i’ve read regarding this seem to indicate it’s cheaper than running an EC2 instance (both from an actual cost and a time-cost in maintaining the thing), but of course you can do a lot of things with EC2!
The cost estimates i’m using come from US East (Ohio), and were taken on 8/Feb/2019;
Lambda (for your actual PHP server-side code)
$3.50/million requests – first 333 million requests/month
AWS Lambda – Compute Free Tier – 400,000 GB-Seconds – US East (Ohio)12.163 Lambda-GB-Second
AWS Lambda – Requests Free Tier – 1,000,000 Requests – US East (Ohio)
S3 (for storage of your packaged site, as well as any assets – css/images/js/uploaded files);
$0.005 per 1,000 PUT, COPY, POST, or LIST requests
$0.004 per 10,000 GET and all other requests
$0.023 per GB – first 50 TB / month of storage used
Some others include;
CloudFront (for serving up your assets if you use it later on)
Relational Database Service (for a database if you need one)
Route 53 (for your DNS needs)
Simple Email Service (for sending emails)
Inspiration from this comes from this YouTube vid – well worth watching;
If the commit has already been pushed to your external repo, you’ll need to add ‘–force’ next time you push if the commit is rejected (the repo is probably going to be ‘ahead’ of the commit you’re trying to push to it);