DEV Community

Uendi Hoxha
Uendi Hoxha

Posted on

Avoiding Pitfalls: Essential Configuration Tips for AWS Lambda

In this article, I will cover common misconfigurations in lambda functions, their impact and how to resolve them. Topics will include VPC integration, setting appropriate permissions, connecting to RDS databases, monitoring and operational best practices, provisioned concurrency and also lambda layers.

I. Configuring AWS Lambda in a VPC
Do configure your lambda in a VPC! When you configure Lambda in a VPC, it can access resources like Amazon RDS databases, Amazon ElastiCache clusters or other services that are only available within your private subnet. This is essential for applications that require secure database connections without exposing those databases to the public internet.

When configuring Lambda functions in a VPC, it’s crucial to set up the right security groups to control traffic based on your application’s needs. For example, if your Lambda function needs to access an Amazon RDS instance, the security group associated with the RDS must allow inbound traffic from the security group associated with the Lambda function.

If the Lambda function needs to access other AWS services (e.g., S3, DynamoDB), ensure that the Lambda’s security group allows outbound traffic to those service endpoints. This is usually done by allowing all outbound traffic, as AWS services are designed to communicate within the AWS network securely.

If your Lambda needs to communicate with external APIs, configure the Lambda function to use a NAT Gateway or NAT instance for outbound internet access. The security group should allow outbound traffic to 0.0.0.0/0 for HTTP/HTTPS.

Enable VPC Flow Logs to capture information about the IP traffic going to and from network interfaces in your VPC. This can help you identify and troubleshoot network issues.

II. Setting the Right Permissions with AWS IAM
Lambdas often have overly broad IAM permissions or lack the necessary permissions, leading to either security risks or operational failures. Follow the principle of least privilege and assign only the necessary permissions using granular IAM roles and policies. Use IAM Roles for Lambda instead of attaching permissions directly to the function.

For example, if your Lambda function needs to access an S3 bucket and a DynamoDB table, attach policies that provide read/write access to those resources.

When defining IAM policies, aim for granularity. Instead of using wildcard permissions (e.g., s3:* or dynamodb:*), specify the exact actions your Lambda function needs to perform.
Instead of granting full access to S3, create a policy that only allows specific actions on a designated bucket:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "s3:GetObject",
        "s3:PutObject"
      ],
      "Resource": [
        "arn:aws:s3:::example-bucket/*"
      ]
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

For a DynamoDB table, allow only the necessary operations:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "dynamodb:GetItem",
        "dynamodb:PutItem"
      ],
      "Resource": [
        "arn:aws:dynamodb:region:account-id:table/my-table"
      ]
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

III. Connecting Lambda to RDS Databases
Using Amazon RDS Proxy is a recommended solution for alleviating the issues associated with connecting AWS Lambda to RDS databases. RDS Proxy acts as an intermediary between your Lambda function and the RDS instance, managing and pooling connections effectively. RDS Proxy enables your application to scale seamlessly, accommodating sudden spikes in traffic without degrading performance.

First, create an IAM role that grants RDS Proxy permission to connect to your RDS database. The policy should allow the rds-db:connect action on your database resources.

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "rds-db:connect",
      "Resource": "arn:aws:rds-db:region:account-id:dbuser/my-db-user"
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

Next, use the AWS CLI or the AWS Management Console to create the RDS Proxy.

Ensure that the security group associated with your RDS Proxy allows inbound traffic from your Lambda function. You may need to modify the security group rules to allow access on the database port (e.g., 3306 for MySQL).

In your Lambda function code, update the database connection string to point to the RDS Proxy endpoint rather than the RDS instance directly.
_Example: _

const mysql = require('mysql');
const connection = mysql.createConnection({
  host: 'my-db-proxy.proxy-abcdefghijkl.us-east-1.rds.amazonaws.com',
  user: 'my-db-user',
  password: 'my-db-password',
  database: 'my-database'
});

connection.connect((err) => {
  if (err) {
    console.error('Error connecting to the database:', err.stack);
    return;
  }
  console.log('Connected to the database.');
});
Enter fullscreen mode Exit fullscreen mode

