Skip to content

Commit eb97408

Browse files
committed
setup the basic app
1 parent c13cf2e commit eb97408

File tree

7 files changed

+153
-1
lines changed

7 files changed

+153
-1
lines changed

.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export NEO4J_PASSWORD=mySecretPassword
2+
export NEO4J_URI=bolt://neo4j:7687
3+
export NEO4J_USERNAME=neo4j

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ celerybeat.pid
120120
*.sage.py
121121

122122
# Environments
123-
.env
124123
.venv
125124
env/
126125
venv/

docker-compose.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
version: '3.9'
2+
3+
services:
4+
localstack:
5+
container_name: "${LOCALSTACK_DOCKER_NAME:-localstack-main}"
6+
image: localstack/localstack-pro:latest # required for Pro
7+
ports:
8+
- "127.0.0.1:4566:4566" # LocalStack Gateway
9+
- "127.0.0.1:4510-4559:4510-4559" # external services port range
10+
- "127.0.0.1:443:443" # LocalStack HTTPS Gateway (Pro)
11+
environment:
12+
- LOCALSTACK_AUTH_TOKEN=${LOCALSTACK_AUTH_TOKEN}
13+
- DEBUG=1
14+
- LAMBDA_RUNTIME_ENVIRONMENT_TIMEOUT=600
15+
volumes:
16+
- "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
17+
- "/var/run/docker.sock:/var/run/docker.sock"
18+
networks:
19+
ls:
20+
# Set the container IP address in the 10.0.2.0/24 subnet
21+
ipv4_address: 10.0.2.20
22+
neo4j:
23+
container_name: neo4j
24+
image: neo4j:latest
25+
ports:
26+
- 7474:7474
27+
- 7687:7687
28+
volumes:
29+
- ./neo4j_db/data:/data
30+
- ./neo4j_db/logs:/logs
31+
- ./neo4j_db/import:/var/lib/neo4j/import
32+
networks:
33+
- ls
34+
35+
networks:
36+
ls:
37+
ipam:
38+
config:
39+
# Specify the subnet range for IP address allocation
40+
- subnet: 10.0.2.0/24

event.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"Records": [
3+
{
4+
"kinesis": {
5+
"kinesisSchemaVersion": "1.0",
6+
"partitionKey": "1",
7+
"sequenceNumber": "49590338271490256608559692538361571095921575989136588898",
8+
"data": "SGVsbG8sIHRoaXMgaXMgYSB0ZXN0Lg==",
9+
"approximateArrivalTimestamp": 1545084650.987
10+
},
11+
"eventSource": "aws:kinesis",
12+
"eventVersion": "1.0",
13+
"eventID": "shardId-000000000006:49590338271490256608559692538361571095921575989136588898",
14+
"eventName": "aws:kinesis:record",
15+
"invokeIdentityArn": "arn:aws:iam::123456789012:role/lambda-role",
16+
"awsRegion": "us-east-2",
17+
"eventSourceARN": "arn:aws:kinesis:us-east-2:123456789012:stream/lambda-stream"
18+
},
19+
{
20+
"kinesis": {
21+
"kinesisSchemaVersion": "1.0",
22+
"partitionKey": "1",
23+
"sequenceNumber": "49590338271490256608559692540925702759324208523137515618",
24+
"data": "VGhpcyBpcyBvbmx5IGEgdGVzdC4=",
25+
"approximateArrivalTimestamp": 1545084711.166
26+
},
27+
"eventSource": "aws:kinesis",
28+
"eventVersion": "1.0",
29+
"eventID": "shardId-000000000006:49590338271490256608559692540925702759324208523137515618",
30+
"eventName": "aws:kinesis:record",
31+
"invokeIdentityArn": "arn:aws:iam::123456789012:role/lambda-role",
32+
"awsRegion": "us-east-2",
33+
"eventSourceARN": "arn:aws:kinesis:us-east-2:123456789012:stream/lambda-stream"
34+
}
35+
]
36+
}

function/lambda_function.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import os
2+
import logging
3+
import jsonpickle
4+
import boto3
5+
from aws_xray_sdk.core import patch_all
6+
import os
7+
from neo4j import GraphDatabase
8+
9+
neo4j_uri = os.environ.get('NEO4J_URI')
10+
neo4j_user = os.environ.get('NEO4J_USERNAME')
11+
neo4j_password = os.environ.get('NEO4J_PASSWORD')
12+
13+
logger = logging.getLogger()
14+
logger.setLevel(logging.INFO)
15+
patch_all()
16+
17+
if neo4j_uri is None or neo4j_user is None or neo4j_password is None:
18+
logger.info('## ENVIRONMENT VARIABLES\r' + jsonpickle.encode(dict(**os.environ)))
19+
raise Exception("Missing environment variables for connecting to Aura; double check!")
20+
21+
driver = GraphDatabase.driver(neo4j_uri, auth=(neo4j_user, neo4j_password),
22+
max_connection_lifetime=5*60,
23+
keep_alive=True)
24+
25+
client = boto3.client('lambda')
26+
client.get_account_settings()
27+
28+
def write_graph_record(tx, event):
29+
val = jsonpickle.encode(event)
30+
tx.run("""
31+
MERGE (e:LambdaEvent { created: datetime(), event: $event })
32+
""", event=jsonpickle.encode(event))
33+
34+
def lambda_handler(event, context):
35+
logger.info('## ENVIRONMENT VARIABLES\r' + jsonpickle.encode(dict(**os.environ)))
36+
logger.info('## EVENT\r' + jsonpickle.encode(event))
37+
logger.info('## CONTEXT\r' + jsonpickle.encode(context))
38+
logger.info("## CONNECTING TO %s" % neo4j_uri)
39+
40+
with driver.session() as session:
41+
session.write_transaction(write_graph_record, event)
42+
43+
response = client.get_account_settings()
44+
return response['AccountUsage']

function/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
jsonpickle==1.3
2+
neo4j==4.3.5
3+
aws-xray-sdk==2.8.0

template.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: 'AWS::Serverless-2016-10-31'
3+
Description: An AWS Lambda application that calls the Lambda API.
4+
Resources:
5+
function:
6+
Type: AWS::Serverless::Function
7+
Properties:
8+
Handler: lambda_function.lambda_handler
9+
Runtime: python3.10
10+
CodeUri: function/.
11+
Description: Call the AWS Lambda API
12+
Timeout: 600
13+
Policies:
14+
- AWSLambdaBasicExecutionRole
15+
- AWSLambda_ReadOnlyAccess
16+
- AWSXrayWriteOnlyAccess
17+
Tracing: Active
18+
Layers:
19+
- !Ref libs
20+
libs:
21+
Type: AWS::Serverless::LayerVersion
22+
Properties:
23+
LayerName: aura-lambda-python-lib
24+
Description: Dependencies for the aura-lambda sample app.
25+
ContentUri: package/.
26+
CompatibleRuntimes:
27+
- python3.10

0 commit comments

Comments
 (0)