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