IV. Monitoring and Operations with CloudWatch
Without proper monitoring, it’s challenging to gain insights into your Lambda function’s performance, leading to potential issues like undetected errors, performance bottlenecks, resource wastage, etc.

By default, AWS Lambda automatically integrates with CloudWatch Logs. Each invocation of your function generates log entries that contain details about execution, errors and any output returned.

CloudWatch automatically collects several key metrics for Lambda functions, including:
Invocations: Number of times your function is invoked.
Duration: Time taken to execute the function.
Errors: Count of failed executions.
Throttles: Number of invocation requests that were throttled due to concurrency limits.

To proactively monitor your Lambda functions, configure CloudWatch Alarms to notify you of potential issues. For example, you can set up an alarm to trigger if the error rate exceeds a certain threshold. For example, creating an Alarm for High Error Rate:

aws cloudwatch put-metric-alarm --alarm-name HighErrorRate \
  --metric-name Errors --namespace AWS/Lambda --statistic Sum --period 300 \
  --threshold 5 --comparison-operator GreaterThanThreshold \
  --dimensions Name=FunctionName,Value=myLambdaFunction \
  --evaluation-periods 1 --alarm-actions arn:aws:sns:us-east-1:123456789012:my-sns-topic \
  --unit Count
Enter fullscreen mode Exit fullscreen mode

Use CloudWatch Logs Insights to analyze and query your logs. This is an awesome feature that allows you to run SQL-like queries to find specific logs, helping with debugging and performance analysis. For example:

fields @timestamp, @message
| filter @message like /ERROR/
| sort @timestamp desc
| limit 20
Enter fullscreen mode Exit fullscreen mode

For deeper insights, enable AWS X-Ray to trace requests through your Lambda function. This can help you understand latencies and identify bottlenecks in your application flow.

V. Provisioned Concurrency Configurations
Provisioned Concurrency is a feature that ensures AWS Lambda functions are always ready to respond instantly to incoming requests. By pre-initializing a specified number of function instances, Provisioned Concurrency helps eliminate cold starts, leading to improved performance and reduced latency.

To configure Provisioned Concurrency for a Lambda function, specify the amount of concurrency you want to provision. This ensures that a set number of instances are always warm and ready to handle requests.

Use CloudWatch to monitor metrics related to Provisioned Concurrency, such as the number of provisioned instances and the number of concurrent requests. This data can help you optimize the provisioned level based on usage patterns.

Considerations for Costs: While Provisioned Concurrency reduces cold starts, it comes at an additional cost. Be mindful of your application’s usage patterns to avoid over-provisioning, which can lead to unnecessary expenses.
Enter fullscreen mode Exit fullscreen mode

VI. Using Lambda Layers for Code Reusability
Don't ever repeat your self! AWS lambda layers allows you to package and share common code, libraries, and dependencies across multiple Lambda functions.

To create a Lambda Layer, package the libraries and dependencies you want to reuse into a zip file. This zip file should contain a directory structure that follows the conventions for Lambda Layers. For example, if you’re including a Python library, it should be in the python/lib/python3.8/site-packages directory structure.
Example of packaging a python library:

mkdir -p my-layer/python/lib/python3.8/site-packages
pip install requests -t my-layer/python/lib/python3.8/site-packages/
zip -r my-layer.zip my-layer
Enter fullscreen mode Exit fullscreen mode

Once the zip file is ready, publish it to AWS Lambda using the AWS CLI or the AWS Management Console.

Example using the AWS CLI to Publish a Layer:

aws lambda publish-layer-version --layer-name MyLayer --zip-file fileb://my-layer.zip --compatible-runtimes python3.8
Enter fullscreen mode Exit fullscreen mode

After publishing the layer, you can include it in your Lambda functions. You can do this either when creating a new function or by updating an existing one:

aws lambda update-function-configuration --function-name myLambdaFunction --layers arn:aws:lambda:us-east-1:123456789012:layer:MyLayer:1
Enter fullscreen mode Exit fullscreen mode

Each time you publish a new version of a layer, it gets a unique version ARN. This allows you to manage different versions of libraries independently. Please be cautious about breaking changes when updating layers that are used by multiple functions.

Top comments (0)