Amazon Lambda

aws

I was having problem with limits on request / response body size, and was having problem with accessing RDS.

https://docs.aws.amazon.com/lambda/latest/dg/vpc.html
https://aws.amazon.com/blogs/aws/new-access-resources-in-a-vpc-from-your-lambda-functions/ - done reading

https://docs.aws.amazon.com/lambda/latest/dg/vpc-rds.html - done reading, but I should come back to this later because it covers command-line usage.
https://docs.aws.amazon.com/lambda/latest/dg/programming-model.html - done reading
https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-logging.html - later
https://docs.aws.amazon.com/lambda/latest/dg/nodejs-create-deployment-pkg.html - done reading
https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-nodejs.rds.html - Later. This is for BeanStalk environment
http://www.rojotek.com/blog/2017/08/07/accessing-rds-from-aws-lambda/ - done reading
https://blog.risingstack.com/getting-started-with-aws-lambda-and-node-js/ - done reading
https://apievangelist.com/2017/11/06/a-simple-api-using-aws-rds-lambda-and-api-gateway/ - done reading
http://apievangelist.com/2017/10/23/a-simple-api-with-aws-dynamodb-lambda-and-api-gateway/ - later
https://redstapler.co/aws-lambda-tutorial-rds-mysql/ - done reading
https://cloudonaut.io/passwordless-database-authentication-for-aws-lambda/

https://seancoates.com/blogs/lambda-payload-size-workaround/

https://aws.amazon.com/blogs/security/how-to-automatically-update-your-security-groups-for-amazon-cloudfront-and-aws-waf-by-using-aws-lambda/
https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-at-the-edge.html
https://docs.aws.amazon.com/lambda/latest/dg/vpc-rds.html
https://docs.aws.amazon.com/lambda/latest/dg/vpc-ec.html
https://docs.aws.amazon.com/lambda/latest/dg/best-practices.html

https://www.reddit.com/r/aws/comments/3st34k/using_api_gateway_lambda_how_do_you_deal_with/
https://medium.com/@jconning/tutorial-aws-lambda-with-api-gateway-36a8513ec8e3
https://serverless.com/framework/docs/providers/aws/events/apigateway/
https://dzone.com/articles/aws-elastic-beanstalk-timeouts-a-detective-story

http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_nodejs.html
https://dzone.com/articles/aws-lambda-for-beginners
http://docs.aws.amazon.com/lambda/latest/dg/welcome.html
https://dzone.com/articles/the-rise-of-lambda
https://dzone.com/articles/lambdas-and-clean-code

How to create our first Lambda function using Node?

  1. In the AWS Lambda interface, we can click on the "Create function" in the top right corner of the screen.
  2. Give the function a name
  3. Select a run-time (Node version)
  4. Select an existing role or create a new role
  5. Click on the "Create function" button at the bottom of the screen

On the subsequent screen, we can select "Edit code inline", "Upload a ZIP file", or "Upload a file from Amazon S3". If we choose the "Edit code inline" option, and it keeps saying "Loading your function", we can right click in this middle section of the page and select "Reload frame" (if we are using Chrome). This is a nice online editor for some simple code, but for things that are more complicated, like requiring other modules, we need to create a Node project:

  1. Open a command prompt
  2. Navigate to a folder where we want to keep our project
  3. mkdir projectName
  4. cd projectName
  5. Run "npm init" and provide answers to on-screen questions.
  6. npm install mysql --save

The above steps assume that we already have Node, and npm installed, and that we are already familiar with creating a Node project. Now create the index.js:

var mysql = require("mysql");
exports.handler = (event, context, callback) => {
    const connection = mysql.createConnection({
      host     : '...',
      user     : '...',
      password : '...',
      database : '...'
    })

    connection.connect(function(err) {
        if (err) {
            return callback(err);
        }
        if (err) throw err;
        console.log("Connected!");

        const sql = 'SELECT NOW() FROM DUAL';
        connection.query(sql, function (error, results, fields) {
            if (error) {
                return callback(error)
            }
            callback(null, results)
        })
    });

};

Open Windows Explorer and goes into our project folder. This folder should now contains: index.js, package.json, and the node_modules folder. Select these items, right click on these items, and select "Send to -> Compressed (zipped) folder". Give this zip file a name. Switch back to the browser. Select the "Upload a .ZIP file" option. Click on the Upload button. Select the zip file that we just created and upload. Click on the Test button in the top right corner of the screen and follow the on-screen instruction. If everything is configured correctly, the test should pass, otherwise, it may fails or timed-out, and we will need to look at the log. When we run a test, and if it failed, it should display "Execution result: failed(logs)" at the top. We can click on (logs) to see more detail.

The above code is just a function. There is no URL associated with it yet. We can specify the URL for this function using AWS API Gateway. Our project can be complex and involve multiple URLs, so we can modify the logic in the exports.handler function to match whatever the equivalent of a Traditional Node project need to be.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License