run a php script in aws EC2 server using lambda - php

I have setup EC2 and RDS on it for an app, now I want to call a script present on the EC2 server (ubuntu, Apache running) every day (which is a sort of trigger for another service) to run within EC2, or a way to run that PHP script on Lambda itself removing EC2 from it.
What I did find was about python script and this: Serverless PHP on AWS Lambda – Rob Allen's DevNotes

Option 1: PHP on AWS Lambda
If you can get PHP working on Lambda, then that solves half your question. You can then schedule it using Amazon CloudWatch Events. Simple create a rule with a schedule to trigger the Lambda function.
Option 2: Triggering script on Amazon EC2 instance
If you just want to trigger a script on an Amazon EC2 instance, you can use a local cron definition.
If your intention was to only run the EC2 instance for the script and then turn off, then:
Configure the script to run when the instance starts up (configure the operating system to run the script)
Configure an Amazon CloudWatch Events rule to run an AWS Lambda function once per day
The Lambda function should start the instance
When the script on the instance has completed its work, it should call the operating system to shutdown the instance. This will cause EC2 to stop it.
Instead of starting and stopping an instance, you could instead Launch and Terminate an instance. In this case, supply the script as User Data and it will automatically run after launch. Configure the instance Shutdown Behavior as Terminate.

The way I'm currently doing this is by calling the script through HTTP using Node.
First, I have set up an AWS Lambda function like this:
exports.handler = (event, context, callback) => {
const https = require('https');
https.get('https://www.example.com/myscript?secret-token', (resp) => {});
};
The secret token is just so that I'm the only one who can execute this script by going to that URL.
Next, I schedule it to run on an EventBridge trigger, with a schedule expression similar to the following:
cron(0 8 * * ? *)
Which makes AWS Lambda call that URL every day 5am local time for me, 8am UTC.

Related

Getting AWS Elastic Beanstalk health code programatically via php

I have an AWS Elastic Beanstalk instance on which I have some crons running. I occasionally have issues
Environment health has transitioned from Ok to Severe. 4.5 % of the requests to the ELB are failing with HTTP 5xx (2 minutes ago)
I have alarms set to let me know about this, and that part is working correctly. But I want to stop the crons from executing if the environment health status is not "OK" or "Warning". Is there a way to programatically get the health status code (shown in image below, circled in red) from a php script running on the same instance?
I would use CloudWatch alarm to trigger sending a notification email in such event.
The first thing to understand is that Elastic Beanstalk to a convenient tool to bootstrap the infrastructure on AWS. Anything you setup with Elastic Beanstalk, either via its cli or console, you can pretty much do the same on the EC2 page.
On AWS console, go to EC2
Go to Load Balancing/Target Groups
Find the right Target Group that is attached to the Load Balancer created by Elastic Beanstalk
Check the "Monitoring" tab, you should see "CloudWatch alarms: no alarm configured"
Click "Create Alarm" and finish off the rest, you may need to create a topic, set email address, and define the alarm triggering criteria.
Assuming you have a health script within your application that beanstalk pings periodically, you should be able to determine if your app is healthy or not within your application. You should not rely on having to programically get the status from Benstalk.
For example, within your health script you can set a flag if your application is healthy or not. Such flag can be in the form of a touched file in the filesystem. Or a flag somewhere in a cache if you use one. Ex: Memcache.

Schedule a monthly event in PHP

I want PHP layer to execute a method (or call a web service) every month. My PHP sits in AWS elastic beanstalk environment.
My requirement is to run a task every month on a certain time.
The
triggering of event execute a PHP code that purges data that sits in
the RDS as per the defined business rules.
Cron job or MySQL triggers would not help me. Is this possible at all?

Run a scheduled Azure PHP script on each instance

