AWS Lambda can interact with other AWS services, such as Amazon S3 and Amazon DynamoDB, securely over a private network using AWS PrivateLink and VPC configurations. Setting up Lambda functions to access S3 and DynamoDB in a private network offers better security and control over network traffic, helping avoid exposure to the public internet. This article provides step-by-step instructions for setting up S3 and DynamoDB access from Lambda within a private network, along with the advantages of this setup.
Tip: S3 Bucket Name Generator - Use this tool to generate unique and compliant S3 bucket names.
Create a VPC:
10.0.0.0/16
.Create Private Subnets:
Add a NAT Gateway (Optional):
AWS VPC endpoints allow Lambda functions within a VPC to access AWS services directly without using a public IP.
Create an S3 VPC Endpoint:
Create a DynamoDB VPC Endpoint:
Create or Update Lambda Function:
Configure VPC Access:
To confirm that Lambda can access S3 and DynamoDB within the private network:
import boto3
s3 = boto3.client('s3')
dynamodb = boto3.client('dynamodb')
def lambda_handler(event, context):
# Access S3 bucket
response = s3.get_object(Bucket='your-private-bucket', Key='example.txt')
content = response['Body'].read().decode('utf-8')
# Write data to DynamoDB
dynamodb.put_item(
TableName='your-table',
Item={'id': {'S': '123'}, 'content': {'S': content}}
)
return {
'statusCode': 200,
'body': 'Data retrieved and stored successfully'
}
Monitor in VPC Flow Logs:
Check CloudWatch Logs:
No, if the Lambda function is configured within a VPC without additional public access configurations, it won’t be directly accessible from the public internet. By default, placing a Lambda function in a private subnet within a VPC isolates it from public access.
However, there are ways to allow external access while keeping the Lambda function’s operations private:
API Gateway: You can use API Gateway as an entry point, which securely invokes the Lambda function. This allows public internet requests to reach API Gateway, but the Lambda function itself remains isolated in the private VPC.
Lambda Function URLs: AWS Lambda Function URLs provide a direct HTTP(S) endpoint to invoke a Lambda function. When used with VPC, additional security controls and IAM policies would be needed to ensure only authorized requests can access it.
Application Load Balancer (ALB): An ALB can also route traffic from the public internet to Lambda functions, offering more flexibility for security configurations and allowing you to set access restrictions via ALB rules.
By using one of these services, you can allow public access to the Lambda function securely, without exposing the Lambda function or its VPC configuration to the internet directly.
By configuring Lambda functions to access S3 and DynamoDB over a private network, you can enhance security, reduce latency, and better control access to sensitive resources. This setup helps protect your data by keeping it within AWS’s private network, avoiding exposure to the public internet. Following the above steps will allow you to set up a secure and efficient environment for your Lambda functions, ensuring they can communicate with other AWS services while maintaining compliance and optimizing performance.