Making: My first AWS lambda
Here's what I did to write my first AWS lambda!
A little bit of experience with Python (I'm learning from Python for Kids by Jason R. Briggs)
A little bit of experience with AWS (I've completed the A Cloud Guru Solutions Architect course, but haven't yet passed the exam)
Python 3.6 installed on your machine
I decided I wanted to build a lambda that texts me with a reminder message every afternoon at 2pm. As far as I could tell, this involved three main things: specifying the message, specifying the time of day, and sending the message to my phone.
I logged into my AWS account and in the Lambda service, I created a new lamda function and filled out a few things:
I chose the 'Author from scratch' option
Runtime code (Python 3.6!)
Role: I already had one as part of my AWS training course, so I used that existing one.
On the Lambda Manager page, two main sections stood out:
Designer: looks like this is for setting up a trigger for the lambda
Function code: this is where I entered the python code
There was some code pre-filled in here, which I promptly deleted. My first mistake!
There were a few other options at the bottom of the page but I never needed to use those.
I started with the Function code section because I wanted to get it over with. I had heard (I think from the cloud guru course?) that boto was a package that would be helpful for this kind of thing, so I started with the boto documentation here. Then I went down a rabbit hole:
I knew that SNS (simple notification service) was the AWS service I wanted to use, so I narrowed down my search to SNS.
Reading through, I realized I wanted an SNS client. Narrowed it down further!
Within the client section, I remained overwhelmed by options but eventually sifted through to find the SNS client publish function.
And then, after some number of attempts, I wrote this code:
import boto3 def lambda_handler(event, context): client = boto3.client('sns') response = client.publish( TopicArn= '<ARN maps to Morgan's phone number>', Message= '<My message>' )
My code didn't work at first because I'd deleted this line of code: "def lambda_handler(event, context):". Frankly, I'm still not sure what that bit does. I added it back in, and it worked. I'll figure that out later!
I needed to create an SNS topic in AWS in order to to get the TopicArn, which is a long string that maps to my cell phone number. I set that up in the SNS Service in AWS. Word to the wise: the Display Name of the SNS topic shows up at the start of the message that's sent.
So I had the message and phone connection handled, and next I needed to make this message send at 2pm every day. I knew, through my AWS training, that I'd use the Trigger feature here, which is in the Designer section of the lambda manager. I eventually honed in on the Cloudwatch Event trigger, which runs the lambda on a schedule (more documentation in the Lambda FAQs here). There were two semi-frustrating things about setting this up:
Figuring out the cron expression format. (I used this Stack Overflow page)
The stray question-mark that needed to for some reason be in the middle of the cron expression. (Per this)
But I got it! The Cron expression I went with was:
cron(0 19 ? * * *)
So once I set up the lambda trigger, I had a working lambda that would send me a text reminder every day at 2pm! In the future, I might build on this lambda by sending a different message on different days of the week, or sending a photo or something. This felt like a good start.
There are quite a few Lambda trigger options that seem interesting and worth exploring.
Stack Overflow might end up being helpful to me?
Don't forget the <def lambda_handler(event, context):> code at the beginning of the lambda!
There are several ways of accomplishing the same thing.
I still don't really understand how github works. I opened the boto3 github page and I find it pretty overwhelming.
A lot of rules about software coding are based on the preferences of the person who wrote the coding language/function/whatever, which is why they might seem arbitrary to me but meaningful to someone else. (That, or, I just don't get it yet.)