You can become a serverless blackbelt. Enrol to my 4-week online workshop Production-Ready Serverless and gain hands-on experience building something from scratch using serverless technologies. At the end of the workshop, you should have a broader view of the challenges you will face as your serverless architecture matures and expands. You should also have a firm grasp on when serverless is a good fit for your system as well as common pitfalls you need to avoid. Sign up now and get 15% discount with the code yanprs15!
If you enjoy reading these exercises then please buy Crista’s book to support her work.
Style 16 – Introspective
This marks the start of a couple meta-programming related styles which also includes Reflective, Aspects and Plugins.
If you have come from a .Net or Java background then the subtle difference between introspection and reflection might not be obvious. This wikipedia article gives a pretty good explanation:
…type introspection is the ability of a program to examine the type or properties of an object at runtime…
Introspection should not be confused with reflection, which goes a step further and is the ability for a program to manipulate the values, meta-data, properties and/or functions of an object at runtime.
- The problem is decomposed using some form of abstraction (procedures, functions, objects, etc.)
- The abstractions have access to information about themselves and others, although they cannot modify that information
In her example, Crista used two of Python’s introspective capabilities:
- inspecting the stack to see who the caller is and only allow the function to be called from ‘extract_words’
- fetching the value of the argument passed into the function
both feel a bit contrived, especially 2.
I don’t know of any ways to do 1. in F#, you can walk the stackframe but it gives you filename, line number, method name, etc. but I didn’t find a way to get the calling function name in any usable form. Since I’m doing this exercise in a F# script might have complicated matters further, as I kept seeing things such as ‘FSI_0004’ as type names.
After giving this some thought, I think a more realistic example in our context here is to use objects to hold data as properties and use introspection to fetch their value.
So first, let’s define the two types which we’ll use introspection on:
We’ll use introspection to get at the DataStorage.Words and StopWordsFilter.StopWords properties. But, let’s do that with style and define the dynamic operator to do the heavy lifting for us:
and now we can have a filterWords function that:
- takes two arguments – dataStorage and filter
- dataStorage can be of any type with a string property called Words
- filter can be of any type with a Set<string> property called StopWords
this function has the signature of
‘a -> ‘b -> seq<string>
Perhaps here lies the strength and weakness of this approach:
- that it’s much more generic than if we had depended upon the concrete types, and would even work with types that I don’t control (and therefore can’t enforce any interfaces upon them)
- but the constraints on dataStorage and filter are now implicit and you’ll have to inspect my code to figure them out
a much better solution in F# would be to use statically resolved type parameters, but that’ll be out-of-style I think.
Next, we’ll add another function to count the frequency of the filtered words, nothing special here:
Finally, to string everything together:
You can find the source code for this exercise here.
Hi, I’m Yan. I’m an AWS Serverless Hero and I help companies go faster for less by adopting serverless technologies successfully.
Are you struggling with serverless or need guidance on best practices? Do you want someone to review your architecture and help you avoid costly mistakes down the line? Whatever the case, I’m here to help.
Skill up your serverless game with this hands-on workshop.
My 4-week Production-Ready Serverless online workshop is back!
This course takes you through building a production-ready serverless web application from testing, deployment, security, all the way through to observability. The motivation for this course is to give you hands-on experience building something with serverless technologies while giving you a broader view of the challenges you will face as the architecture matures and expands.
We will start at the basics and give you a firm introduction to Lambda and all the relevant concepts and service features (including the latest announcements in 2020). And then gradually ramping up and cover a wide array of topics such as API security, testing strategies, CI/CD, secret management, and operational best practices for monitoring and troubleshooting.
If you enrol now you can also get 15% OFF with the promo code “yanprs15”.
Check out my new podcast Real-World Serverless where I talk with engineers who are building amazing things with serverless technologies and discuss the real-world use cases and challenges they face. If you’re interested in what people are actually doing with serverless and what it’s really like to be working with serverless day-to-day, then this is the podcast for you.
Check out my new course, Learn you some Lambda best practice for great good! In this course, you will learn best practices for working with AWS Lambda in terms of performance, cost, security, scalability, resilience and observability. We will also cover latest features from re:Invent 2019 such as Provisioned Concurrency and Lambda Destinations. Enrol now and start learning!
Check out my video course, Complete Guide to AWS Step Functions. In this course, we’ll cover everything you need to know to use AWS Step Functions service effectively. There is something for everyone from beginners to more advanced users looking for design patterns and best practices. Enrol now and start learning!
Here is a complete list of all my posts on serverless and AWS Lambda. In the meantime, here are a few of my most popular blog posts.
- All you need to know about caching for serverless applications
- Lambda optimization tip – enable HTTP keep-alive
- You are wrong about serverless and vendor lock-in
- You are thinking about serverless costs all wrong
- Just how expensive is the full AWS SDK?
- Check-list for going live with API Gateway and Lambda
- How to choose the right API Gateway auth method
- CloudFormation protip: use !Sub instead of !Join
- AWS Lambda – should you have few monolithic functions or many single-purposed functions?
- Guys, we’re doing pagination wrong
- Top 10 Serverless framework best practices
- How to break the “senior engineer” career ceiling
- My advice to junior developers