Search code examples
aws-lambdamicroservicesaws-cloudformationserverlessaws-sam

Breaking down a large AWS SAM template file into smaller, more manageable files


I am working on a large-scale serverless application with AWS SAM, and the template file has become so large and complex that it's becoming difficult to manage. I want to break it down into smaller, more manageable files, but I'm not sure of the best way to do this.

I've heard that nested stacks might be an option, but I'm not sure how to implement them or if there are other ways to achieve this.

What is the best approach for breaking down a large AWS SAM template file into smaller, more manageable files? Are there any best practices or recommended patterns for doing this? And how can I maintain the overall structure and organization of the application while doing so?

Any advice or examples on how to break down AWS SAM templates for large scale applications would be greatly appreciated. Thank you!

Here is an example of how I managed to do it in serverless.yml:

serverless.yml

service: example
frameworkVersion: "3"
useDotenv: true

functions:

    ${file(./services/authentication/functions.yml)}
    ${file(./services/posts/functions.yml)}

functions.yml


get-example:
  image:
    name: latest
    command: example_service/app.lambda_handler
    entryPoint:
      - "/lambda-entrypoint.sh"
  events:
    - http:
        path: example/get
        method: get
        cors: true
        authorizer: authorizer

Is there a way to achieve this kind of split for AWS SAM templates? I have looked into using nested stacks, but that doesn't seem like a very elegant solution.

I have also used serverless-compose to manage my serverless.yml files in a more modular way. Is there a similar tool or technique that can be used with AWS SAM to achieve a similar result?


Solution

  • Finally i found that Nested Stacks are a very elegant way to do it. So after searching and watching AWS YT. The recommened way to do it is using NestedStack.

    Please refer to this github repo https://github.com/aws-samples/sam-accelerate-nested-stacks-demo

    This repo is actually a very nice that it placed a really well documented way to do a large scale application but one limitation is only you can manage 200 Resource.

    I didn't reach 200 resource at the time of writting so it is safe for me to do it the way the above repo did.