
The security groups acts as virtual firewall for the cluster to control inbound and outbound traffic. The database is running in public subnet and is publicly accessible. Output: Connect to cross account Amazon Redshift database in Public subnet with Publicly accessible set to Yesīelow is Amazon Redshift connection details running in Account A. The handler creates a connection to the PostgreSQL database, executes a SELECT sql to fetch the current timestamp from the database into results variable and returns the results as string.

#Aws postgresql lambda 2018 code
The code installs the PostgreSQL interface library pg8000 to interact with the database. This managed policy has the following permissions which Lambda uses to create and manage network interfaces:īelow is the VPC configuration for my Lambda function attached with 2 Private subnets:Īfter creating the lambda function, I used the below code to connect and execute SQL against the Amazon Aurora PostgreSQL database. In case you choose to use an existing role while creating the Lambda function make sure to attach AWSLambdaVPCAccessExecutionRole policy. In this case the Lambda function is launched with an execution role (IAM role) having 2 managed policies attached by default: An ENI represents a virtual network cards and you can read more here. The function creation can take some time as Lambda creates ENI (elastic network interface) in each subnet of the VPC configuration. Once you have completed the details, click on Create function. In case you select public subnets instead of Private subnet, the Lambda function will time out as they cannot have public IP addresses. To access private Amazon VPC resources, such DB instance you need to associate your Lambda function with one or more private subnets. Choose the VPC name in which the Aurora PostgreSQL database is running.In the Permissions section you can keep the default – Create a new role with basic Lambda permissions.

Choose the language of your preference to use.Lets go ahead and create the Lambda function –ĪWS Service > Lambda > Functions > Create Function > Author from scratch To connect to the Aurora PostgreSQL database in a Private subnet you need to configure Lambda function in a Virtual Private Cloud (VPC). The connectivity and security detail are as follows: In this setup, Amazon Aurora PostgreSQL database is running in private subnet with public accessibility set to No. Connecting to cross account Amazon Redshift database in public subnet with public accessibility set to Yes.Ĭonnect to Amazon Aurora PostgreSQL database in Private subnet with Public accessibility set to No in the same AWS account.

