CDK - everything in one stack?

Starting to use AWS in my company via automated CICD pipelines. I will be using the CDK to provision my application.

I will have:

  • API gateway
  • lambda authorizer
  • backend lambdas which interact with the database
  • postgres aurora database
  • kinesis

Should I have everything in one stack? Or should I have my database in a different separate deployable stack?

How do you insert tables and columns etc into you database in each environment, is there some sort of migration tool?

Thanks

I’d recommend one stack for this. Maximum would be two - one for your application logic and another for your infrastructure

Edit: Since I’m getting downvotes with zero actual counter arguments, I want to point out that you’ve been given some absolutely terrible advice in this thread:

  1. “Breakup to multiple stacks to avoid reaching the limit”: you’ll reach the limit when your stack is a monolithic monstrosity (we are talking over 200 resources) spanning multiple bounded contexts, and should’ve been broken into separate microservices a long time ago. But the motivation for this breakdown isn’t to avoid reaching the limit - it’s to have a scalable architecture. You should separate stacks the same way you separate microservices: things should be in different stacks when they serve different domains, are loosely coupled, and can be maintained independently and by different people. Not because in some point in the future you might reach some limit. You will never reach those limits if your architecture makes sense, but you can easily create a spaghetti architecture that makes no sense if the stack limit is what’s motivating you. At this moment it seems like you have a small app with a very small number of strongly coupled components. There is no reason at all to seperate them because of some imaginary limit in a future.
  2. “Move the DB to a different stack so that it can be shared between microservices”: First of all, if you currently have a single small application and no actual microservices, the entire thought process is premature optimization and a waste of time. Focus on simple solutions that work. Secondly, sharing the same DB stack between different microservices is absolutely terrible idea: this stack will have no clear owner, and different teams will step over each other’s toes all the time. This is a recipe for disaster: the deployment of one team to this shared stack can completely cripple who knows how many independent microservices that have nothing to do with whatever that team is doing, and teams will be significantly limited in their ability to iterate independently.
  3. “If one resource fails - everything rolls back” - I’m not sure what is meant here. Yes - if one resource in the same service fails, you want everything to be rolled back to the previous working state, because the changes in a deployment are normally related to, and depend on, one another. If you create a new reader endpoint for your Aurora, and deploy it together with code changes to your lambdas that tell them to access this endpoint - if the former fails you want the latter to fail as well, otherwise you will break your entire application. This is a feature, not a bug, and has nothing to do with data loss or outages. If anything - it protects you against outages.

Original reply:

Should definitely be one stack IMO as it’s a single bounded context with strong coupling between the components. There is nothing to be gained by putting the DB in a separate stack.

There are multiple migration tools, depending on the language you are using - either google or go to the subreddit of that language, and I’m sure you’ll find multiple copies of the same question there.

Also a minor warning - although it was several months ago, but I found CDK to be painful to use for anything that’s beyond basic. It has great potential and is probably the future of AWS IaT, but at this point I’m not sure that it’s mature enough and worth the time compared to other tools like Serverless Framework or SAM.

We started off on one stack with database and lamda but ended up moving to two. It was easier to manage the database separately particularly when it came to backups and we moved from a plain RDS cluster to Aurora.
Also it allowed us to share the database cluster among microservices when we started adding more.
Overall I like the idea of keeping data and stateless infrastructure separate.
We also have a separate stack for VPCs and internet gateways which again allows us to share these among microservices.

Don’t do one stack - if one resource fails, everything rolls back and you may experience data loss, outages etc.

Split your compute from storage. So, without knowing exactly what your dependencies look like you might need something like:

  1. One stack for your database, and another stack for kinesis.
  2. Another for API gateway and any immediate Lambda that might be triggered from it, or not if you have multiple lambda’s being trigged from it.
  3. A stack for each DNS record pointing to the API gateway.
  4. A stack per backend lambda that is doing backend work (presumably Kinesis based triggering). If you’re not latency critical, stick to cron jobbing them or using Firehose, as you’ll be invoking every small handfull of Kinesis messages which can be inefficient.

Ignore the 20 stack region/account limit. Open a support ticket, and request 100, it will be done generally without a fuss. Use outputs and exports to expose things like names (from Fn::GetAtt, Ref etc) cross-stack.

FWIW I totally agree with the single stack idea. Anything else is just time sinking premature generalization and over complication. Curious though about your notion that CDK wasn’t mature enough to handle complex systems, are you talking more about the infra capabilities or the deployment and operational tooling? I’ve been have a good time with CDK so far, with decently complex applications. Granted we don’t do too much with the deployment side of things.

I won’t downvote your but i will tell your why i don’t like your answer.

If we’re using AWS already then i think we don’t care about pre-mature optimization at some point.

also, cdk is for building components to reuse in larger constructs. you probably had trouble because one stack when the construct is an advantage. classes are re-usable and testable in code. with out cfn-lint. i’d still recommend both.

you do want your stacks split up by their domain. I’m deploying permissions buckets and db way less than I’m building apis and service pipelines.

also, you’re getting caught up on persistence and mucking that up, build validators if you’re using a schema less data store to enforce rigor. or rely on your dba to build out the relational db schema.

If we’re using AWS already then i think we don’t care about pre-mature optimization at some point.

I don’t know how to make sense of this sentence. What does using AWS, or any cloud provider, has to do with not caring about premature optimization? Those are completely unrelated thing.

also, cdk is for building components to reuse in larger constructs. you probably had trouble because one stack when the construct is an advantage. classes are re-usable and testable in code. with out cfn-lint. i’d still recommend both.

None of this has anything to do with stack granularity. I really don’t understand what you were trying to say here.

I’m deploying permissions buckets and db way less than I’m building apis and service pipelines.

A “domain” isn’t a grouping of resources by deployment frequency. That some resources change less frequently than others is orthogonal to whether they should be deployed together or not. It’s completely unclear to me what you are gaining by separating stacks by the change frequency of resources, however you are adding a lot of unnecessary complexity, and it will become very apparent as the number of microservices you have to maintain grows.

also, you’re getting caught up on persistence and mucking that up, build validators if you’re using a schema less data store to enforce rigor. or rely on your dba to build out the relational db schema.

Not sure what argument of mine you are referring to precisely, seems rather unrelated to anything I’ve written.

Honestly I couldn’t understand much of your reply - many of the sentences don’t seem coherent / written in correct english, so It’s difficult to reply properly.

I break my stacks into layers based on their lifecycle. I put the resources that change more frequently in a stack, and those that change less in another. This keeps my deploy times low and I can iterate quickly. I’ve had large stacks end up in a red state that could only be deleted, which really sucked for DDB or other datalayer resources. I am also moving away from using Cloudformation Exports for everything and only where tight coupling is needed. Trying to leverage SSM Parameters instead.

Multiple. The resources limit is 200 which can be pretty.easy to hit when you’re doing DB tables. Add in backups, encryption keys, resource alarms (things you’re want to template to be standard on every table or DB and it can add up really quickly.

Also, make sure you don’t end up with a bootstrapping problem when you do it. I.e. the database stack has a dependant resource in the service stack and the service is dependant on the DB. It’s.easu to do as you build up over time so keep an eye on it. When you have to redeploy or make a new stack in a new region, you’ll be glad you were watching that.

you’ve never run large infrastructure in multiple accounts so i wouldn’t expect you to understand.

Yes I have - my current company successfully maintains hundreds of production stacks across multiple teams and accounts. Stacks are defined and separated by bounded contexts, not by the change frequency or type of resources. If you do not understand what this means - please read a book on DDD or microservices. I can guarantee that, with high probability, past a certain scale, doing some of the things being advocated here (can’t say if advocated by you specifically, because you haven’t presented any coherent arguments for anything) would significantly hinder both system stability and dev velocity.

I’ve been looking into getting outputs working, can you outline your approach / code samples.

How do you use SSM to pass parameters between stacks, sounds very interesting!

I would also recommend cloudmap as being a great alternative to SSM

Thanks for an the replies, lots to think about and maybe there isn’t a right answer. I feel like the database might be better as a separate deployable. Since I may want to manage it separately. I’m still not sure how to manage changes to the database, I will need to use some sort of migration tool that runs when the database is deployed - to create any new tables etc. I’m not sure how to do this in RDS or what tools I can use locally.

I would have proposed something similar with three stacks, but I see you also separate DNS records, and I guess that it might be a good idea when you need to ensure that you change those fast and without any trouble.
For me, that’s the best answer in this thread so far.

The application is small today, but if it grows big later on, and you want to keep adding more resources you will be forced to create another stack because of the 200 resources hard limit per stack. If you want to rearrange the resources at that point to keep the stacks organised, then you’ll have to delete and re-create the stack, may be including database, which is not ideal. In my opinion, it is best to have some minimal organisation up front - networking stack, database stack, and so on.

Found this in the docs: https://github.com/aws/aws-cdk/blob/master/design/aws-guidelines.md#imports

Essentially it says you should implement exports (which applies outputs to cloudformation template ) and imports (which applies parameters to cloudformation template)

All of our infrastructure code is raw .json cloudformation and split into separate stacks, e.g. lambda, api, database, kinesis, network etc - we have a parameters.json file which receives values from stack outputs and injects values using parameters-

at the moment I’ve been rewriting our cf into cdk and would really like to nail down an approach along with code samples, I’ll share anything I put together on GitHub

I wrote a cloudformation custom resource that takes stack outputs and turns them into parameters. The path is /{stack name}/{output name}. Then I use the SSM Parameter type in my other stacks to import these values.
https://aws.amazon.com/blogs/mt/integrating-aws-cloudformation-with-aws-systems-manager-parameter-store/

I’m doing it this way based on an experience I had where we had our VPC, subnets, and all network level stuff defined in a stack. These ids were exports that hundreds of other stacks were importing. We decided we wanted to enable more subnets, but the import/export prevented us from just enabling them without major updates to our automation and hardcoded values in our templates. If two stacks should be coupled, I use exports. If there are AWS controls keeping me from breaking things across stacks (like, you can’t delete a subnet that’s in use), then I use the SSM pattern instead.