Skip to main content

How to Deploy an Application in AWS Lambda (Serverless Architecture)

AWS Lambda allows you to run code without provisioning or managing servers. In this guide, we'll walk you through the deployment of an application using AWS Lambda in a serverless architecture. AWS Lambda is typically used in conjunction with other AWS services such as API Gateway, DynamoDB, S3, etc., to build a fully serverless application.

Key Components of a Lambda Deployment:
  • AWS Lambda Function: Your code packaged in a zip file or container.
  • Event Source: Triggers for your Lambda function (e.g., API Gateway, S3, DynamoDB).
  • Execution Role (IAM): Permissions that your function uses to interact with AWS resources.
  • Amazon CloudWatch: Used for logging and monitoring Lambda executions.

Prerequisites​

Before starting, ensure you have the following:

  1. An AWS account.
  2. AWS CLI installed and configured locally.
  3. A basic understanding of Node.js, Python, or any Lambda-supported language.
  4. AWS IAM permissions to create and manage Lambda functions, API Gateway, and other resources.

Step-by-Step Guide to Deploy a Lambda Application​

Step 1: Prepare Your Application Code

Lambda supports multiple languages like Node.js, Python, Java, Go, etc. Let’s take an example of a basic Node.js function.

Create a simple index.js file:

javascript
// index.js
exports.handler = async (event) => {
console.log("Event: ", event);
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
return response;
};

Step 2: Package Your Code

To deploy the application in AWS Lambda, package your code in a zip file.

bash
zip -r lambda-function.zip index.js

Alternatively, you can deploy using container images for more complex applications.

Step 3: Create an IAM Role for Lambda Execution

Your Lambda function needs permissions to execute and interact with AWS services.

  1. Go to the IAM console and create a new role.
  2. Choose Lambda as the trusted entity for the role.
  3. Attach the following policies:
    • AWSLambdaBasicExecutionRole (for CloudWatch logging)
    • Any additional policies required by your function, such as access to DynamoDB, S3, or other AWS services.
  4. Name the role (e.g., lambda-execution-role) and save it.
Step 4: Create the Lambda Function

Once the code and role are ready, it’s time to create the Lambda function using either the AWS Console, AWS CLI, or Infrastructure as Code (like Terraform).

Using AWS CLI
Run the following command to create the Lambda function:

bash
aws lambda create-function \
--function-name MyFirstLambdaFunction \
--zip-file fileb://lambda-function.zip \
--handler index.handler \
--runtime nodejs18.x \
--role arn:aws:iam::<your-account-id>:role/lambda-execution-role
Step 5: Add a Trigger (API Gateway)

To invoke your Lambda function via an HTTP request, you can set up an API Gateway as a trigger.

Use the following commands to create an API Gateway and link it to your Lambda function.

bash
aws apigatewayv2 create-api --name "MyAPIGateway" --protocol-type "HTTP"
# Get the API ID from the response, then create a route.
aws apigatewayv2 create-route --api-id <api-id> --route-key "GET /lambda"
# Attach the Lambda function to the route
aws apigatewayv2 create-integration \
--api-id <api-id> \
--integration-type AWS_PROXY \
--integration-uri arn:aws:apigateway:<region>:lambda:path/2015-03-31/functions/arn:aws:lambda:<region>:<account-id>:function:MyFirstLambdaFunction/invocations
Step 6: Set Up Permissions for API Gateway to Invoke Lambda

Lambda functions need explicit permissions to be invoked by API Gateway. Use the following command to grant this permission:

bash
aws lambda add-permission \
--function-name MyFirstLambdaFunction \
--statement-id apigateway-permission \
--action lambda:InvokeFunction \
--principal apigateway.amazonaws.com \
--source-arn arn:aws:execute-api:<region>:<account-id>:<api-id>/*
Step 7: Test the Application

After deploying the API Gateway, note the public URL for your endpoint (it will be displayed in the API Gateway console or CLI response). Use any HTTP client like curl or Postman to test your API.

bash
curl -X GET https://<api-id>.execute-api.<region>.amazonaws.com/lambda

If everything is set up correctly, you should receive the response:

json
{
"statusCode": 200,
"body": "\"Hello from Lambda!\""
}
Step 8: Monitoring and Logs

To monitor the performance of your Lambda function and check for any errors, use Amazon CloudWatch Logs:

  1. Go to the CloudWatch console.
  2. Navigate to Logs Groups.
  3. Find the log group associated with your Lambda function (/aws/lambda/MyFirstLambdaFunction).
  4. View the logs to monitor invocations, execution time, and potential errors.
Step 9: Update the Function (Optional)

If you need to update your Lambda code, follow these steps:

  1. Modify your code locally.

  2. Zip the file again:

bash
zip -r lambda-function.zip index.js
  1. Update the Lambda function using either the AWS console or CLI.
bash
aws lambda update-function-code \
--function-name MyFirstLambdaFunction \
--zip-file fileb://lambda-function.zip

Alternatively, you can use deployment automation tools like AWS SAM (Serverless Application Model) or Serverless Framework for more complex deployments, automated rollbacks, and configuration management.

Transform Your Business with Expert DevOps Solutions

Our tailored DevOps consulting services help streamline your processes, accelerate delivery, and ensure scalability.

Conclusion​

Deploying an application on AWS Lambda allows you to take full advantage of serverless computing. By following these steps, you can set up a Lambda function, integrate it with an API Gateway, and manage it effectively using AWS tools like CloudWatch for monitoring. This serverless model reduces infrastructure management, allows for scaling based on demand, and is cost-effective, especially for applications with unpredictable workloads.