Azure speech to text not working in AWS Lambda

MH 5 Reputation points

Azure STT Python SDK returns "Reason.Cancelled" automatically after starting the transcription.
I am using it in AWS Lambda environment.

Here is my code:

def speech_recognize_continuous_from_file():
        results = []
        """performs continuous speech recognition with input from an audio file"""
        # <SpeechContinuousRecognitionWithFile>
        speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
        # Ask for detailed recognition result
        speech_config.output_format = speechsdk.OutputFormat.Detailed

        # If you also want word-level timing in the detailed recognition results, set the following.
        # Note that if you set the following, you can omit the previous line
        #   "speech_config.output_format = speechsdk.OutputFormat.Detailed",
        # since word-level timing implies detailed recognition results.
        audio_config =

        speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)

        def new_func(evt):


        #CustomPhrases = ["villusectomy", "Villa","abranchi","ataabranchi","ateabrasio"]
        CustomPhrases = event["CustomPhrases"]

        phrase_list_grammar = speechsdk.PhraseListGrammar.from_recognizer(speech_recognizer)

        for phr in CustomPhrases:

        done = False

        def stop_cb(evt: speechsdk.SessionEventArgs):

            nonlocal done
            done = True

        # Connect callbacks to the events fired by the speech recognizer

        speech_recognizer.recognized.connect(lambda evt: new_func(evt))

        # stop continuous recognition on either session stopped or canceled events

        # Start continuous speech recognition
        while not done:

        # </SpeechContinuousRecognitionWithFile>



        return results
Azure AI Speech
Azure AI Speech
An Azure service that integrates speech processing into apps and services.
1,444 questions
{count} vote

1 answer

Sort by: Most helpful
  1. YutongTie-MSFT 46,996 Reputation points

    Hello @MH Thanks for reaching out to us. If you are having issues with Azure Speech-to-Text not working in AWS Lambda, there could be a few possible reasons:

    1. Network connectivity: Lambda functions run inside a VPC (Virtual Private Cloud) by default, which means they may not have internet access unless you explicitly configure the VPC to allow outbound internet traffic. If Azure Speech-to-Text is not accessible from within your Lambda function, this could be due to network connectivity issues.
    2. Authentication: Azure Speech-to-Text requires an authentication token to access the service. If you are not passing the correct credentials or if the token has expired, the service will not work. Make sure you have provided the correct subscription key and region for your Azure Speech-to-Text service.
    3. Runtime dependencies: The Azure Speech-to-Text SDK has some runtime dependencies, such as the pyaudio library, that may not be available in the AWS Lambda runtime environment. Make sure all required dependencies are included in your deployment package.
    4. Resource limitations: AWS Lambda functions have limits on the amount of memory and CPU time they can use. If your function is running out of resources while trying to transcribe speech, it may not work correctly.

    To troubleshoot the issue, you can start by checking the CloudWatch logs for your Lambda function to see if there are any error messages or exceptions being thrown. You can also try running the same code outside of Lambda to see if it works correctly, and make sure all dependencies are included in your deployment package.

    Please have a try and let me know is you still see more issue. If you are still blocked by this issue, we would like to recommend you raise a support ticket for this issue. Let us know if you have no support plan and we are happy to enable you a free ticket for this issue.

    Regards, Yutong

    -Please kindly accept the answer if you feel helpful to support the community, thanks a lot.

    0 comments No comments