I have a simple Lambda function in Python that converts a text string into speech and saves it to an S3 bucket. I want to then get that mp3 file (or URL) as a response.
I will invoke the Lambda function from an HTTP Request (POST or PUT?) with AWS Signature auth. I believe I set the permissions correct on the user and the function role.
I get no errors when using Postman with the credentials, it returns a 200 status. But I’m not understanding how to do the request outside of testing on Postman. I just get 400 errors like:
“The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.”
I’ve tried on Cloudshell (after aws config), I copied the cURL code snippet from Postman….didn’t work,
I also tried using Make.com’s Lambda module and signed in with my key there, didn’t work, for some reason it didn’t seem to recognize the body of the request so it didn’t output anything (even though it was there).
Any help is appreciated. I’m learning about AWS and it’s a bit overwhelming to me. All I’m trying to do is get this converted TTS mp3 file in (close to) real-time if possible.
This is a screenshot of the cURL code snippet from Postman
Here is just the body of the request:
{
"text": "Test text to speech"
}
And here is the function code:
import json
import boto3
def lambda_handler(event, context):
polly = boto3.client('polly')
s3 = boto3.client('s3')
# Check if 'body' key exists in the event
if 'body' not in event:
return {
'statusCode': 400,
'body': json.dumps('Error: Request body is missing')
}
# Parse the body to extract the text
body = json.loads(event['body'])
if 'text' not in body:
return {
'statusCode': 400,
'body': json.dumps('Error: text field is required in the request body')
}
text = body['text']
bucket_name = 'pollytts--use1-az6--x-s3'
object_key = 'converted-speech.mp3'
try:
# Synthesize speech using Amazon Polly
response = polly.synthesize_speech(
Text=text,
OutputFormat='mp3',
VoiceId='Joanna'
)
# Get the audio stream from the response
audio_stream = response['AudioStream'].read()
# Upload the audio stream to S3
s3.put_object(
Bucket=bucket_name,
Key=object_key,
Body=audio_stream,
ContentType='audio/mpeg'
)
# Generate the URL for the uploaded file
file_url = f'https://{bucket_name}.s3.amazonaws.com/{object_key}'
return {
'statusCode': 200,
'body': json.dumps({'url': file_url})
}
except Exception as e:
return {
'statusCode': 500,
'body': json.dumps(f'Error: {str(e)}')
}
Talib Kareem is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.