How do I use DynamoDB Accelerator (DAX) with AWS Lambda?

3 minute read
0

I want to use my Amazon DynamoDB Accelerator (DAX) cluster with my AWS Lambda function. How do I do that?

Resolution

After you create a DAX cluster, note the VPC ID, subnets, and security group launched with the DAX cluster. Keep this information available for reference. Then, follow the steps below to use your Lambda function with your DAX cluster.

Create a Lambda function with access to your DAX cluster's VPC

1.    Open the Lambda console. Then, open the Functions page.

2.    Choose Create function, and then enter a Function name.

3.    Choose the Runtime and Architecture under Basic information.

4.    Expand Advanced settings. Then, check the box to the left of Enable VPC.

5.    In the VPC dropdown list, choose the previously noted VPC. This is the VPC in which your DAX Cluster was launched.

6.    In the Subnets dropdown list, choose the previously noted subnets. Select all that apply.

7.    In the Security group dropdown list, choose the previously noted VPC security group. The console shows the inbound and outbound rules for that security group.

Important: For the Lambda function to connect with the DAX Cluster, the Inbound rules for the security group must display TCP as the Protocol. Also, 8111 or 9111 must appear under Ports. 8111is for unencrypted clusters, and 9111 is for encrypted clusters.

8.    Choose Create function.

Configure an existing Lambda function to access your DAX cluster's VPC

1.    Open the Lambda console. Then, open the Functions page.

2.    Choose the function that you want to use with your DAX Cluster.

3.    Choose Configuration from the ribbon, and then choose VPC. Choose Edit.

4.    Follow steps 4 through 6 in the previous section, Create a Lambda function with access to your DAX cluster's VPC.

5.    Choose Save.

Test the connection from your Lambda function to your DAX cluster

Important: You must first prepare a deployment package because amazondax isn't available by default in Lambda. See the All versions section in Amazon DynamoDB Accelerator (DAX) to install amazon-dax-client.

After you install amazon-dax-client, follow the steps in Deploy Python Lambda functions with .zip file archives to create a deployment package. Use the following Python code to create the package:

import amazondax
import boto3

def lambda_handler(event, context):
    daxclient = amazondax.AmazonDaxClient(endpoint_url='<endpoint-from-your-cluster>')
    print("Connected!!")

In the deployment package, change the endpoint_url to the one found in your DAX cluster. To do this:

1.    Open the DynamoDB console.

2.    In the navigation pane, under DAX, choose Clusters.

3.    Select the previously created DAX cluster, and then choose Overview.

4.    Under General information, find Cluster endpoint. Copy the URL listed there.

5.    Replace in the Python code with your copied URL.

  1.     Upload the deployment package as a .zip file or through an S3 URL. Then, choose Test.

  2.    If your connection is successfully configured, then the Execution results display “Connected!!”


Related information

Configuring VPC access (console)

Use DynamoDB Accelerator (DAX) from Lambda to increase performance while reducing costs

Developing with the DynamoDB Accelerator (DAX) client

AWS OFFICIAL
AWS OFFICIALUpdated a year ago