Saturday, August 10, 2019

Run AWS Lambda Function Code Locally - Quick Tutorial

Why do I want to Run the Lambda Function Code locally?


One of my dev friends falsely believed that AWS Lambda Function would require deployment in order to run. This is not true. It is a common misunderstanding since in the Lambda Function you are only defining a handler.

The solution is simple - just write a main function that includes your lambda handler and pass parameters to it! No deployment needed.

How to Do it?


Here's your lambda function in index.js

exports.handler = (event, context, callback) => {
  console.log('hello', event.name, 'from', process.env.SERVICE_NAME);
  callback(undefined, 'done');
};
Write your main.js

process.env.SERVICE_NAME = 'commandline lambda function';

const index = require('./index.js');

const context = {};

const event = {
  "name": "world"
}

index.handler(event, context, (err, data) => {
  if (err) { console.error('error: ', err); }
  if (data) { console.log('data: ', data); }
});
You have environment variables defined, have your event defined, and invoked the lambda function handler. Now just call it from the terminal:
node main.js
Note that it will use the credential in your ~/.aws/credentials file, just like the aws-cli tool.

How to run the command line lambda function with aws-okta credentials:

aws-okta exec my-aws-account-name -- node main.js

Getting Started with Vim - Quick Tutorial

Why would you want to use Vim?


When you ssh into a server, you will need a text editor that works in the terminal to change for example, server configurations. Nano is a text editor that works on terminal. But it is hard to navigate fast with Nano. Improve your Vim skill for the speed!

How to use Vim?


To open a file:
  • In the terminal, type in:  vi /tmp/1.txt
To navigate:
  • Your cursor keys just work
  • Press 'control' + 'D' for pageDown
  • Press 'control' + 'U' for pageUp

To get to the bottom of the doc:
  • Press 'shift' + 'G'
To get to the top of the doc:
  • Type in ':0' then 'enter'
To get to the start of the line:
  • Press 'shift' + '^'
To get to the end of the line:
  • Press 'shift' + '$'
To get to the 50th line of the doc:
  • Type in ':50' then 'enter'
To delete the current line:
  • Type in 'dd'
To delete the next 20 lines:
  • Type in '20dd'
To delete the all lines:
  • Type in '100000dd'
  • Repeat until there's no line left
To save file and exit:
  • Type in ':wq!' then 'enter'
To exit without saving:
  • Type in ':q!' then 'enter'
To edit:
  • Press the 'i' key to enter editing mode
  • Your 'delete' key and 'backspace' key just work
To exit editing mode:
  • Press the 'esc' key
To undo a change:
  • Press the 'esc' key to exit the edit mode
  • Press the 'u' key to undo
To get to a word:
  • Press the '/' key to search forward
  • Example: '/hello' will search forward the next hello that appears in the text
  • Press the 'n' key to get to the next 'hello'
To get to a word from the bottom of the doc:
  • Press 'shift' + 'G' to get to the bottom
  • Press 'shift' + '?' key to search backward
  • Example: '?hello' will search last hello that appears in the text
  • Press the 'n' key to get to the next 'hello'
To delete a word:
  • Use the above method to get to the word
  • Press 'dw' to delete the word
To delete one character:
  • Use the above method to get to the word
  • Press 'x' to delete a character
To copy text: 
  • Your cursor just works in selecting text
  • Your Windows command just works: 'ctrl' + 'c'
  • 'cmd' + 'c' for OSX
To paste text:
  • Press the 'i' key to enter editing mode
  • Your Windows command just works: 'ctrl' + 'v'
  • 'cmd' + 'v' for OSX
 If your terminal does not support text selection with copy & paste, try:

To copy 1 line:
  • Press 'yy' to copy the current line
To paste 1 line:
  • Press 'p' to paste a copied a line


Great. You know how to use vim.