Skip to content

Commit 99e3841

Browse files
committed
Push json object to s3
1 parent 6541634 commit 99e3841

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

lambda/lambda_function.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -138,26 +138,31 @@ def check_service_health(service_urls, access_token):
138138
})
139139
return health_status
140140

141-
def upload_file_to_s3(file_name, bucket_name, object_name=None):
141+
142+
def upload_json_to_s3(json_data, bucket_name, object_name):
142143
"""
143-
Upload a file to an S3 bucket.
144+
Upload JSON data to an S3 bucket.
144145
145146
Parameters:
146-
- file_name (str): File to upload.
147+
- json_data (dict): JSON data to upload.
147148
- bucket_name (str): Bucket to upload to.
148-
- object_name (str): S3 object name. If not specified, file_name is used.
149+
- object_name (str): S3 object name.
149150
"""
150-
# If S3 object_name was not specified, use file_name
151-
if object_name is None:
152-
object_name = file_name
153-
154151
# Create an S3 client
155152
s3_client = boto3.client('s3')
156153

157154
try:
158-
# Upload the file
159-
response = s3_client.upload_file(file_name, bucket_name, object_name)
160-
return True, "File uploaded successfully."
155+
# Convert the JSON data to a string
156+
json_string = json.dumps(json_data)
157+
158+
# Upload the JSON string to S3
159+
response = s3_client.put_object(
160+
Bucket=bucket_name,
161+
Key=object_name,
162+
Body=json_string,
163+
ContentType='application/json'
164+
)
165+
return True, "JSON uploaded successfully."
161166
except Exception as e:
162167
# The upload failed; return False and the error
163168
return False, str(e)
@@ -205,14 +210,11 @@ def lambda_handler(event, context):
205210
now = datetime.datetime.now()
206211
filename = now.strftime("health_check_%Y-%m-%d_%H-%M-%S.json")
207212

208-
# Write the result to a JSON file
209-
with open(filename, 'w') as file:
210-
json.dump(health_status, file, indent=4)
211-
212-
# Upload the file to S3
213-
bucket_name = 'mgmt-13l4zrzw'
214-
upload_message = upload_file_to_s3(filename, bucket_name)
215-
print("Upload Status:", upload_message)
213+
# Upload the JSON data to S3
214+
bucket_name = 'mgmt-13l4zrzw'
215+
upload_status, upload_message = upload_json_to_s3(health_status, bucket_name, filename)
216+
print("Upload Status:", upload_message)
217+
216218

217219
# Output the result as JSON
218220
return {

0 commit comments

Comments
 (0)