Am I able to save a Canaries "text/xml" responseBody to S3

0

I have Canaries set that monitor feeds and retain 24h of the responses; When performing this for feeds with the mime type "text/xml", the responseBody log in the HttpRequestReport states "Content Type is not supported"

Please advise how I may retain the XML data from a Canary run where save resposneBody is enabled

Mike D
asked 7 months ago378 views
1 Answer
1
Accepted Answer

Hi Mike,

Let's see if I can help you!

The error "Content Type is not supported" suggests that while your Canary is trying to log the response body, the built-in CloudWatch Synthetics functionality does not natively support the "text/xml" content type for storing response data.

To retain the XML data from a Canary run, you can consider the following approach:

  1. Modify your script to manually capture the XML response.
  2. Within the same script, use the AWS SDK to store the XML data to an S3 bucket.

Here is a Python code:

import boto3
import os

# Initialize the S3 client
s3_client = boto3.client('s3')

# Set the bucket name from an environment variable.
# Make sure to set the 'BUCKET_NAME' environment variable in the AWS Lambda console.
BUCKET_NAME = os.environ['BUCKET_NAME']

def lambda_handler(event, context):
    # Assume the XML data comes as a string in the 'xml_data' field of the event.
    xml_data = event['xml_data']
    
    # Define the path where the XML file should be saved in S3. Replace 'path/to/your' with your desired path.
    object_name = 'path/to/your/xmlfile.xml'  

    try:
        # Upload the XML data to the specified S3 bucket and path.
        response = s3_client.put_object(Bucket=BUCKET_NAME, Key=object_name, Body=xml_data, ContentType='text/xml')
        return {
            'statusCode': 200,
            'message': f"XML saved successfully in {BUCKET_NAME}/{object_name}"
        }
    except Exception as e:
        print(e)
        return {
            'statusCode': 500,
            'message': 'Error saving XML to S3'
        }

Ensure the necessary IAM permissions are granted to the role associated with the Canary so that it can read and write to the desired S3 bucket.

I'll keep tracking your post to see if you've found a resolution.

profile picture
answered 7 months ago
profile pictureAWS
EXPERT
reviewed 7 months ago
  • Thanks Victor! step 2 is a bit unnecessary as you can simply direct the response to the logger as I have done, but your response got me there; thanks again!

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions