Key | Value |
---|---|
Environment | LocalStack, AWS |
Services | Lambda, CloudWatch |
Integrations | SAM |
Categories | Serverless; Databases |
Level | Beginner |
GitHub | Repository link |
This sample application will guide you through the process of integrating a local Lambda function with a local Neo4j database using LocalStack. We will use the Serverless Application Model (SAM) to deploy the Lambda function and spin a local Neo4j database using Docker. We will also use the AWS CLI to invoke the Lambda function and execute a sample query on the Neo4j database.
- LocalStack Auth Token
- Serverless Application Model with the
samlocal
installed. - AWS CLI with the
awslocal
wrapper. - Python 3.10 &
pip
- Docker Compose
Start LocalStack Pro with the LOCALSTACK_AUTH_TOKEN
pre-configured:
export LOCALSTACK_AUTH_TOKEN=<your-auth-token>
docker-compose up
The Docker Compose file will start LocalStack Pro and a local Neo4j database. The LOCALSTACK_AUTH_TOKEN
environment variable is required to activate the LocalStack Pro features, such as Lambda Layers in this example.
You can install the dependencies using the following command:
cd function
pip install --target ../package/python -r requirements.txt
To build the SAM application, run the following command from the root directory of the application:
samlocal build
If you see a Build Succeeded
message, you can proceed to the next step.
To deploy the SAM application, run the following command:
samlocal deploy --guided
The above command will create a new managed S3 bucket to store the artifacts of the SAM application. If you want to use an existing S3 bucket, you can use the --s3-bucket
flag to specify the bucket name. Before being deployed, the CloudFormation changeset will be displayed in the terminal. If you want to deploy the application without confirmation, you can use the --no-confirm-changeset
flag.
You need to update the Lambda function configuration to use the Neo4j environment variables. You can do this by running the following command:
export NEO4J_PASSWORD=neo4j-harsh-test
export NEO4J_URI=bolt://neo4j:7687
export NEO4J_USERNAME=neo4j
FUNCTION=$(awslocal cloudformation describe-stack-resource --stack-name sam-app --logical-resource-id function --query 'StackResourceDetail.PhysicalResourceId' --output text)
awslocal lambda update-function-configuration \
--function-name $FUNCTION \
--environment "Variables={NEO4J_USERNAME=$NEO4J_USERNAME,NEO4J_PASSWORD=$NEO4J_PASSWORD,NEO4J_URI=$NEO4J_URI}"
You can invoke the Lambda function using the following command:
awslocal lambda invoke \
--function-name $FUNCTION \
--payload file://event.json \
--cli-binary-format raw-in-base64-out out.json
The following output would be displayed in the terminal:
{
"StatusCode": 200,
"ExecutedVersion": "$LATEST"
}
You can see the following in the out.json
file:
{"TotalCodeSize": 29662331, "FunctionCount": 1}
To clean up the resources created by the SAM application, run the following command:
docker-compose down
LocalStack is ephemeral, and all the resources will be deleted once the LocalStack process is stopped.
This application sample hosts an example GitHub Action workflow that starts up LocalStack, builds the Lambda functions, and deploys the infrastructure on the runner.
You can find the workflow in the .github/workflows/main.yml
file. To run the workflow, you can fork this repository and push a commit to the main
branch.