How can invoke a Lambda function with AWS Signature auth without using Postman?

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{
"text": "Test text to speech"
}
</code>
<code>{ "text": "Test text to speech" } </code>
{ 
    "text": "Test text to speech"
}

And here is the function code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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)}')
}
</code>
<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)}') } </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)}')
        }

New contributor

Talib Kareem is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật