The following kills all processes (in ubuntu) which match the word ‘test123’;
kill $(ps aux | grep test123 | awk '{print $2}')
The ramblings of a software developer
The following kills all processes (in ubuntu) which match the word ‘test123’;
kill $(ps aux | grep test123 | awk '{print $2}')
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).
I followed this guide for the commands;
Download and compile the following required libraries;
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
sudo phpize
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/ sudo make make test
For me, I include the following in the zip file;
The following command creates a ‘gd.zip’ file with the above libraries & files;
zip -r gd.zip lib* bref
This part’s the easy one;
aws lambda publish-layer-version --layer-name php-7-3-gd --description "gd php extension" --zip-file fileb://gd.zip
The lambda layer name can be whatever you like, along with the description. The file part (.zip) refers to the zip file you’ve created, stored on your local machine
AWS will spit out some JSON, with the ‘LayerVersionArn’ as well — this is what you need to include in your list of Lambda layers for your function, eg;
.... "LayerVersionArn": "arn:aws:lambda:ap-southeast-2:1234567:layer:php-7-3-gd:16", "Version": 16, "Description": "gd php extension", .... }
Ingredients;
Method
Sauce
Credit goes to Scott for this amazing recipe!
For those going down the server-less route and using Symfony, this will hopefully give you a decent starting point.
I’ve added a ‘part 2‘ to this post, with performance optimisations, as well as handling being behind the API Gateway.
This uses a project called Bref – their website is a great starting point and has loads of info.
Ingredients (what you’ll need on-hand);
Method (how do we do this);
A lot of these instructions come from https://bref.sh
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;
1) Install the Symfony skeleton project (see https://symfony.com/doc/current/setup.html) & check to make sure it works as expected.
composer require mnapoli/bref
You may also need to run the following;
vendor/bin/bref init
3) Create an AWS S3 bucket to store your ‘packaged’ files;
aws s3 mb s3://symfony-lambda --region=ap-southeast-2
4) Create a deploy.sh executable file to save yourself some typing;
#!/bin/bash
# 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);
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: ''
Outputs:
DemoHttpApi:
Description: URL of our function in the *Prod* environment
Value: !Sub 'https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/'
Globals:
Function:
Environment:
Variables:
APP_ENV: prod
Resources:
Website:
Properties:
CodeUri: s3://<your s3 bucket here>/94ce2f32ac156cd3f06208d3e05dca8f
Description: ''
Events:
HttpRoot:
Properties:
Method: ANY
Path: /
Type: Api
HttpSubPaths:
Properties:
Method: ANY
Path: /{proxy+}
Type: Api
FunctionName: symfony-website
Handler: public/index.php
MemorySize: 1024
Layers:
- arn:aws:lambda:ap-southeast-2:416566615250:layer:php-72-fpm:8
Runtime: provided
Timeout: 30
Type: AWS::Serverless::Function
MyFunctionCli:
Properties:
CodeUri: s3://<your s3 bucket here>/94ce2f32ac156cd3f06208d3e05dca8f
FunctionName: symfony-console
Handler: bin/console
Timeout: 120 # in seconds
Layers:
- arn:aws:lambda:ap-southeast-2:416566615250:layer:php-72-fpm:8
- arn:aws:lambda:ap-southeast-2:416566615250:layer:console:4
Runtime: provided
Type: AWS::Serverless::Function
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;
Inspiration from this comes from this YouTube vid – well worth watching;
(Author Unknown)
An American investment banker was at the pier of a small coastal Mexican village when a small boat with just one fisherman docked. Inside the small boat were several large yellowfin tuna. The American complimented the Mexican on the quality of his fish and asked how long it took to catch them.
The Mexican replied, “only a little while.”
The American then asked why didn’t he stay out longer and catch more fish?
The Mexican said he had enough to support his family’s immediate needs.
The American then asked, “but what do you do with the rest of your time?”
The Mexican fisherman said, “I sleep late, fish a little, play with my children, take siestas with my wife, Maria, and stroll into the village each evening where I sip wine, and play guitar with my amigos. I have a full and busy life.”
The American scoffed. “I have an MBA from Harvard, and can help you,” he said. “You should spend more time fishing, and with the proceeds, buy a bigger boat. With the proceeds from the bigger boat, you could buy several boats, and eventually you would have a fleet of fishing boats. Instead of selling your catch to a middle-man, you could sell directly to the processor, eventually opening up your own cannery. You could control the product, processing, and distribution,” he said. “Of course, you would need to leave this small coastal fishing village and move to Mexico City, then Los Angeles, and eventually to New York City, where you will run your expanding enterprise.”
The Mexican fisherman asked, “But, how long will this all take?”
To which the American replied, “Oh, 15 to 20 years or so.”
“But what then?” asked the Mexican.
The American laughed and said, “That’s the best part. When the time was right, you would announce an IPO, and sell your company stock to the public and become very rich. You would make millions!”
“Millions – then what?”
The American said, “Then you could retire. Move to a small coastal fishing village where you could sleep late, fish a little, play with your kids, take siestas with your wife, and stroll to the village in the evenings where you could sip wine and play guitar with your amigos.”
Ref; http://renewablewealth.com/the-parable-of-the-mexican-fisherman/
If something gets committed to a GIT repo, the following command can be run to reverse it;
$ git reset HEAD~
Eg;
$ git commit -m "Something terribly misguided" # (1) $ git reset HEAD~ # (2) << edit files as necessary >> # (3) $ git add ... # (4) $ git commit -c ORIG_HEAD # (5)
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);
$ git push origin master --force
Ref; https://stackoverflow.com/questions/927358/how-to-undo-the-most-recent-commits-in-git
The following forces the browser to download the file as an ‘attachment’, rather than inline (within the browser window);
(in this case, named ‘my-pdf-filename.pdf’)
$response = new Response($pdfData, 200, [ 'Content-Length' => strlen($pdfData), 'Content-type' => 'application/pdf', 'Content-Transfer-Encoding' => 'binary' ]); $disposition = $response->headers->makeDisposition( ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'my-pdf-filename.pdf' ); $response->headers->set('Content-Disposition', $disposition);
This is an update to the previous translator post, allowing the system to use the default translator and just adding the required messages to the message-catalogue in-use.
This is a better approach as it doesn’t then mean you miss out on the good things Symfony provides out-of-the-box because you’ve had to define what specific component is in use for the translator.
Anyway — code is below;
services.yml
No changes are needed — no translator specifics are needed (apart from including a listener as in the previous post)
MyListener.php
Add a function to your kernel.event-listener as below;
/** * @param TranslatorInterface $translator */ public function addTranslatorResources(TranslatorInterface $translator) { $locale = $translator->getLocale(); $catalogue = $translator->getCatalogue($locale); $translations = ['messages' => ['key1' => 'my message is here']]; $loader = new ArrayLoader(); $customCatalogue = $loader->load($translations, $locale); $catalogue->addCatalogue($customCatalogue); }
Test using the following;
// debug code to test; echo $translator->trans('messages.key1');
du -h -d 1 .
Ordering this by size can be done by;
du -h -d 1 . | sort -h
Adding ‘r’ in the sort options (… | sort -rh ) will sort it in reverse order (largest at the top)
The following lists the virtual-host configuration for apache2 (in the command-line)
apache2ctl -S