Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
AWS CDK in Practice

You're reading from  AWS CDK in Practice

Product type Book
Published in Jun 2023
Publisher Packt
ISBN-13 9781801812399
Pages 196 pages
Edition 1st Edition
Languages
Authors (2):
Mark Avdi Mark Avdi
Profile icon Mark Avdi
Leo Lam Leo Lam
Profile icon Leo Lam
View More author details

Table of Contents (17) Chapters

Preface Part 1: An Introduction to AWS CDK
Chapter 1: Getting Started with IaC and AWS CDK Chapter 2: A Starter Project and Core Concepts Part 2: Practical Cloud Development with AWS CDK
Chapter 3: Building a Full Stack Application with CDK Chapter 4: Complete Web Application Deployment with AWS CDK Chapter 5: Continuous Delivery with CDK-Powered Apps Chapter 6: Testing and Troubleshooting AWS CDK Applications Part 3: Serverless Development with AWS CDK
Chapter 7: Serverless Application Development with AWS CDK Chapter 8: Streamlined Serverless Development Part 4: Advanced Architectural Concepts
Chapter 9: Indestructible Serverless Application Architecture (ISAA) Chapter 10: The Current CDK Landscape and Outlook Index Other Books You May Enjoy

Streamlined Serverless Development

Previously, we covered how to convert our code into a serverless architecture by replacing ECS, RDS, and VPC services with API Gateway, Lambda functions, and DynamoDB. But there’s still room for improvement in the development process.

Even though using serverless technologies simplifies maintenance after deployment, we can still optimize the way we work on the code locally.

In this chapter, we’ll be covering the following topics:

  • Common problems with serverless development
  • Running Lambda application logic locally and integrating it with a local express server
  • How to run AWS services locally using the LocalStack toolset

By the end of this chapter, you will be aware of the problems in serverless-oriented development, and your code will have been streamlined to the point where you will be able to develop infrastructure as code (IaC) and test it locally before deploying it to AWS.

Technical requirements

The source code for this chapter can be found at https://github.com/PacktPublishing/AWS-CDK-in-Practice/tree/main/chapter-8-streamlined-serverless-development.

The Code in Action video for this chapter can be viewed at: https://packt.link/06ppp.

Since it continues from the previous chapter, the directory structure is the same, with a few modifications to the services that we will explore in this chapter.

Configuring the project

Open the chapter code in your editor of choice. Just like in the previous chapters, we have divided up the code into the infrastructure, server, and web directories.

First, let’s go ahead and install all the dependencies for our infrastructure and server folders. Go to the infrastructure directory and in the terminal, run the following:

$ yarn

Then, back in the server directory, run the following command:

$ yarn

This will install all the project’s dependencies.

With that sorted out, let’s move on.

Common problems with serverless development

Developing serverless applications has become a popular and useful tool in the tech industry, promising to simplify the deployment and creation of applications. However, as with any new technology, there are always a few bumps in the road.

One of the most common issues is a lack of visibility into the underlying infrastructure. Unlike traditional server-based applications, where the servers and their configurations are visible and accessible, serverless applications abstract these away, making it more difficult to identify and solve issues when they arise.

Another problem is the lack of control over the environment. As serverless technologies are designed to abstract away the underlying infrastructure, developers are often limited in the amount of control they have over the environment in which their applications run. This makes it challenging to implement certain types of functionality, such as fine-grained access control or data storage...

Running Lambda application logic locally

You might have noticed that since we changed the infrastructure code to serverless, we haven’t touched the server’s folder code. You might be wondering if we are still going to use it. The answer is yes: we are going to use it for local development in the same way we were using it when we had RDS, but we will have to make some changes to make it work with DynamoDB. Additionally, we will import the same code that is running inside the deployed Lambda function into the local server and run it in their POST and GET routes. So, let’s get started.

You can find all the code discussed here in the server/src/index.ts file.

The first thing we need to do is import the code used in the Lambda integration of API Gateway for POST and GET endpoints:

import { handler as PostHandler } from '../../infrastructure/lib/constructs/Lambda/post/lambda';
import { handler as GetHandler } from '../../infrastructure/lib/constructs...

Using LocalStack to simulate AWS services

Remember when we said that the lack of a proper local development environment is a bit of a pain when developing serverless applications? To fix that, instead of deploying our code to AWS every time we want to test it, we can use LocalStack to run it locally. LocalStack allows us to mimic the functionality of AWS services, such as DynamoDB and S3, on our own machine. This way, we can test and develop our cloud and serverless apps offline.

Installing LocalStack

To use LocalStack, we first need to install its command-line interface (CLI). The following is a detailed set of instructions:

  1. To be able to install the CLI, you need to have the following installed on your computer:
    • Python (3.7 up to 3.10)
    • Pip (Python package manager)
    • Docker
  2. Once you have those set up, you can proceed to your terminal and run the following command:
    $ python3 -m pip install localstack
  3. If everything goes well, you should see this prompt upon the command...

Limitations of LocalStack

We dislike it when a solution to a certain problem is proposed without covering the limitations of that solution. All of that is left for the developers to find out themselves (looking at you, AWS docs) through trial and error, potentially wasting hours or days on a solution that might not be the right one for them.

We’ve presented LocalStack as a silver bullet for local serverless development, but it too comes with limitations that you might find annoying or impossible to work with. In our company, we use LocalStack on some projects and not on others, mainly depending on the AWS services we are using.

One of the main limitations is that LocalStack may not fully replicate the behavior of some AWS services. For example, the behavior of the S3 service in LocalStack may differ from that of AWS’s actual S3 service, which can lead to unexpected errors or behavior when the code is deployed to production. Additionally, not all AWS services are...

Summary

In this chapter, we discussed how to streamline serverless development by using a local express server and LocalStack for a local cloud environment. We provided step-by-step instructions for installing and running LocalStack and cdklocal, configuring the DynamoDB endpoint to point to LocalStack, and importing the same function used in the deployed Lambda function to the local server. Additionally, we showed how to run commands and make requests to the local development server to test the code’s functionality. The overall objective of this process is to enable the local testing and development of AWS cloud infrastructure without incurring costs or impacting production resources. In the next chapter, we will introduce the concept of indestructible serverless application architecture (ISAA), a design pattern for building serverless applications that are highly resilient and can scale infinitely.

lock icon The rest of the chapter is locked
You have been reading a chapter from
AWS CDK in Practice
Published in: Jun 2023 Publisher: Packt ISBN-13: 9781801812399
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}