We have a website running on multiple Azure instances – typically between 2 and 5.
There is a PHP script I would like to schedule to run every few minutes on each instance. (It just makes a local copy of data from a system that couldn't handle the load from all our users hitting it in real-time.)
If it were just one instance, that would be easy - I'd use Azure Scheduler to call www.example.com/my-scheduled-task.php every 5 minutes.
But the script needs to run on each instance, so that every instance has a reasonably up-to-date copy of the data. How would you achieve this? I can't work out if it's something in Azure Scheduler, or if I should be looking at some sort of startup script?
You can use a continuous webjob for that.
Just tweak your php script to have a loop and add a sleep of a few minutes between runs of your code.
The continuous webjob will run on all of your instances and even if somethings fails it will be brought back up.
Per my experience, a PHP webjob running on your each webapp instance is the good solution as #AmitApple said. However, I think you can try to use a scheduled webjob with a CRON expression for ensuring a start time, not a continuous one with a sleep time. And please make sure the script can be completed in the interval time.
You can refer to the section Create a scheduled WebJob using a CRON expression of the doc Run Background tasks with WebJobs to know how to get start.
Please see the note of the section Create a continuously running WebJob https://azure.microsoft.com/en-us/documentation/articles/web-sites-create-web-jobs/#CreateContinuous.
Note:
If your web app runs on more than one instance, a continuously running WebJob will run on all of your instances. On-demand and scheduled WebJobs run on a single instance selected for load balancing by Microsoft Azure.
For Continuous WebJobs to run reliably and on all instances, enable the Always On* configuration setting for the web app otherwise they can stop running when the SCM host site has been idle for too long.

PHP application on Azure as "Console App"

is there a way how to easily run a PHP application as from command line on Windows Azure?
I have a standard Web Application (on Azure) and I want to communicate using WebSockets.
So I need to have a WebSocket Server running all the time on Azure.
I use Wrench project which I need to run "all the time" to listen on some port and deal with messages from JavaScript-sended WebSocket.
So again - how easily run a "persistent" PHP application on Azure?
Thank you in advance.
Sandrino's answer is fine, but I prefer ProgramEntryPoint for doing this sort of thing. The trouble with a background task is that (unless you build something on your own) nothing is monitoring it. Using ProgramEntryPoint, Windows Azure will monitor the process, and if it exits for any reason, the role instance will be restarted.
EDIT:
Sandrino points out that the PHP program isn't the only thing running. (There's also a website.) In that case, I'd recommend launching php.exe in Run() in WebRole.cs. Process.Start it and then do a .WaitForExit() on it. That way, if the process exits, the role itself will exit from Run(), causing the role instance to restart. See http://blog.smarx.com/posts/using-other-web-servers-on-windows-azure for an example.
In order to run your PHP script as a command line application you should use the PHP CLI (command line interface).
php.exe -f "yourWebSocketServce.php" -- -arg1 -arg2 -arg3
Now, in order to run this in Windows Azure you'll need to define a startup task that runs this command. You'll see that the default task type is simple, which means that the startup of your role will block until the task finishes. But in your case running the WebSocket in PHP will be a blocking process, that's why you should change the type to background (this will make sure the instance continues starting up while your WebSocket server is running).
Here is a WebSockets service on Azure. - Live XSockets.NET
Have a look at http://live.xsockets.net, an easy way of getting started, but it depends on what you are about to do on the "server side". This service i mention can be uses as a "message" dispatcher, to ntify "clients" on changes etc.. Hmm in other words it is a way of boosting "regular" web-apps..

MySQL trigger on RDS

I am trying to create a MySQL trigger that will invoke a php script. I have a MySQL server instance running in RDS and would like to use the php script to send a message to my SQS messaging system.
Where do I save the php scripts?
Do I need to install the PHP SDK for SQS on my EC2 instance?
Yes, you can use triggers normally.
Use this steps:
Change in "parameter group" menu the variable:
log_bin_trust_function_creators to "ON" or "1".
Do a "modify" and "reboot" at instance. MANDATORY!
Verify with mysql client if the variable was really modified. The command is: SHOW VARIABLES LIKE 'log_bin_trust%'
Now, you can create your triggers normally. The great difficult is to check questions correctly: your instance should be use that parameter group, and this should be correctly set
Forget about triggers, especially on Amazon RDS.
Use a cron to execute a PHP script every 5 minutes (for example) that looks for unsent messages and sends them using the SQS messaging system.

Categories