Amazon Web Services Feed
Jump-starting your serverless development environment
Developers building serverless applications often wonder how they can jump-start their local development environment. This blog post provides a broad guide for those developers wanting to set up a development environment for building serverless applications.
To use AWS Lambda and other AWS services, create and activate an AWS account.
Command line tooling
Command line tools are scripts, programs, and libraries that enable rapid application development and interactions from within a command line shell.
The AWS CLI
The AWS Command Line Interface (AWS CLI) is an open source tool that enables developers to interact with AWS services using a command line shell. In many cases, the AWS CLI increases developer velocity for building cloud resources and enables automating repetitive tasks. It is an important piece of any serverless developer’s toolkit. Follow these instructions to install and configure the AWS CLI on your operating system.
AWS enables you to build infrastructure with code. This provides a single source of truth for AWS resources. It enables development teams to use version control and create deployment pipelines for their cloud infrastructure. AWS CloudFormation provides a common language to model and provision these application resources in your cloud environment.
AWS Serverless Application Model (AWS SAM CLI)
AWS Serverless Application Model (AWS SAM) is an extension for CloudFormation that further simplifies the process of building serverless application resources.
It provides shorthand syntax to define Lambda functions, APIs, databases, and event source mappings. During deployment, the AWS SAM syntax is transformed into AWS CloudFormation syntax, enabling you to build serverless applications faster.
The AWS SAM CLI is an open source command line tool used to locally build, test, debug, and deploy serverless applications defined with AWS SAM templates.
Install AWS SAM CLI on your operating system.
Test the installation by initializing a new quick start project with the following command:
$ sam init
- Choose 1 for the “Quick Start Templates”
- Choose 1 for the “Node.js runtime”
- Use the default name.
The generated /sam-app/template.yaml
contains all the resource definitions for your serverless application. This includes a Lambda function with a REST API endpoint, along with the necessary IAM permissions.
Resources: HelloWorldFunction: Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction Properties: CodeUri: hello-world/ Handler: app.lambdaHandler Runtime: nodejs12.x Events: HelloWorld: Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api Properties: Path: /hello Method: get
Deploy this application using the AWS SAM CLI guided deploy:
$ sam deploy -g
Local testing with AWS SAM CLI
The AWS SAM CLI requires Docker containers to simulate the AWS Lambda runtime environment on your local development environment. To test locally, install Docker Engine and run the Lambda function with following command:
$ sam local invoke "HelloWorldFunction" -e events/event.json
The first time this function is invoked, Docker downloads the lambci/lambda:nodejs12.x container image. It then invokes the Lambda function with a pre-defined event JSON file.
Helper tools
There are a number of open source tools and packages available to help you monitor, author, and optimize your Lambda-based applications. Some of the most popular tools are shown in the following list.
Template validation tooling
CloudFormation Linter is a validation tool that helps with your CloudFormation development cycle. It analyses CloudFormation YAML and JSON templates to resolve and validate intrinsic functions and resource properties. By analyzing your templates before deploying them, you can save valuable development time and build automated validation into your deployment release cycle.
Follow these instructions to install the tool.
Once, installed, run the cfn-lint
command with the path to your AWS SAM template provided as the first argument:
cfn-lint template.yaml
The following example shows that the template is not valid because the !GettAtt
function does not evaluate correctly.
IDE tooling
Use AWS IDE plugins to author and invoke Lambda functions from within your existing integrated development environment (IDE). AWS IDE toolkits are available for PyCharm, IntelliJ. Visual Studio.
The AWS Toolkit for Visual Studio Code provides an integrated experience for developing serverless applications. It enables you to invoke Lambda functions, specify function configurations, locally debug, and deploy—all conveniently from within the editor. The toolkit supports Node.js, Python, and .NET.
The AWS Toolkit for Visual Studio Code
From Visual Studio Code, choose the Extensions icon on the Activity Bar. In the Search Extensions in Marketplace box, enter AWS Toolkit and then choose AWS Toolkit for Visual Studio Code as shown in the following example. This opens a new tab in the editor showing the toolkit’s installation page. Choose the Install button in the header to add the extension.
AWS Cloud9
Another option to build a development environment without having to install anything locally is to use AWS Cloud9. AWS Cloud9 is a cloud-based integrated development environment (IDE) for writing, running, and debugging code from within the browser.
It provides a seamless experience for developing serverless applications. It has a preconfigured development environment that includes AWS CLI, AWS SAM CLI, SDKs, code libraries, and many useful plugins. AWS Cloud9 also provides an environment for locally testing and debugging AWS Lambda functions. This eliminates the need to upload your code to the Lambda console. It allows developers to iterate on code directly, saving time, and improving code quality.
Follow this guide to set up AWS Cloud9 in your AWS environment.
Advanced tooling
Efficient configuration of Lambda functions is critical when expecting optimal cost and performance of your serverless applications. Lambda allows you to control the memory (RAM) allocation for each function.
Lambda charges based on the number of function requests and the duration, the time it takes for your code to run. The price for duration depends on the amount of RAM you allocate to your function. A smaller RAM allocation may reduce the performance of your application if your function is running compute-heavy workloads. If performance needs outweigh cost, you can increase the memory allocation.
Cost and performance optimization tooling
AWS Lambda power tuner is an open source tool that uses an AWS Step Functions state machine to suggest cost and performance optimizations for your Lambda functions. It invokes a given function with multiple memory configurations. It analyzes the execution log results to determine and suggest power configurations that minimize cost and maximize performance.
To deploy the tool:
- Clone the repository as follows:
$ git clone https://github.com/alexcasalboni/aws-lambda-power-tuning.git
- Create an Amazon S3 bucket and enter the deployment configurations in
/scripts/deploy.sh
:# config BUCKET_NAME=your-sam-templates-bucket STACK_NAME=lambda-power-tuning PowerValues='128,512,1024,1536,3008'
- Run the
deploy.sh
script from your terminal, this uses the AWS SAM CLI to deploy the application:$ bash scripts/deploy.sh
- Run the power tuning tool from the terminal using the AWS CLI:
aws stepfunctions start-execution --state-machine-arn arn:aws:states:us-east-1:0123456789:stateMachine:powerTuningStateMachine-Vywm3ozPB6Am --input "{"lambdaARN": "arn:aws:lambda:us-east-1:1234567890:function:testytest", "powerValues":[128,256,512,1024,2048],"num":50,"payload":{},"parallelInvocation":true,"strategy":"cost"}" --output json
- The Step Functions execution output produces a link to a visual summary of the suggested results:
Monitoring and debugging tooling
Sls-dev-tools is an open source serverless tool that delivers serverless metrics directly to the terminal. It provides developers with feedback on their serverless application’s metrics and key bindings that deploy, open, and manipulate stack resources. Bringing this data directly to your terminal or IDE, reduces context switching between the developer environment and the web interfaces. This can increase application development speed and improve user experience.
Follow these instructions to install the tool onto your development environment.
To open the tool, run the following command:
$ Sls-dev-tools
Follow the in-terminal interface to choose which stack to monitor or edit.
The following example shows how the tool can be used to invoke a Lambda function with a custom payload from within the IDE.
Serverless database tooling
NoSQL Workbench for Amazon DynamoDB is a GUI application for modern database development and operations. It provides a visual IDE tool for data modeling and visualization with query development features to help build serverless applications with Amazon DynamoDB tables. Define data models using one or more tables and visualize the data model to see how it works in different scenarios. Run or simulate operations and generate the code for Python, JavaScript (Node.js), or Java.
Choose the correct operating system link to download and install NoSQL Workbench on your development machine.
The following example illustrates a connection to a DynamoDB table. A data scan is built using the GUI, with Node.js code generated for inclusion in a Lambda function:
Conclusion
Building serverless applications allows developers to focus on business logic instead of managing and operating infrastructure. This is achieved by using managed services. Developers often struggle with knowing which tools, libraries, and frameworks are available to help with this new approach to building applications. This post shows tools that builders can use to create a serverless developer environment to help accelerate software development.
This list represents AWS and open source tools but does not include our APN Partners. For partner offers, check here.
Read more to start building serverless applications.