Press enter or click to view image in full size
Christmas is approaching rapidly. If you’re like me and you have trouble finding gift ideas, you’ll be happy to know that I’ve got you covered. In this article, I will show you how to deploy your very own serverless gift idea generator (GaaS, acronym for Gift Idea as a Service). Using this novel approach, you’ll be able to offload those annoying gift idea requests to the cloud. But first, let’s examine what are our technical requirements.
Requirements
Most importantly, you’ll want your gift idea generator to scale automatically to accommodate large volumes of requests. Some of you out there have big families. We don’t want to see Aunt Gloria be 503'ed because your little cousins are hammering your GaaS.
Also, you’ll want your GaaS to output well-formatted JSON responses. Since JSON is now the lingua franca of web services, your service will accomodate all types of HTTP clients that your extended family might want to use.
Finally, as any good cloud practitioner, you are allergic to those blue buttons. For that reason, you’ll want your GaaS to be defined in versionable text files. For that matter, we’ll use CloudFormation’s Serverless Application Model (better known as SAM).
Let’s go
You’ll probably want to clone the code to follow along. First, let’s have a look at the core of your GaaS: the Lambda function that generates gift ideas:
Nothing too fancy here. We just statically declare all possible gifts you might want to have, as well as adjectives so you can get truly random gift ideas.
Now, the CloudFormation template is simple too:
We declare two resources in there. First, we declare MyApi, which uses AWS SAM’s AWS:Serverless:API transform. Notice how AWS::Serverless::API supports Swagger definitions to let you describe your API. Finally, we create a Python 3 AWS:Serverless::Function that will deploy our gift idea generator logic to a Lambda function.
You are now ready to deploy your GaaS to the clouds! In order to do that, you’ll first need to create a S3 bucket in your AWS account. Once this is done, open your favorite terminal and make sure your AWS credentials are available to the AWS CLI.
You can then run
DEPLOY_BUCKET={the name of the S3 bucket you just created} STACK_NAME={choose a stack name} ./deploy.sh and let the magic happen. Once deployment is completed, the deploy.sh script will print out the URL for your GaaS endpoint:
API endpoint: https://3ef6atvn4b.execute-api.us-east-1.amazonaws.com/prod/giftsSend a GET request to this endpoint using your favorite HTTP client, and you’ll get a fancy randomized list of gift ideas:
{
"gift_ideas": [
"A crazy Cup of tea",
"A disturbing Mickey Mouse Toaster",
"A useless Ghost",
"A unique String",
"A tiny Harry Potter Scarf"
]
}What’s next?
The first thing you’ll need to do is set aside some time to teach your neophyte relatives how to use a decent HTTP client. Little Jimmy might like using Postman. Your old granddaddy will probably prefer sticking to cURL. Showing them how to harness the power of HTTP clients will ensure you get awesome gifts this Christmas.
Also, you’ll probably want to wire up your API gateway to Route53 and a custom domain. This will lessen the probability that one finger typists in your family stumble when trying to invoke the alphanumeric mess that is your API Gateway provides us for an URL.