# Deploying AWS Lambda To The Cloud with Serverless

# What are Serverless and Lambda

AWS Lambda is a fantastic Serverless technology from Amazon. It allows you to focus on your code and let AWS handle the infrastructure for you. Serverless Framework makes it easy to package and deploy your Lambda infrastructure as code. In this article, we'll go over deploying a simple Lambda function to the cloud.

# Serverless Configuration

Our Serverless project has several files. Feel free to download or clone our open source repo with the full project code located under sample-apps/serverless-lambda. Now, let's start with package.json.

{
  "dependencies": {
    "aws-sdk": "2.556.0"
  },
  "devDependencies": {
    "@types/aws-lambda": "8.10.24",
    "serverless-plugin-typescript": "1.1.7",
    "typescript": "3.6.4"
  }
}

package.json - package configuration

Here we define the packages needed to deploy our project. Which are aws-sdk, typings for aws-lambda since we're using Typescript, Serverless Typescript plugin, and Typescript npm.

service: commandeer-serverless-lambda

provider:
  name: aws
  runtime: nodejs10.x
  memorySize: 256
  region: us-east-1
  stage: ${opt:stage, 'dev'}
  timeout: 30
  versionFunctions: false

plugins:
  - serverless-plugin-typescript

functions:
  tankHandler: ${file(./handlers/tankHandler.yml):tankHandler}

serverless.yml - main serverless configuration file

Serverless.yml file connects all Serverless pieces together. At the top, we define our service, AWS provider, followed by the plugins, and our function definition.


 


tankHandler:
  handler: handlers/tankHandler.process
  timeout: 900

tankHandler.yml - tank handler configuration

The next stop is the tank handler configuration. We define our handler at handlers/tankHandler.process. We also define an optional function timeout setting.









 














import { Context, Handler } from 'aws-lambda';

/**
 * @description handle some tank payload
 * @param event event object
 * @param context context object
 */
const process: Handler = async (event: any, context: Context) => {
  console.log('tankHandler.process', { event });

  try {
    const shot = {
      message: 'Boom! 💥',
      event,
    };
    context.succeed(shot);
  } catch (exception) {
    context.fail(exception);
  }
};

export { process };

tankHandler.ts - tank handler code

tankHandler.ts file defines the code that will be executed by our Lambda. The code is quite straightforward. It succeeds with a shot object containing the original event and the message. Note the console.log statement printing tankHandler.process. We'll use it to verify our lambda is invoked later on.

# Running It

Now we're off to the races, let's run some Serverless! Open Commandeer. If you haven't connected your AWS account yet, just follow our getting started guide. Select your serverless file. If you downloaded or cloned our open source repo it's located under sample-apps/serverless-lambda. Click deploy and let the magic happen. Once the deploy is complete, you'll see the terminal output on the right-hand side.

Serverless deploy completed

TIP

If you're curious how to deploy your Lambda locally using LocalStack, check out our LocalStack Serverless tutorial.

# Invoking Your Lambda

Now that our lambda is deployed, let's invoke it to make sure it's working as expected. In Commandeer, navigate to Lambda -> tankHandler -> Invoke Lambda. On the left side, you can enter the incoming event for your Lambda, we'll just use the default. Click Invoke, and you'll see the logs from your Lambda invocation on the right. Remember the log message we added to our lambda handler earlier? Here it is.

Lambda invoked

# Downloading Your Lambda Code

Another useful feature of Commandeer is being able to download your Lambda code. It's quite useful for debugging purposes. Especially if you use a packaging tool like webpack. Open Commandeer, Navigate to Lambda -> tankHandler -> Dashboard. Choose the Code tab and click Download Code to download the zipped version of your lambda. The downloaded archive will have the resulting lambda the way it's stored on AWS so you can inspect it if necessary.

# Conclusion

AWS Lambda combined with Serverless framework allows you to take advantage of IAC for your Serverless deployment and abstract away the DevOps complexity of your infrastructure. Commandeer allows you to deploy and test your lambdas effortlessly from a convenient desktop UI. Here is the download link for Commandeer. If you run into any issues, feel free to create an issue on our Github. Cheers!