Search code examples
amazon-web-servicesamazon-s3amazon-rdsamazon-cloudwatchaws-xray

How to configure AWS RDS and S3 bucket to be visible in CouldWatch Service map?


Please look at these 2 images

Current Situation

Desired Situation

So for the lambda code itself, here is my S3 Lambda code connected with the API Gateway in Node.js

const AWS = require('aws-sdk');
const XRay = require('aws-xray-sdk'); // Import the X-Ray SDK
const S3 = XRay.captureAWSClient(new AWS.S3());

exports.handler = async (event) => {
try {
    // Extract picture data from the event (e.g., base64 encoded data)
    const pictureData = event.pictureData.toString();

    // Specify the S3 bucket and object key
    const bucketName = 'MyBucketName';
    const objectKey = event.pictureKey + '.jpg'; // Adjust the file extension accordingly

    // Convert the picture data to a buffer
    const buffer = Buffer.from(pictureData.replace(/^data:image\/\w+;base64,/, ""), 'base64');

    console.log(buffer);
    console.log(pictureData);

    await XRay.captureAsyncFunc('S3Upload', async (subsegment) => {
        // Upload the picture to S3
        await S3.upload({
            Bucket: bucketName,
            Key: objectKey,
            Body: buffer,
            ACL: 'public-read',
            ContentType: 'image/jpeg',
            ContentEncoding: 'base64'
        }).promise();

        subsegment.close(); // Close the subsegment when the operation is done
    });

    const s3ImageLink = 'https://giftcardshop-group55-ddac.s3.amazonaws.com/' + objectKey;

    return {
        statusCode: 200,
        body: JSON.stringify(s3ImageLink),
    };
} catch (err) {
    console.error('Error uploading picture:', err);
    return {
        statusCode: 500,
        body: JSON.stringify('Error uploading picture'),
    };
}
};

The code works, it insert data to my S3 bucket on the API call, but it just not visible on the CloudWatch service map, anyone knows how to fix this ?


Solution

  • This is a known issue with the S3.upload command specifically (due to it's design in the AWS SDK library) and is documented in this Github issue. You can try this as a workaround:

    const XRay = require('aws-xray-sdk');
    const AWS = XRay.captureAWS(require('aws-sdk'));
    const S3 = new AWS.S3();
    

    It also seems that this issue is fixed in the AWS SDK v3, which is now being recommended over v2.