Get started with AWS Lambda: A beginner’s tutorial

Syed Ali
6 min readDec 18, 2022

--

WHAT?

AWS Lambda is a cloud computing service offered by Amazon Web Services (AWS) that allows you to run code in response to events or automatically in response to certain triggers. You can use AWS Lambda to build serverless applications, which are applications that are built using functions that are executed in response to events or triggers, rather than running on servers all the time.

PRE-REQUISITES?

First, you’ll need an AWS account. If you don’t have one yet, start by opening a free AWS account here. AWS has a free tier that’s more than enough for what you will need for this tutorial.

HOW?

Here’s a step-by-step tutorial on how to create and test your first AWS Lambda function that utilizes Amazon S3:

  • ✦1✦ Sign in to the AWS Management Console and navigate to the Lambda service.
  • ✦2✦ Click the “Create function” button to create a new function.
  • ✦3✦ Choose “Author from scratch” and give your function a name. For the runtime, choose the programming language that you want to use to write your function.
  • ✦4✦ In the “Designer” section, you will see the “Add triggers” box. Click on it and choose “S3” from the list of triggers.
  • ✦5✦ Configure the trigger by selecting the S3 bucket that you want to use as the trigger for your function and then specify the event type that should trigger the function. For example, you might choose “Object created” if you want the function to be triggered when a new object is added to the S3 bucket.
  • ✦6✦ Scroll down to the “Function code” section and write the following function code:
import boto3

def send_email(to_address, subject, body):
client = boto3.client('ses')
response = client.send_email(
Source='youremail@example.com',
Destination={
'ToAddresses': [
to_address,
]
},
Message={
'Subject': {
'Data': subject
},
'Body': {
'Text': {
'Data': body
}
}
}
)

def lambda_handler(event, context):
to_address = 'recipient@example.com'
subject = 'Hello from Lambda!'
body = 'This is a test email sent from a Lambda function using Amazon SES.'
send_email(to_address, subject, body)

This code defines a function called send_email that takes three arguments:

1. the email address to send the email to

2. the subject of the email

3. and the body of the email.

It uses the boto3 library to send an email using Amazon SES. The lambda_handler function is the entry point for the Lambda function. It specifies the recipient’s email address, the subject, and the body of the email, and then calls the send_email function to send the email.

You will need to replace youremail@example.com with the email address that you want to use as the sender. You will also need to replace receipient@example.com with the email address that you want to send the email to.

  • ✦7✦ Scroll down to the “Basic settings” section and specify the memory and timeout settings for your function.
  • ✦8✦ Click the “Save” button to save your function.

Create an S3 bucket: To create an S3 bucket, you will need to go to the S3 section of the AWS Management Console and click the “Create bucket” button. You will then be prompted to enter a name for your bucket and select a region. It’s important to choose a unique bucket name, as bucket names must be globally unique across all AWS accounts.

Set up the bucket’s permissions: By default, S3 bucket access is private, meaning that only the owner of the bucket has access to the objects stored in the bucket. If you want to allow other users or systems to access the objects in your bucket, you will need to set up the appropriate permissions. You can do this by editing the bucket’s access control list (ACL) or by using bucket policies.

  • ✦9✦ Upload an object to the bucket: To test your Lambda function, you will need to upload an object to your S3 bucket. You can do this using the S3 Management Console, the AWS CLI, or the S3 API. Once you have uploaded an object to your bucket, you can trigger your Lambda function by performing the action specified in the function’s trigger (e.g. adding an object to the bucket).
  • ✦10✦ If everything is working correctly, your function should be triggered and the object should be processed according to the logic in your function code.
  • ✦11✦ Monitor the function’s execution: You can monitor the execution of your Lambda function by using the “Monitoring” tab on the function’s configuration page.

To configure Amazon SES, sign in to the AWS Management Console and navigate to the SES service.

In the “Identity Management” section, click on the “Verified Senders” tab and then click the “Verify a New Email Address” button.

Enter the email address that you want to use as the sender and follow the prompts to verify the email address.

Once the email address is verified, you can use it to send emails using the send_email function that you defined in your Lambda function.

Congratulations — you have just set up and deployed your own Lambda Function!

Another advantage of going serverless is that you no longer need to keep a server running all the time. The “server” suddenly appears when you need it, then disappears when you’re done with it. Now you can think in terms of functions instead of servers, and all your business logic can now live within these functions.

Thanks to Adam Watt for sharing this great story about AWS Lambda. The excerpt above is from his story.

PROS & CONS

Here are some of the pros and cons of using AWS Lambda:

Pros:

  • Scalability: AWS Lambda automatically scales up or down based on the demand for your code, so you don’t have to worry about capacity planning.
  • Cost-effectiveness: Because AWS Lambda only charges you for the actual execution of your code, it can be a cost-effective way to run your applications, especially if you have unpredictable or varying workloads.
  • Ease of use: AWS Lambda makes it easy to get started with serverless computing, with a simple interface and pre-built runtime environments for popular programming languages.

Cons:

  • Cold starts: AWS Lambda may experience “cold starts” when it has to spin up a new instance of your code to handle a request. This can cause increased latency for the first request and may not be suitable for applications with very low-latency requirements.
  • Limited control: Because AWS Lambda manages the infrastructure for you, you have less control over the underlying servers and operating system. This can make it more difficult to customize or optimize your environment for certain workloads.
  • Limited resources: AWS Lambda has limits on the amount of memory and CPU that you can allocate to your functions, which may not be sufficient for certain workloads.

ALTERNATIVES

Other cloud providers also offer services that are similar to AWS Lambda, including:

  • Google Cloud Functions: Google Cloud Functions is a serverless computing platform offered by Google Cloud that allows you to run code in response to events or triggers.
  • Microsoft Azure Functions: Microsoft Azure Functions is a serverless computing platform offered by Microsoft Azure that allows you to run code in response to events or triggers.
  • IBM Cloud Functions: IBM Cloud Functions is a serverless computing platform offered by IBM Cloud that allows you to run code in response to events or triggers.
  • Alibaba Cloud Function Compute is a serverless computing platform offered by Alibaba Cloud that allows you to run code in response to events or triggers. It supports a wide range of programming languages and integrates with other Alibaba Cloud services, such as Object Storage Service (OSS) and Message Service (MNS).
  • Oracle Cloud Functions is a serverless computing platform offered by Oracle Cloud that allows you to run code in response to events or triggers. It supports a wide range of programming languages and integrates with other Oracle Cloud services, such as Oracle Autonomous Transaction Processing and Oracle Autonomous Data Warehouse.

I hope this helps to give you an understanding of what AWS Lambda is used for and some of its alternatives in other cloud providers. Let me know if you have any further questions or need further assistance.

BOTTOM LINE

AWS Lambda can be used to build a wide range of applications, including microservices, backend services for mobile or web applications, real-time data processing pipelines, and more. Some of the key benefits of using AWS Lambda include scalability, cost-effectiveness, and ease of use.

Thanks for reading!

--

--

Syed Ali

With over a decade of experience, Syed Ali is an IT expert with a passion for data analytics and research & development.