Top 10 Serverless Best Practices
Tags: serverless, aws, awslambda
Published: 2019-10-22 10:07:46

The [Serverless Framework](https://serverless.com/cli/) is one of the oldest (and still going strong!) deployment frameworks around for serverless applications. It’s my framework of choice and enjoys a great deal of support from the community in terms of contributions as well as plugins. In fact, its ecosystem of plugins is one of its greatest strengths compared with other deployment frameworks.

The Serverless Framework gives you a number of best practices out of the box, such as:

* Enforcing a consistent naming convention.
* Generating the minimum IAM permissions to other services so they can invoke your Lambda functions.
* Supporting the use of SSM parameter store for storing sensitive data securely with `SecureStrings`.

But there are still an awful lot of things for us to do to ensure our applications are secure and robust. So here are my top 10 best practices that you should adopt when working with the Serverless Framework.

# 1. No wildcards in IAM role statements

Serverless applications can be more secure than their container and VM counterparts. But, we still need to do our part and make sure we don’t overprovision our functions with access.

We should follow [the principle of least privilege](https://en.wikipedia.org/wiki/Principle_of_least_privilege) and grant our functions the minimal amount of access they need. And that means granting permissions to perform **specific operations** against **specific resources**.

![](https://lh3.googleusercontent.com/TWW3_Z1GYymVanvPfoSwxsrmu4H5lAAPb87GtUgITOUld-qzcKsU8ynfAHklOBP4w_tB9Hia0ZLcEb-nmpZPp4hIzA79P8N4BXRKzePrvRCICpsLp_ZR4Nw8Cys4uLh9ZbLoFzfz)

# 2. One IAM role per function

By default, the Serverless Framework uses a shared role for all the functions in the `serverless.yml`. This too violates the principle of least privilege as functions would gain unnecessary access through the shared role.

Instead, you should use the [serverless-iam-roles-per-function](https://www.npmjs.com/package/serverless-iam-roles-per-function) plugin and define IAM roles for each function.

# 3. Configure DLQ for async functions

When you define functions that are invoked by async event sources (see [here](https://docs.aws.amazon.com/lambda/latest/dg/lambda-services.html)), you should configure a separate DLQ for each function. Services that invoke Lambda functions asynchronously provide built-in retry with exponential delay. However, if the function errors persistently then the invocation events can be lost forever if you haven’t configured a DLQ.

# 4. Configure framework version range

If you are using an instance of the Serverless Framework that is installed on your machine (or on the CI server), then you should add the `frameworkVersion` property to the `serverless.yml`.

`frameworkVersion: ‘>=1.0.0 <2.0.0’`

This checks that you’re using a version of the Serverless Framework that is compatible with the current `serverless.yml`.

# 5. Configure CloudFormation deploy role

The Serverless Framework lets you pass a dedicated deployer role to CloudFormation to run deployments with.

`cfnRole: arn:aws:iam::XXXXXX:role/role`

You can use a different role for each project, or each team and apply the principle of least privilege to the deployment pipeline. This approach also combines well with [attribute-based access control](https://aws.amazon.com/blogs/security/simplify-granting-access-to-your-aws-resources-by-using-tags-on-aws-iam-users-and-roles/) (ABAC). For example, the deployer role for team A can be restricted to create and delete (for rollbacks) DynamoDB tables with the tag “Team=TeamA”.

# 6. Configure stack tags

Tags help you find resources easier and you can even use them to track your AWS spending. By default, the Serverless Framework inserts the STAGE tag to the generated CloudFormation template.

However, you should consider adding other custom tags using the `stackTags` property. Common tags to consider include Author, Team, Feature, Cost Center and Region. Unfortunately, CloudFormation doesn’t propagate its tags to some resources such as CloudWatch log groups. You can address this limitation by deploying the [propagate-cfn-tags SAR app](https://github.com/lumigo-io/SAR-Propagate-CFN-Tags) to your account.

# 7. When using VPCs, configure at least 2 subnets

When configuring your functions with VPC access, make sure you configure at least 2 dedicated subnets for Lambda. This mitigates the risk of IP exhaustion in the subnet and limits the blast radius to Lambda functions.

However, given the recent announcement of [improved VPC networking for Lambda](https://aws.amazon.com/blogs/compute/announcing-improved-vpc-networking-for-aws-lambda-functions/), this would no longer be necessary once the improvement has been rolled out to your account.

# 8. Use Fn::Sub instead of Fn::Join for clarity

Most of the time `Fn::Join` (or `!Join`) is used to construct ARNs and URLs out of different parts. In such cases, you should use `Fn::Sub` (or the `!Sub` shorthand) instead, which makes for much more readable code.

![](https://lh4.googleusercontent.com/ixlZixdRrvTQbqvXasjrWqTBhrSd7WY1ylbFq3afWIjYO3_HSY-h7DQ1cXczNRbuariKG79kzUqfleQB6WWZn6QyA_oLwgkAT0QznubZhLz66b3vpil7tc37Z63mpq7udZFIkxQH)

![](https://lh5.googleusercontent.com/7KOOVZseRf_8_LW9rptOztm_EZWAruqfj6M-raR4z4FImN2BGUSG1PS7NwOLFazZyvaH55N63zuDWMh3mNEGR6bGRoPwHDThXngfvO9QQj1fDTLgU8BTZl2OPsvTbpnpm1duksiE)

See more examples [here](https://theburningmonk.com/2019/05/cloudformation-protip-use-fnsub-instead-of-fnjoin/).

# 9. For Node.js functions, use Webpack to improve cold start and reduce package size

For Node.js functions, the `Initialization` time is a big part of the cold start time and can vary greatly depending on how many dependencies you have.

![](https://lh6.googleusercontent.com/k0L50qHcZh-ioNIqdSy2T-MN6iyPX441n3l2CnwO3qNanwgaL5K-WsfKi3Yg8OzRe71XW5iwAo4i7ujJDBjSzN3Q8C2_8uYIT_NqRB9h9tL4sFrCxLdvMTAjnrGu6lyw9U9lPYpH)

Webpack can help reduce `Initialization` time significantly. You should use the [serverless-webpack](https://github.com/serverless-heaven/serverless-webpack) plugin to run webpack automatically every time you deploy your code.

See [here](https://theburningmonk.com/2019/03/just-how-expensive-is-the-full-aws-sdk/) for more details.

# 10. Break large serverless.yml into multiple files

The Serverless Framework lets you [reference external YML and JSON files](https://serverless.com/framework/docs/providers/aws/guide/variables/#reference-variables-in-other-files). When your `serverless.yml` gets too big, you should break it up into smaller files and reference them back in the main `serverless.yml`. This helps you keep the `serverless.yml` file manageable.

And that’s it! 10 things you should be doing when working with the Serverless Framework. Let us know on Twitter or LinkedIn if you don’t agree with our selection, or maybe we have missed any practices that you think everyone should adopt.

The article, [Top 10 Serverless Best Practices](https://www.datree.io/resources/serverless-best-practices), first appeared on the
website of [Datree](https://datree.io/), the policy enforcement platform.