If you are developing a Serverless web app with React and AWS technologies, how would you create a test/deve environment?

I am quite new to this topic, but let’s say I am developing a commercial web application based on the serverless architecture and React (React, AWS Lambda, DynamoDB and API Gateway).

If I am developing this web app, I imagine it will be a pain to do the development against a real AWS configuration (beside costs and the need to maintain a second AWS account). I wondered if there is any recommended practice when it comes to develop and test a serverless web application. Like, is there a way to replicate AWS in a local environment, where you could run your tests as well before you deploy?

If you know a book that talks about best practices when it comes to this, please point me to that book!

Thanks!

There’s Localstack - https://localstack.cloud/ - for local development. It has lots of mock endpoints for most services.

For a ‘living’ development environment, carve out a separate IP space (VPC, Private/Public subnets etc) and DNS zone, then duplicate the architecture components within the same account. You can set up a totally separate account if you want but both will work.

I would recommend terraform. You can write your infrastructure using terraform so that it is in code. Make it so that the resource names (and tags) are different based on the environment. Then you can create as many dev/stg/prd environments as you need with little effort. The great thing about serverless apps is that the infrastructure is basically free until they start to generate traffic.

I’ll admit It’s not super efficient if you’re wanting to test on your local machine, because instead, for every env, you will need to build and deploy the code to the lambas and also deploy the API documentation (like a swagger) to API gateway. But I would argue it’s better to do it this way because you’ll have less discrepancies across environments which protects production.

Source: Am devops engineer and this very thing is the main focus of my career.

As was already mentioned, localstack can emulate several of these components. But it sounds like you are relatively new to AWS and might be trying to do too much at once.

You can start by building your react app and api in a container. For dynamodb, there are containers you can use locally (I think one is called dynalite).

Lambda and API gateway are more “deployment concerns” - meaning that your app can be built and tested to a large extent without worrying about them until later. You can also avoid the API Gateway by using an ALB, or even lightsail, both are probably easier to get started with, but might cost slightly more if you expect very low traffic to your app, compared to the API gateway.

So, my suggestion: use containers locally, and worry about setting up the AWS networking/routing/certs separately. When you do that, using terraform, or cloudformation to configure it will allow you to create a “staging” deployment that matches a “production” deployment so you can verify any deployment-specific setups are correct.

I imagine it will be a pain to do the development against a real AWS configuration

I’d argue the contrary. The real pain is trying to emulate a serverless application locally. Why try to emulate AWS when you can just operate out of AWS?

beside costs and the need to maintain a second AWS account

Costs are a factor, but for a serverless application, they should be minimal. If you can’t afford a dev version of a serverless application, than you can’t afford the prod version as well. Maintaining a second AWS account also shouldn’t be an issue if you’re set up correctly. Again, if maintaining a second AWS account (or an nth account) is too much of a pain, then a serverless application will be too much of a pain, too.

I wondered if there is any recommended practice when it comes to develop and test a serverless web application.

If your application is set up well, then you’ll have segregated the serverless portions of the app from the business logic of the app. From there, you should be able to handle the business logic locally (i.e., using unit tests, etc.) and handle serverless development in the cloud. There are different approaches for this and I don’t think any one approach is the recommended standard, but I recommend this approach.

Like, is there a way to replicate AWS in a local environment, where you could run your tests as well before you deploy?

Others have mentioned Localstack and other ways of emulating AWS services. These can work and are certainly important in some settings, but odds are if you’re asking these questions, then this approach isn’t very appropriate. You’ll be spending more time trying to get your local machine to act like AWS then actually getting your application to work in AWS.

Basically, I think you need to take a step back and recognize that you shouldn’t be trying to shoehorn a serverless app development process into something that looks more traditional. You either need to move away from serverless into something more conceptually simple or embrace serverless and try to work out of AWS directly. It’s the only feasible way of addressing classic issue of “but it worked on my machine” when the production “machine” fundamentally doesn’t resemble your machine.

