Responding an image from lambda

0

I am trying to develop an application in which I am trying to respond an image/file on an api call to the lambda through api gateway.

I am trying to use matplotlib and pandas layers to generate a figure ,save it and then respond as a FileResponse (fastapi) . But its not working .

Data is being taken from dynamodb.

The lambda is itself not working in saving the figure .

I would appreciate any help.

3 réponses
0

What is the error you are getting? Where is your Lambda trying to save the image to? Local file? Lambda only supports writing files to /tmp folder.

profile pictureAWS
EXPERT
Uri
répondu il y a un an
  • Matplotlib created a temporary config/cache directory at /tmp/matplotlib-8j65adly because the default path (/home/sbx_user1051/.config/matplotlib) is not a writable directory; it is highly recommended to set the MPLCONFIGDIR environment variable to a writable directory, in particular to speed up the import of Matplotlib and to better support multiprocessing.

    This the error I am getting.

  • import json import boto3 import pandas as pd from matplotlib import pyplot as plt from boto3.dynamodb.conditions import Key from datetime import datetime

    dynamo=boto3.resource('dynamodb') table=dynamo.Table('airplotdata')

    def lambda_handler(event, context): # TODO implement response=table.query(KeyConditionExpression=Key('id').eq('12468'), ProjectionExpression="#c,aqi,co,so2,pm25,pm10", ExpressionAttributeNames = {'#c': 'timestamp'}) items=response['Items']

    df=pd.DataFrame(items)
    
    df['timestamp'] = df['timestamp'].apply(lambda x: datetime.strptime(x, '%Y-%m-%d %H:%M:%S'))
    
    dfm = df.melt('timestamp', var_name='metrics', value_name='metric_vals')
    
    dfm.plot(kind='line',figsize=(12,12))
    
    plt.savefig("/tmp/output")
    
    
    
    
    
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }
    

    This is my code.

  • I am having problem in plt.savefig("/tmp/output")

0

Got a successful response after 5 seconds of computation the function time out was 3 seconds.

répondu il y a un an
  • So you need to change the function timeout :)

0

as I understand your lambda is responsible for the creation of the image and save it on /tmp folder? or on S3 ?

répondu il y a un an

Vous n'êtes pas connecté. Se connecter pour publier une réponse.

Une bonne réponse répond clairement à la question, contient des commentaires constructifs et encourage le développement professionnel de la personne qui pose la question.

Instructions pour répondre aux questions