Forget about the "CDK vs. Amplify" mindset: why are AWS CDK and Amplify good complements to each other?
CDK and amplify.js together to build maintainable fullstack apps
tl:dr; if you build fullstack app on AWS, you probably have encountered some articles about “CDK vs. Amplify”, this post will demonstrate why you may want to forget about the “vs.” mindset and start thinking how to use them together
AWS CDK and AWS Amplify, what are they?
First, what is Amplify?
We wrote a previous post to introduce what AWS Amplify really is (for absolute beginners)
amplify.js is the client library for front-end make API calls to AWS backend services;
amplify cli is the Infrastructure as Code (IaC) building blocks for you to set up and configure the AWS backend infrastructures;
NOTE: there is another building block under the amplify umbrella, ‘amplify console’, which is mainly for building the CI/CD pipeline —feel free to read it; but I feel we can “have more control” using Github Actions or Gitlab CI to accomplish this piece of DevOps job.
Secondly, what is CDK?
AWS CDK is
….framework to define your cloud application resources using familiar programming languages.
it is infrastructure as code (IaC) — same as cloudformation to AWS
it’s written in your familiar language, such as Typescript / Python — for engineers who don’t like lengthy YAML / JSON configuration files, this is a bless
Where do Amplify and CDK overlap and complement to each other?
Overlap: both amplify cli and CDK fulfill our needs of IaC
Complement: if we use CDK as our IaC solution and use amplify js library as our front-end library to communicate the IaC defined backend, then everything is in our familiar language.
A concrete example they can complement each other well
Please read this github issue discussion. In summary, the requested feature is about file uploads (think that someone wants to build a mini Instagram that they upload a photo and share with friends)
CDK could make hair-pulling Amplify-CLI based infrastructure process well-codified — it’s very similar to how the typical Cloudfront + S3 setup should be configured (though it needs to change the IAM access control of the subfolders) — you can see that the IaC code structure is simple and easy to understand when you read the CDK code.
And then we can use Amplify JS library’s storage module to build the photo upload and photo viewing process.
NOTE: We did not have a full github project illustrate a mini-Instagram photo upload feature (because my limited bandwidth to write this weekly newsletter), but please feel free to ask questions if you do want to build some similar feature in this way.
We can leverage the good parts of amplify.js along with CDK to build a maintainable applications:
amplify.js gives the benefits of the easy-to-use client framework to communicate with AWS backends APIs — much easier than the aws sdk directly;
CDK give engineers the well-maintainable infrastructure definition code — to engineers, it could be much more attractive than it’s alternatives:
cloudformation scripts may be too verbose and not maintainable;
amplify CLI could make us feel not controllable at all
So, when you start thinking to build an app on AWS, maybe you could start combining the goodness of Amplify and CDK.