Looking for Senior AWS Serverless Architects & Engineers?
Let's TalkLet’s learn how to build a REST API with Stackery
Recently, the Serverless Guru team met with some of the folks at Stackery to do a working session on building Serverless applications with the Stackery platform. Thanks to that meeting, we were inspired to build a common somewhat complex REST API with Stackery and share it with all of you 😃.
Who is Stackery?
Stackery is a Portland-based startup who has been actively building a platform which allows users to build complex Serverless architecture through a visual user interface. Stackery accelerates the infrastructure development process by enabling developers to skip having to manually write out the hundreds of lines of IAC (Infrastructure as Code) which has typically been done by hand.
The Stackery platform supports AWS (Amazon Web Services) and allows users to skip writing AWS Cloudformation templates natively or through an automation framework which makes it slightly easier such as AWS SAM or the Serverless Framework.
Why should you care about Stackery?
If like us at Serverless Guru, your team uses AWS and AWS Cloudformation to automate the construction and deployment of your applications, you should pay attention to Stackery. During our meeting with Stackery, we were able to see a couple of features which were only a couple of clicks in the Stackery console but would have taken days to set up for even experienced cloud developers who work with AWS and Cloudformation daily.
The infrastructure that we built while sitting with some of the Stackery team was a very common setup. The goal was to create a REST API with a private database spun up using AWS RDS (Relational Database Service) inside a VPC (Virtual Private Cloud) alongside a lambda function which would sit in the same VPC. The lambda functions would need to be triggered by AWS API Gateway and have access to the private RDS instance. This combination can sound simple on the surface. However, when you are writing the Cloudformation natively or through a framework you will easily hit 200+ lines of Cloudformation.
The worst part is you will naturally be copying that Cloudformation from an example online which is 90% what you need for your use case. However, that last 10% is nothing to scoff at and can easily turn into multiple hours of debugging.
Inspired by that meeting we decided to create a similar architecture. In the span of about 5 minutes, we were able to spin up the architecture and commit the template to GitHub. A nice feature of Stackery is its ability to create a GitHub repository under your GitHub account and commit directly from the Stackery interface. Which creates a seamless workflow when brainstorming ideas and trying to roll out a quick MVP.
Let’s build a REST API with Stackery
As you can see in the image above, we’ve created an API with 10 paths and methods, two AWS Lambda functions inside a VPC and an RDS database inside the same VPC.
Let’s walk through it.
Create a Stack
At this point, we are assuming that you’ve already created a Stackery account and linked your GitHub account.
After you create the stack you should see a blank slate.
Now let’s add some resources. First up, we are going to search for “vpc”. We can then drag and drop that into the grid.
Next an RDS database.
Then a lambda function. In our case, we are selecting a regular function versus an edge function.
Perfect you should now have something that looks like this.
Let’s drag the Lambda function and RDS database inside the VPC. Then connect the Lambda function to the RDS database.
Alright, let’s stop and review what is going on. On the surface, it looks like we are creating an architecture diagram to export as a png. However, the entire time that we’ve been dragging these AWS resources around. Stackery has automatically been generating an actual deployable Cloudformation template. Which also creates the connections between the resources, meaning the AWS IAM policies and AWS IAM roles are created as well. We will circle back to this a bit later.
Let’s keep going and add an API with AWS API Gateway. We can name it “Retail API”.
Then let’s create some routes for our products.
And some more routes for our customers.
Let’s also flip the switch to “enable CORS” as this API would be used in a web application.
Now let’s save the API. In our case, we went ahead and created one additional Lambda function and named those functions Products and Customers. We then connected the various API routes to the corresponding Lambda functions and finally connected both Lambda functions to the RDS database.
The end result, we have reached our goal of creating a complex REST API with private Lambda functions and a private RDS database. All with a couple of button clicks. Now let’s take a look at the template which was created.
If you pay attention to the line count. We are sitting at 479 lines. Every single line was generated automatically by Stackery and the connections between the services were also done automatically by Stackery. 🔥
Now let’s commit back to our GitHub repository. A simple button click.
We can also write a custom commit message.
And view the differences with our latest changes.
Once we hit Commit and Push, we can switch over to GitHub and see everything loaded.
We can also see a folder called src was created and a file called template.yaml. The template.yamlfile is the same as what we saw in the Stackery interface. The src folder was created by Stackery based on our choices in the Stackery interface. Stackery doesn’t generate any code for the Lambda, but they did create some boilerplate for us to work from.
Now you could easily jump into the index.js file and start hacking away with your actual API and business logic.
Conclusion
The great part about Cloudformation is that when it works, it will continue to work. However, getting to that point can sometimes be easier said than done. The time commitment to work out the kinks in a Cloudformation template and build out a Cloudformation template to match your entire hand-drawn architecture can span days if not weeks. Depending on the complexity and experience on the team.
As you saw above, with minimal effort, we were able to create a REST API with a private RDS database, private lambda functions, multiple Lambda functions and a VPC with a whole slew of unseen networking resources. All through drag-n-drop inside the Stackery interface. As we made changes in the Stackery interface such as dragging the Lambda resource into the VPC resource. Stackery automagically generated a Cloudformation template with all the supporting networking resources 🔥.
With all of that infrastructure squared away, we can now spend all of our time writing the code required to make our application work.