|
| 1 | +# Integrating a local Lambda function with Neo4j using LocalStack |
| 2 | + |
| 3 | +| Key | Value | |
| 4 | +| ------------ | --------------------------------------------------------------------------------------------- | |
| 5 | +| Environment | LocalStack, AWS | |
| 6 | +| Services | Lambda, CloudWatch | |
| 7 | +| Integrations | SAM | |
| 8 | +| Categories | Serverless; Databases | |
| 9 | +| Level | Beginner | |
| 10 | +| GitHub | [Repository link](https://github.com/localstack-samples/sample-lambda-neo4j) | |
| 11 | + |
| 12 | +## Introduction |
| 13 | + |
| 14 | +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. |
| 15 | + |
| 16 | +## Prerequisites |
| 17 | + |
| 18 | +- [LocalStack Auth Token](https://docs.localstack.cloud/getting-started/auth-token/) |
| 19 | +- [Serverless Application Model](https://docs.localstack.cloud/user-guide/integrations/aws-sam/) with the [`samlocal`](https://github.com/localstack/aws-sam-cli-local) installed. |
| 20 | +- [AWS CLI](https://docs.localstack.cloud/user-guide/integrations/aws-cli/) with the [`awslocal` wrapper](https://docs.localstack.cloud/user-guide/integrations/aws-cli/#localstack-aws-cli-awslocal). |
| 21 | +- [Python 3.10](https://www.python.org/downloads/) & `pip` |
| 22 | +- [Docker Compose](https://docs.docker.com/compose/install/) |
| 23 | + |
| 24 | +Start LocalStack Pro with the `LOCALSTACK_AUTH_TOKEN` pre-configured: |
| 25 | + |
| 26 | +```shell |
| 27 | +export LOCALSTACK_AUTH_TOKEN=<your-auth-token> |
| 28 | +docker-compose up |
| 29 | +``` |
| 30 | + |
| 31 | +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. |
| 32 | + |
| 33 | +## Instructions |
| 34 | + |
| 35 | +### Installing the dependencies |
| 36 | + |
| 37 | +You can install the dependencies using the following command: |
| 38 | + |
| 39 | +```shell |
| 40 | +cd function |
| 41 | +pip install --target ../package/python -r requirements.txt |
| 42 | +``` |
| 43 | + |
| 44 | +### Building the application |
| 45 | + |
| 46 | +To build the SAM application, run the following command from the root directory of the application: |
| 47 | + |
| 48 | +```shell |
| 49 | +samlocal build |
| 50 | +``` |
| 51 | + |
| 52 | +If you see a `Build Succeeded` message, you can proceed to the next step. |
| 53 | + |
| 54 | + |
| 55 | +### Deploying the application |
| 56 | + |
| 57 | +To deploy the SAM application, run the following command: |
| 58 | + |
| 59 | +```shell |
| 60 | +samlocal deploy --guided |
| 61 | +``` |
| 62 | + |
| 63 | +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. |
| 64 | + |
| 65 | +### Updating the Lambda function configuration |
| 66 | + |
| 67 | +You need to update the Lambda function configuration to use the Neo4j environment variables. You can do this by running the following command: |
| 68 | + |
| 69 | +```shell |
| 70 | +export NEO4J_PASSWORD=neo4j-harsh-test |
| 71 | +export NEO4J_URI=bolt://neo4j:7687 |
| 72 | +export NEO4J_USERNAME=neo4j |
| 73 | +FUNCTION=$(awslocal cloudformation describe-stack-resource --stack-name sam-app --logical-resource-id function --query 'StackResourceDetail.PhysicalResourceId' --output text) |
| 74 | +awslocal lambda update-function-configuration \ |
| 75 | + --function-name $FUNCTION \ |
| 76 | + --environment "Variables={NEO4J_USERNAME=$NEO4J_USERNAME,NEO4J_PASSWORD=$NEO4J_PASSWORD,NEO4J_URI=$NEO4J_URI}" |
| 77 | +``` |
| 78 | +
|
| 79 | +### Invoking the Lambda function |
| 80 | +
|
| 81 | +You can invoke the Lambda function using the following command: |
| 82 | +
|
| 83 | +```shell |
| 84 | +awslocal lambda invoke \ |
| 85 | + --function-name $FUNCTION \ |
| 86 | + --payload file://event.json \ |
| 87 | + --cli-binary-format raw-in-base64-out out.json |
| 88 | +``` |
| 89 | +
|
| 90 | +The following output would be displayed in the terminal: |
| 91 | +
|
| 92 | +```json |
| 93 | +{ |
| 94 | + "StatusCode": 200, |
| 95 | + "ExecutedVersion": "$LATEST" |
| 96 | +} |
| 97 | +``` |
| 98 | +
|
| 99 | +You can see the following in the `out.json` file: |
| 100 | +
|
| 101 | +```json |
| 102 | +{"TotalCodeSize": 29662331, "FunctionCount": 1} |
| 103 | +``` |
| 104 | +
|
| 105 | +### Cleaning up |
| 106 | +
|
| 107 | +To clean up the resources created by the SAM application, run the following command: |
| 108 | +
|
| 109 | +```shell |
| 110 | +docker-compose down |
| 111 | +``` |
| 112 | +
|
| 113 | +LocalStack is ephemeral, and all the resources will be deleted once the LocalStack process is stopped. |
| 114 | +
|
| 115 | +### GitHub Action |
| 116 | +
|
| 117 | +This application sample hosts an example GitHub Action workflow that starts up LocalStack, builds the Lambda functions, and deploys the infrastructure on the runner. |
| 118 | +
|
| 119 | +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. |
| 120 | +
|
| 121 | +## License |
| 122 | +
|
| 123 | +[Apache 2.0](./LICENSE) |
0 commit comments