And like I said, emulating AWS locally is an option and is something that is done regularly. But I’m telling you, on a personal level, that it’s more work than it seems, and if you’re a small (or one man) team, it ain’t worth it.

You can run Lambda, APIG, and DynamoDB locally in Docker / SAM

Combine with moto_server and you can have local S3 as well

Everyone here mentioning local stack is wrong. It’s Lambda - it’s cloud native. You should run it in AWS and use real resources. Sandbox accounts for your devs to use as ‘local’

You could write unit tests which run locally for your code and then write integration tests which run against a test environment. This way, you’re testing the full package. You could go a step further and add e2e tests if you needed/wanted.

Use AWS organizations to separate environments. The cost for a simple app is minimal. If your very new to AWS you are better off learning on live infrastructure.Also, use AWS CDK for deployments.

I think AWS Amplify could help here. You can create environments automatically and you get everything you need for a serverless webapp. I used it to create an app with react.

It also helps you to connect a dynamoDB database, cares for hosting and you can add lamba functions. Also it helps to connect your app to Cognito. It should be no problem to add API gateway.

It improves a lot lately and I can totally recommend. Especially for beginners.

Create a VPC with multi AZ and create a CI/CD pipeline with Lambda as the target deploymen on each AZ for fault tolerance t. Use Canary as the deployment method.

SAM CLI

https://serverless-stack.com/

It will be difficult without many years of programming, webdev, devops, and AWS.

Ive had really pleasant results with Serverless and the offline plugin

This isn’t really about local development. Typically you will decide on your framework that you will use for serverless and this will often have helpers and best practices built in for testing. Some common frameworks have been mentioned in the comments:

Serverless

serverless-stack

Chalice

SAM

Each one of these has local testing mechanisms built in based on their own best practices. Often for Serverless you only run your unit tests against a local and everything else talks to a dev environment of the real system, but a lot of this depends on what your entire stack looks like. The nice thing about that is you are very unlikely to run into the “works on my machine” problem. Most of these frameworks are designed to be run in CI/CD workflows as well.

Personally I would not recommend localstack. It is basically hacked together bits of various parts of AWS, and not the actual AWS stack you are going to be running on, therefore you run the risk of works in the custom dev environment but not in the real world.

This is the answer OP. Unit tests should be testing business logic and are infrastructure independent. When you have solid unit tests you can write a suite of E2E tests that execute in your dev/test environment in aws.

Have a look at using sam sync. It syncs your local code with a remote stack. This lets you develop lambda locally and run them either remotely or locally against actual aws resources.

The main benefit of serverless is the fact the infrastructure costs correlate to your traffic #s. In dev, you’ll generate traffic on your stuff equivalent to less than a percent of a percent compared to prod traffic, so the cost to standup dev infra is usually pennies/month.

As for recommended practice: use a single Infrastructure-as-code technology (CloudFormation, Terraform, CDK, AWS SAM, Serverless, etc.), make a single template that takes in an environment identifier variable and add that env identifier to each resource you create in the template. That way, you can spin up as many environments as you want - prod to different regions, a QA environment for each QA engineer on your team, a dev environment for each member.

Hell, if you get real fancy with whatever work management platform your use (Jira or otherwise), you can spin up an environment per ticket and then destroy the environment once the work branch has been merged.

At work currently, we make our environment identifier out of a combination of someone’s employment id, the level of deployment they are targetting (dev,qa,prod) and we throw in the AWS region name for good measure.

If you want an example, I can PM you some.

Thank you, both @skilledpigeon and @snowman4415, this is useful to know. When you say E2E you mean a combo of local unit-tests for the front-end and then some other tests to check the API request/response?

Thank you, emch2. So, you mean with AWS Amplify you could “deploy” a test web app to a different URL, connected to a different, test, DynamoDB?

Thank you for your input.