BACKGROUND IMAGE: iSTOCK/GETTY IMAGES
Writing a Lambda function on AWS is easy, especially given the extensive blueprints available and the wide choice of supported languages. Lambda functions are fully integrated into the AWS ecosystem, allowing developers to create added value, such as monitoring AWS cloud services with a dozen or more lines of code. One such task that adds value is to have a Lambda function send a developer a text message to alert them when something happens.
Once the development team has configured a Lambda function on AWS, it can begin to customize it. And while there is a template to trigger Lambda when an Amazon Simple Notification Service (SNS) message arrives, there isn't one to actually send an SNS. Doing a quick search of StackOverFlow provides the necessary code (Figure 1). In this AWS Lambda example, we will set a function to send a text message for Amazon Simple Storage Service (S3) changes.
The first step in this AWS Lambda example is to include the aws-sdk -- line 3 of the code shown in Figure 1 -- and then create objects for S3 and SNS, so a developer can make a call against those services. Because we're operating within the Lambda system and have established an Amazon Machine Image role for the function, there's no need to authenticate within the function. This can lead to simpler code that strictly focuses on the actual logic you want to execute
Before the Lambda function can emit an SNS message, you need an SNS topic and a subscription to that topic. To set this up, go to the SNS console page and select Create Topic. Give the topic a name and a display name -- for simplicity, I use the same string for both. This creates a topic you can send requests to, but nothing will happen until you configure a subscription to that topic.
A subscription is an endpoint to send the notification to -- Web address, email, phone number, another lambda or an SQS endpoint. In this case, choose SNS and supply the cell phone number as the endpoint. Next, select Confirm Subscription to send a test message to the specified endpoint. You can then continue with the Lambda function.
The body of this AWS Lambda example function -- lines 12 to 25 -- is actually, itself a function. Many languages now support the creation of inline functions -- anonymous functions in Java parlance. In this AWS Lambda example, we call the publish function from SNS, which passes a JSON structure holding the SNS message and the topic's Amazon Resource Name -- in this case, "lambdatop."
The other parameter to the function is a callback that receives an error object and a data object. Because this is a callback, the entire method is asynchronous, which means we must instruct the Lambda system when the function is complete.
The call to sns.publish returns immediately; if you put the call to context.done() after the sns.publish call, line 26, then the Lambda function would terminate before the SNS publish occurred.
Keeping the call to context.done within the body of the function callback forces it to run until the publish completes or fails. However, extending this Lambda runtime also increases costs; Lambda execution is charged by the 100 milliseconds of execution.
Next, upload a file to S3 via the console. Within a second or two, you should receive a text message alert. If you upload multiple files you'll get multiple alerts. To take this AWS Lambda example a step further, add some logic to send different messages based on the type of file you upload or to send a message if the file meets a particular criteria (Figure 2).
AWS customers are grateful for Lambda
Configure Amazon Echo with Lambda
Serverless apps benefit from AWS authentication