Skip to content

Instantly share code, notes, and snippets.

@angelxmoreno
Last active August 14, 2018 01:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angelxmoreno/ec632364b713f5127b84401de02cce07 to your computer and use it in GitHub Desktop.
Save angelxmoreno/ec632364b713f5127b84401de02cce07 to your computer and use it in GitHub Desktop.
Example of running PHP code on AWS Lambda

Example of running PHP code on AWS Lambda

The endpoints

Fahrenheit to Celsius : https://rfvxsxrjd5.execute-api.us-east-1.amazonaws.com/dev/f2c

Celsius to Fahrenheit : https://rfvxsxrjd5.execute-api.us-east-1.amazonaws.com/dev/c2f

Examples

# 32 degrees Fahrenheit to Celsius  
curl -X POST -d "f=32" https://rfvxsxrjd5.execute-api.us-east-1.amazonaws.com/dev/f2c
# 100 degrees Celsius to Fahrenheit  
curl -X POST -d "c=100" https://rfvxsxrjd5.execute-api.us-east-1.amazonaws.com/dev/c2f

The code

$aws->post('/dev/f2c', function (Request $request, Response $response) {
    $params = $request->getParsedBody();
    $f = (int)$params['f'];
    $c = ($f - 32) * .5556;

    return $response->withJson([
        'fahrenheit' => $f,
        'celsius' => $c,
    ]);
});

$aws->post('/dev/c2f', function (Request $request, Response $response) {
    $params = $request->getParsedBody();
    $c = (int)$params['c'];
    $f = ($c * 9 / 5) + 32;

    return $response->withJson([
        'celsius' => $c,
        'fahrenheit' => $f
    ]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment