Transcribe Your Amazon Connect Recordings

This guide walks through the process of setting up a transcription pipeline for Amazon Connect recordings using AssemblyAI.

Get Started

Before we begin, make sure you have:

  • An AssemblyAI account and an API key. You can sign up for a free account and get your API key from your dashboard.
  • An AWS account.
  • An Amazon Connect instance.

Step-by-Step Instructions

1

In the AWS console, navigate to the Amazon Connect services page. Select your instance and then click into the Data Storage section. On this page, find the subsection named Call Recordings and note the S3 bucket path where your call recordings are stored, you’ll need this for later.

2

Navigate to the Lambda services page, and create a new function. Set the runtime to Python 3.13. In the Change default execution role section, choose the option to create a new role with basic Lambda permissions. Assign a function name and then click Create function.

3

In this new function, scroll down to the Code Source section and paste the following code into lambda_function.py.

1import json
2import os
3import boto3
4import http.client
5import time
6from urllib.parse import unquote_plus
7import logging
8
9# Configure logging
10
11logger = logging.getLogger()
12logger.setLevel(logging.INFO)
13
14# Configuration settings
15
16# See config parameters here: https://www.assemblyai.com/docs/api-reference/transcripts/submit
17
18ASSEMBLYAI_CONFIG = {
19
20# 'language_code': 'en_us',
21
22# 'multichannel': True,
23
24# 'redact_pii': True,
25
26}
27
28# Initialize AWS services
29
30s3_client = boto3.client('s3')
31
32def get_presigned_url(bucket, key, expiration=3600):
33"""Generate a presigned URL for the S3 object"""
34
35logger.info({
36"message": "Generating presigned URL",
37"bucket": bucket,
38"key": key,
39"expiration": expiration
40})
41
42s3_client_with_config = boto3.client(
43's3',
44config=boto3.session.Config(signature_version='s3v4')
45)
46
47return s3_client_with_config.generate_presigned_url(
48'get_object',
49Params={'Bucket': bucket, 'Key': key},
50ExpiresIn=expiration
51)
52
53def delete_transcript_from_assemblyai(transcript_id, api_key):
54"""
55Delete transcript data from AssemblyAI's database using their DELETE endpoint.
56
57Args:
58transcript_id (str): The AssemblyAI transcript ID to delete
59api_key (str): The AssemblyAI API key
60
61Returns:
62bool: True if deletion was successful, False otherwise
63"""
64headers = {
65"authorization": api_key,
66"content-type": "application/json"
67}
68
69conn = http.client.HTTPSConnection("api.assemblyai.com")
70
71try: # Send DELETE request to AssemblyAI API
72conn.request("DELETE", f"/v2/transcript/{transcript_id}", headers=headers)
73response = conn.getresponse()
74
75 # Check if deletion was successful (HTTP 200)
76 if response.status == 200:
77 response_data = json.loads(response.read().decode())
78 logger.info(f"Successfully deleted transcript {transcript_id} from AssemblyAI")
79 return True
80 else:
81 error_message = response.read().decode()
82 logger.error(f"Failed to delete transcript {transcript_id}: HTTP {response.status} - {error_message}")
83 return False
84
85except Exception as e:
86logger.info(f"Error deleting transcript {transcript_id}: {str(e)}")
87return False
88finally:
89conn.close()
90
91def transcribe_audio(audio_url, api_key):
92"""Transcribe audio using AssemblyAI API with http.client"""
93logger.info({"message": "Starting audio transcription"})
94
95headers = {
96"authorization": api_key,
97"content-type": "application/json"
98}
99
100conn = http.client.HTTPSConnection("api.assemblyai.com")
101
102# Submit the audio file for transcription with config parameters
103
104request_data = {"audio_url": audio_url}
105
106# Add all configuration settings
107
108request_data.update(ASSEMBLYAI_CONFIG)
109
110json_data = json.dumps(request_data)
111conn.request("POST", "/v2/transcript", json_data, headers)
112response = conn.getresponse()
113
114if response.status != 200:
115raise Exception(f"Failed to submit audio for transcription: {response.read().decode()}")
116
117response_data = json.loads(response.read().decode())
118transcript_id = response_data['id']
119logger.info({"message": "Audio submitted for transcription", "transcript_id": transcript_id})
120
121# Poll for transcription completion
122
123while True:
124conn = http.client.HTTPSConnection("api.assemblyai.com")
125conn.request("GET", f"/v2/transcript/{transcript_id}", headers=headers)
126polling_response = conn.getresponse()
127polling_data = json.loads(polling_response.read().decode())
128
129 if polling_data['status'] == 'completed':
130 conn.close()
131 logger.info({"message": "Transcription completed successfully"})
132 return polling_data # Return full JSON response instead of just text
133 elif polling_data['status'] == 'error':
134 conn.close()
135 raise Exception(f"Transcription failed: {polling_data['error']}")
136
137 conn.close()
138 time.sleep(3)
139
140def lambda_handler(event, context):
141"""Lambda function to handle S3 events and process audio files"""
142try: # Get the AssemblyAI API key from environment variables
143api_key = os.environ.get('ASSEMBLYAI_API_KEY')
144if not api_key:
145raise ValueError("ASSEMBLYAI_API_KEY environment variable is not set")
146
147 # Process each record in the S3 event
148 for record in event.get('Records', []):
149 # Get the S3 bucket and key
150 bucket = record['s3']['bucket']['name']
151 key = unquote_plus(record['s3']['object']['key'])
152
153 # Generate a presigned URL for the audio file
154 audio_url = get_presigned_url(bucket, key)
155
156 # Get the full transcript JSON from AssemblyAI
157 transcript_data = transcribe_audio(audio_url, api_key)
158
159 # Prepare the transcript key - maintaining path structure but changing directory and extension
160 transcript_key = key.replace('/CallRecordings/', '/AssemblyAITranscripts/', 1).replace('.wav', '.json')
161
162 # Convert the JSON data to a string
163 transcript_json_str = json.dumps(transcript_data, indent=2)
164
165 # Upload the transcript JSON to the same bucket but in transcripts directory
166 s3_client.put_object(
167 Bucket=bucket, # Use the same bucket
168 Key=transcript_key,
169 Body=transcript_json_str,
170 ContentType='application/json'
171 )
172 logger.info({"message": "Transcript uploaded to transcript bucket successfully.", "key": transcript_key})
173
174 # Uncomment the following line to delete transcript data from AssemblyAI after saving to S3
175 # https://www.assemblyai.com/docs/api-reference/transcripts/delete
176 # delete_transcript_from_assemblyai(transcript_data['id'], api_key)
177
178 return {
179 "statusCode": 200,
180 "body": json.dumps({
181 "message": "Audio file(s) processed successfully",
182 "detail": "Transcripts have been stored in the AssemblyAITranscripts directory"
183 })
184 }
185
186except Exception as e:
187print(f"Error: {str(e)}")
188return {
189"statusCode": 500,
190"body": json.dumps({
191"message": "Error processing audio file(s)",
192"error": str(e)
193})
194}
4

At the top of the lambda function, you can edit the config to enable features for your transcripts. To see all available parameters, check out our API reference.

1ASSEMBLYAI_CONFIG = {
2 # 'language_code': 'en_us',
3 # 'multichannel': True,
4 # 'redact_pii': True,
5}

If you would like to delete transcripts from AssemblyAI after completion, you can uncomment line 166 to enable the delete_transcript_from_assemblyai function. This ensures the transcript data is only saved on your S3 database and not stored on AssemblyAI’s database.

Once you have finished editing the lambda function, click Deploy to save your changes.

5

On the same page, navigate to the Configuration section, under General configuration adjust the timeout to 15min 0sec and click Save. The processing times for transcription will be a lot shorter, but this ensures plenty of time for the function to complete.

6

Now from this page, on the left side panel click Environment variables. Click edit and then add an environment variable, ASSEMBLYAI_API_KEY, and set the value to your AssemblyAI API key. Then click Save.

7

Now, navigate to the IAM services page. On the left side panel under Access Management click Roles and search for your Lambda function role (it’s structure should look like function_name-role-id). Click into the role and then in the Permissions policies section click the dropdown for Add permissions and then select Attach policies. From this page, find the policy named AmazonS3FullAccess and click Add permissions.

8

Now, navigate to the S3 services page and click into the general purpose bucket where your Amazon Connect recordings are stored. Browse to the Properties tab and then scroll down to Event notifications. Click Create event notification. Give the event a name and then in the prefix section, insert the folder path we noted from Step 1 to ensure the event is triggered for the correct folder.

Then in the Event types section, select All object create events.

Then scroll down to the Destination section, set the destination as Lambda function and then select the Lambda function we created in Step 2. Then click Save changes.

9

To finalise the integration, we’ll need to set the recording behaviour from within your AWS Contact Flows. Navigate to your Amazon Connect instance access URL and sign in to your Admin account. In the left side panel, navigate to the Routing section and then select Flows. Choose a flow to test with, in this case we’ll utilize the Sample inbound flow (first contact experience). You should see the Block Library on the left hand side of the page. In this section, search for Set recording and analytics behaviour and then drag the block into your flow diagram and connect the arrows. You can see in our example, we place the block right at the entry of the call flow:

After connecting this block, click the 3 vertical dots in the top right of the block and select Edit settings. Scroll down to the Enable recording and analytics subsection and expand the Voice section. Then select On and select Agent and customer (or whoever you’d like to record). Then click Save, click Save again in the top right and then click Publish to publish the flow.

With this new flow published, you should now receive recordings for your Amazon Connect calls that utilize that flow, and you should now receive AssemblyAI transcripts for those recordings!

The Amazon Connect Call Recordings are saved in the S3 bucket with this naming convention: /connect/{instance-name}/CallRecordings/{YYYY}/{MM}/{DD}/{contact-id}_{YYYYMMDDThh:mm}_UTC.wav

The AssemblyAI Transcripts will be saved in the S3 bucket with this naming convention: /connect/{instance-name}/AssemblyAITranscripts/{YYYY}/{MM}/{DD}/{contact-id}_{YYYYMMDDThh:mm}_UTC.json

10

To view the logs for this integration, navigate to the CloudWatch services page and under the Logs section, select Log groups. Select the log group that matches your Lambda to view the most recent log stream.