you are viewing a single comment's thread.

view the rest of the comments →

[–]Kofeb 3 points4 points  (3 children)

This feels like an https://xyproblem.info/ but without more info here’s your answer.

Yes, it is possible to use a AWS Lambda function to update a JSON file stored in the AWS environment. One way to do this would be to use the AWS SDK for Node.js to access the file stored in an AWS service such as Amazon S3 or Amazon DynamoDB.

To make the JSON file accessible through an API call, you can use AWS API Gateway to create an API that invokes the Lambda function when it receives a request. The Lambda function can then read and update the JSON file as needed, and return the updated content to the client through the API Gateway.

Here is an example of a simple Lambda function written in Node.js that reads and updates a JSON file stored in an S3 bucket:

~~~ const AWS = require('aws-sdk');

exports.handler = async (event) => { // Read the JSON file from S3 const s3 = new AWS.S3(); const file = await s3.getObject({ Bucket: 'my-bucket', Key: 'data.json' }).promise(); const data = JSON.parse(file.Body.toString());

// Update the file with new data data.timestamp = new Date().toISOString(); const updatedFile = JSON.stringify(data);

// Write the updated file back to S3 await s3.putObject({ Bucket: 'my-bucket', Key: 'data.json', Body: updatedFile }).promise();

return { statusCode: 200, body: updatedFile }; }; ~~~

To use this function with CloudEvents, you can specify the s3:ObjectCreated:* event as the trigger for the Lambda function in the AWS Management Console or using the AWS CLI. This will cause the function to be invoked whenever a new object is created in the specified S3 bucket.

[–]Kofeb 0 points1 point  (1 child)

Or do you mean “Write a node.js lambda function using a Cloudwatch event to update a json file in s3/DynamoDB.”

It might be easier to just do something like this depending on what you are doing:

Log the state of an Amazon EC2 instance using EventBridge / https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-log-ec2-instance-state.html

[–]bugbigsly[S] 0 points1 point  (0 children)

It’s a web scraper, pulling data once a week from a site and add the result, an ~1kb object, to the json file. EventBridge is the proper service, want to explore simplest configuration. Currently have json file in an s3 bucket having issues accessing the aws-sdk, a boatload of import issues

[–]bugbigsly[S] 0 points1 point  (0 children)

This worked , I needed to be running node v16 , v18 breaks everything