Writing, building, and packaging

For the polyglots among us with options when it comes to what to write in, once we've chosen which language to write our function in, we have more choices about how to author a function. Let's talk about what's available.

The most simple method is to use the inline editor in the Lambda console. You can access this after creating your function and make direct changes to the code. Here's what the editor looks like:

Screenshot of the code editor in the Lambda console

The editor is actually quite configurable since it has many user options. This is because it is actually a managed Cloud9 environment underneath (https://aws.amazon.com/cloud9/). One limitation of using the editor is that you can't include your dependencies or libraries, so it is mainly useful for ad hoc changes. Beware, though – it's very easy to make a change in the console and not update your copy in source control! 

I mentioned AWS Cloud9 in the preceding section. Cloud9 is a recent addition to the AWS offerings and is a cloud-based Integrated Development Environment (IDE). You can write code in your browser and deploy it straight to Lambda. Cloud9 also supports collaborative coding for pair programming. Think of it as the Google Docs of word processors! Cloud9 is really useful because you can take all of your user settings and preferences with you when moving onto another device. 

Most people have their favorite IDE, and one of my favorites is Visual Studio Code (VS Code), with the Material Theme Ocean High Contrast color theme and icon set. VS Code comes with a tabbed editor window and an integrated Terminal. How good does it look!

Microsoft Visual Studio Code editor

I've added some extensions to help with my development workflow. Some of these include linters for JavaScript and Dockerfiles, which assist me by highlighting syntax errors so that I can pick up on issues before deployment time. Another awesome extension that I use is called Live Sync for AWS Cloud 9. This was written by Ian Mckay in Australia and allows you to bring Cloud9 features into your VS Code experience. This is really cool if you want to make use of the collaboration and chat functionality, but don't want to change to an IDE that you're not used to. Check it out on GitHub: https://github.com/iann0036/cloud9-sync.

If you're developing in .NET, you might want to look at the AWS Toolkit for Microsoft Visual Studio: https://aws.amazon.com/visualstudio/. This extension has extra tools for building and deploying to Lambda. 

So, now that we have our developer experience set up how we want it, we can move on to options for defining the project's structure. What I'm alluding to here are frameworks for writing and templating serverless applications. Now, these tools aren't a requirement for making a function work, but as you start to grow your serverless application with multiple Lambda functions, messaging systems, and data stores, you will quickly find that you need help.

An application framework brings a few benefits to your Lambda and serverless development:

  • Centralizes the code for components of a serverless application
  • Enables more control across a distributed development team
  • Allows for local testing and debugging
  • Encourages templating and reuse of code and libraries
  • Give you extra tools to help with deploying, rollback, and management

There's a whole heap of frameworks that you can get started with, such as the Serverless Framework, AWS Chalice, Apex, and AWS Serverless Application Model (SAM), to name a few. We're going to cover my favorite framework in Chapter 7, Serverless Framework, but it's good to know that there are options available in this space. 

In the meantime, let's look at what it means to run a build to make a deployment package without the help of a framework. For Python, Node.js, and Ruby, there are no compile steps that need to happen, so technically there is no build step here. Instead, we need to package our function and all the dependencies into a ZIP archive, ready to be uploaded to an S3 bucket. Note that the required dependencies should already be installed in the local directory before zipping. The command to ZIP may look something like the following for a Node.js function:

zip -r new-function.zip handler.js node_modules

For compiled languages, it's a little more complex. To build a Java function using Maven, we need to create a pom.xml file that has the appropriate project build configurations. Then, we can use the command line to build the project and create our Java archive file (.jar). The following is the file that will be uploaded to S3 for deployment:

mvn package 
More information on creating Java deployment packages, including an example using Gradle, can be found at https://docs.aws.amazon.com/lambda/latest/dg/lambda-java-how-to-create-deployment-package.html.

For functions in C#, it's recommended to use the .NET Core CLI to create and deploy the project: https://docs.aws.amazon.com/lambda/latest/dg/lambda-dotnet-coreclr-deployment-package.html.

That covers the basics of writing and building functions. In the next section, we will learn how to deploy artifacts so that we can invoke our functions in Lambda.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.81.98