9 Writing an infrastructure DSL

 

This chapter covers

  • Grasping the ideas behind Infrastructure as Code
  • Parsing a custom syntax with structs and keywords
  • Thinking about parsing tradeoffs
  • Avoiding duplication by combining procedural and declarative macros
  • Calling asynchronous functions in a macro and creating cloud resources

Most of the macros we have created thus far work within Rust, generating new functions, adding methods to enums, etc. But we also know macros can do more than that. For example, they allow you to generate compile-time checked HTML (Yew), or they create infrastructure for your code based on annotations (Shuttle).

This chapter features a macro that boldly goes beyond the confines of Rust and creates infrastructure in Amazon Web Services (AWS). This is interesting because it allows us to manage both application and infrastructure logic in the same repository and the same language, which might make expanding and maintaining the code easier. And, hopefully, the example also offers inspiration to readers as to what else they might do with procedural macros. But first let’s take a step back to make sure we are all on the same page.

9.1 What is IaC? What is AWS?

9.2 How our DSL works

9.3 Parsing our input

9.3.1 Project setup and usage examples

9.3.2 Implementing the Parse trait for our structs

9.4 Two alternative parsing approaches

9.4.1 Using Punctuated with a custom struct

9.4.2 Using Punctuated with a custom enum and builder

9.5 Actually creating the services

9.6 The two AWS clients

9.7 Errors and declarative macros

9.8 The right kind of testing

9.9 From the real world

Exercises

Summary