Follow @devcrafter91

How to deploy Lambda functions and debug them like a boss

Thu Jul 04 2019 aws lambda node.js serverless

Intro

AWS Lambda is a serverless platform by Amazon where you can build simple microservices easily without managing and messing up with servers and painless configurations. You can execute pieces of code based on events in many different programming languages (Javascript, Python, Java, Go, C#) without caring about any environment either it's production or even workstation. Anything can be coded right in your browser and achieve maximum flexibility especially when you combine Lambda with Cloud9. AWS Lambda functions are basically similar to container services but they have a lifetime in seconds and get destroyed after each execution so you cant store local scope variables for example. You can create Lambda functions in many ways but I find Cloud9 the best dev-friendly approach. Cloud9 is an online IDE that was acquired by Amazon in 2016. You can build anything but you have to host somewhere that bad boy. Amazon provides an easy solution where you can host Cloud9 on an t2.micro instance with a single click. You can also host it on your own server like I did.

Setting up Cloud9

  1. Sign in to AWS console
  2. Select Cloud9 from services
  3. Click "Create environment"
  4. Give it a name
  5. Choose the default EC2 plan
  6. Finish the setup wizard

text

text

text

After creating the Cloud9 environment you can access the IDE just by clicking "Open IDE". If you chosen SSH instead of Cloud9 you may experience some issues during the bootstrap, make sure you successfully installed Cloud9 locally on your machine using Cloud9 Installer provided by Amazon and you already whitelisted the right Amazon IPs on your iptables configuration.

Cloud Installer
https://docs.aws.amazon.com/cloud9/latest/user-guide/installer.html.
Amazon IP Address Ranges
https://aws.amazon.com/blogs/aws/aws-ip-ranges-json/

Creating AWS Lambda function using Cloud9

  1. On the right panel, click "AWS Resources" to spawn the Lambda Panel
  2. Now click the λ+ icon to create a new Lambda function text
  3. Choose a name your app. text
  4. Chose the right language and template. You should always stick with empty templates and build from scratch. text
  5. You will next choose if your Lambda function is going to be called (triggered) through a REST API using AWS API Gateway. Cloud9 will automatically create an API for you but you can always set it up later. text
  6. Here's the tricky part. Based on the resources of your application you should choose the right Memory Limit and your Role should always be something that fits your app needs. WARNING! NEVER GIVE FULL AWS PERMISSIONS TO THE APP. text
  7. Click next and finish

Add some code and start debugging

Now, after creating the Lambda function, add the following code and click "Run". You will then be able to see the result (response) back from the Lambda function on your right panel.

exports.handler = (event, context, callback) => {
    const quotes = [{
        author: "Oscar Wilde",
        quote: "Be yourself; everyone else is already taken."
    }, {
        author: "Mahatma Gandhi",
        quote: "Be the change that you wish to see in the world."
    }, {
        author: "Friedrich Nietzsche",
        quote: "Without music, life would be a mistake"
    }];

    const randomQuote = quotes[Math.floor(Math.random() * quotes.length)];

    callback(null, randomQuote);
};

Result
text

If you click on the debug icon next to run icon and set a breakpoint on your code, you can debug your application where you can stop the execution flow of your app and start examine the values on the right panel. text

As far as I know, during running and debugging your application, Amazon will not charge you so feel free to play. After you finish the development stage, right click on your app on the right panel (AWS Resources) and click "Deploy". From there Cloud9 will do it's magic and upload your app automatically for you.

Share