Skip to content

Commit 1f8eeb0

Browse files
almas-liaqatalmas-liaqat
authored andcommitted
Merge branch 'master'
2 parents c09b649 + fb27544 commit 1f8eeb0

File tree

7 files changed

+103
-16
lines changed

7 files changed

+103
-16
lines changed

lambda_typescript/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const handler = async (event: any): Promise<any> => {
66
const operation = event.httpMethod;
77
const dynamoDBTableName = process.env.DYNAMODB_TABLE_NAME || 'DefaultTableName';
88

9-
if (operation === 'POST') {
9+
if (operation === 'POST' && event.path === '/event') {
1010
// Handle the POST request to create a new item
1111
const params = {
1212
TableName: dynamoDBTableName,
@@ -26,7 +26,7 @@ export const handler = async (event: any): Promise<any> => {
2626
body: JSON.stringify('Error creating the item.'),
2727
};
2828
}
29-
} else if (operation === 'GET') {
29+
} else if (operation === 'GET' && event.path === '/event') {
3030
// Handle the GET request to retrieve an item
3131
const params = {
3232
TableName: dynamoDBTableName,
@@ -55,7 +55,7 @@ export const handler = async (event: any): Promise<any> => {
5555
body: JSON.stringify('Error retrieving the item.'),
5656
};
5757
}
58-
} else if (operation === 'DELETE') {
58+
} else if (operation === 'DELETE' && event.path === '/event') {
5959
// Handle the DELETE request to delete an item
6060
const params = {
6161
TableName: dynamoDBTableName,

openapitools.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"$schema": "./node_modules/@openapitools/openapi-generator-cli/config.schema.json",
3+
"spaces": 2,
4+
"generator-cli": {
5+
"version": "7.0.0"
6+
}
7+
}

terraform/batch-processing.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ module "batch-processing" {
33
environment = local.environment
44
prefix = local.prefix
55
short_prefix = local.short_prefix
6+
service_domain_name = module.api_gateway.service_domain_name
67
}

terraform/batch-processing/lambda.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,12 @@ resource "aws_lambda_function" "batch_processing_lambda" {
1616
handler = "batch_processing.lambda_handler"
1717
runtime = "python3.9"
1818
source_code_hash = data.archive_file.lambda_zip.output_base64sha256
19+
20+
environment {
21+
variables = {
22+
"SERVICE_DOMAIN_NAME" : var.service_domain_name
23+
}
24+
}
1925
}
26+
27+
Lines changed: 81 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,88 @@
1+
import http.client
12
from time import time
23
from boto3 import resource
4+
import json
5+
import uuid
6+
import os
37

48

5-
def lambda_handler(_event, _context):
6-
source_bucket_name = _event.get("Records")[0].get("s3").get("bucket").get("name")
7-
dest_bucket_name = source_bucket_name.replace("source", "destination")
8-
output_bucket = resource('s3').Bucket(dest_bucket_name)
9+
def lambda_handler(event, context):
10+
if "Records" in event and isinstance(event["Records"], list) and len(event["Records"]) > 0:
11+
response_list = []
912

10-
# Write some placeholder bytestring data to a file in the bucket,
11-
# so we can test that the lambda writes to the correct output bucket.
12-
filename = f"output_report_{time()}.txt"
13-
data = (b'Test file to see if the lambda writes to the correct s3 bucket. '
14-
b'If our AWS bill skyrockets, this file has been written to the wrong bucket!')
13+
for obj in event["Records"]:
14+
s3_record = obj["s3"]
15+
if "bucket" in s3_record and isinstance(s3_record["bucket"], dict):
16+
source_bucket_name = s3_record["bucket"].get("name")
1517

16-
output_bucket.put_object(Body=data, Key=filename)
18+
if "object" in s3_record and isinstance(s3_record["object"], dict):
19+
dest_bucket_name = source_bucket_name.replace("source", "destination")
20+
output_bucket = resource('s3').Bucket(dest_bucket_name)
21+
api_gateway_url = os.getenv("SERVICE_DOMAIN_NAME")
22+
object_path = s3_record["object"]
1723

18-
return {
19-
'statusCode': 200
20-
}
24+
try:
25+
headers = {
26+
"Content-Type": "application/json",
27+
"User-Agent": "batch-processing-lambda"
28+
}
29+
30+
request_body = json.dumps(object_path)
31+
filename = f"output_report_{time()}.txt"
32+
request_id = str(uuid.uuid4())
33+
34+
payload_for_output_bucket = {
35+
"timestamp": filename,
36+
"level": request_body,
37+
"request": {
38+
"x-request-id": request_id,
39+
"x-correlation-id": request_id
40+
}
41+
}
42+
43+
payload_for_api_gateway = {
44+
"id": request_id,
45+
"message": request_body
46+
}
47+
48+
payload_bytes_output_bucket = json.dumps(payload_for_output_bucket).encode('utf-8')
49+
payload_bytes_api_gateway = json.dumps(payload_for_api_gateway).encode('utf-8')
50+
51+
connection = http.client.HTTPSConnection(api_gateway_url)
52+
connection.request("POST", "/", payload_bytes_api_gateway, headers=headers)
53+
54+
response = connection.getresponse()
55+
json_data = json.loads(response.read().decode('utf-8'))
56+
connection.close()
57+
58+
response_object = {
59+
'statusCode': response.status,
60+
'json': json_data,
61+
'file': filename
62+
}
63+
64+
if response.status in [200, 201]:
65+
response_object.update({
66+
'body': 'Successfully sent JSON data to the API Gateway.'
67+
})
68+
else:
69+
response_object.update({
70+
'body': response.reason
71+
})
72+
output_bucket.put_object(Body=payload_bytes_output_bucket, Key="body")
73+
74+
response_list.append(response_object)
75+
76+
except Exception as e:
77+
output_bucket.put_object(Body=payload_bytes_output_bucket, Key="body")
78+
response_list.append({
79+
'statusCode': 500,
80+
'body': 'internal server error',
81+
'message': str(e)
82+
})
83+
return response_list
84+
else:
85+
return {
86+
'statusCode': 400,
87+
'body': 'Invalid event structure.'
88+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
variable "prefix" {}
22
variable "short_prefix" {}
33
variable "environment" {}
4+
variable "service_domain_name" {}

tests/test_lambda.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,11 @@ def test_lambda_crud_nhs_login(nhsd_apim_proxy_url, nhsd_apim_auth_headers):
6161

6262

6363
@pytest.mark.smoketest
64+
@pytest.mark.debug
6465
@pytest.mark.nhsd_apim_authorization({"access": "application", "level": "level3"})
6566
def test_lambda_crud_app_restricted(nhsd_apim_proxy_url, nhsd_apim_auth_headers):
6667
"""
6768
Test for the POST,GET and Delete for Lambda endpoints. Using app-restricted
6869
"""
70+
print(nhsd_apim_proxy_url)
6971
lambda_crud(nhsd_apim_proxy_url, nhsd_apim_auth_headers)

0 commit comments

Comments
 (0)