Skip to content

Commit 2122a04

Browse files
committed
Adding the apigw-lambda-transcribe-sam-js branch
1 parent e7bb1d0 commit 2122a04

File tree

4 files changed

+235
-0
lines changed

4 files changed

+235
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Amazon API Gateway to AWS Lambda to Amazon Transcribe using AWS SAM
2+
3+
This pattern facilitates audio transcription by using Amazon Transcribe service through a serverless API endpoint. When audio files are uploaded to S3, they can be transcribed using Amazon Transcribe via an API Gateway endpoint backed by Lambda.
4+
5+
This pattern enables speech-to-text transcription use cases by providing a serverless API endpoint that can process audio files stored in S3. The pattern uses AWS Lambda to coordinate with Amazon Transcribe service, making it easy to integrate transcription capabilities into your applications.
6+
7+
Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.
8+
9+
## Requirements
10+
11+
* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
12+
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
13+
* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
14+
* [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed
15+
16+
## Deployment Instructions
17+
18+
1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
19+
```
20+
git clone https://github.com/aws-samples/serverless-patterns
21+
```
22+
1. Change directory to the pattern directory:
23+
```
24+
cd apigw-lambda-transcribe-sam-js
25+
```
26+
1. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yaml file:
27+
```
28+
sam deploy --guided
29+
```
30+
1. During the prompts:
31+
* Enter a stack name
32+
* Enter the desired AWS Region
33+
* Allow SAM CLI to create IAM roles with the required permissions.
34+
35+
Once you have run `sam deploy --guided` mode once and saved arguments to a configuration file (samconfig.toml), you can use `sam deploy` in future to use these defaults.
36+
37+
2. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing.
38+
39+
## How it works
40+
41+
The pattern creates an API Gateway endpoint that accepts POST requests with JSON payloads containing an S3 URL of an audio file. When a request is received:
42+
43+
1. API Gateway forwards the request to AWS Lambda
44+
2. Lambda function starts a transcription job using Amazon Transcribe
45+
3. Amazon Transcribe processes the audio file and generates the transcription
46+
4. The transcription results are stored in the specified S3 bucket
47+
48+
## Testing
49+
50+
To test the deployed API endpoint:
51+
52+
1. Upload an audio file to the created S3 bucket:
53+
```
54+
aws s3 cp audio.mp3 s3://your-bucket-name/
55+
```
56+
2. Get the S3 URL of the uploaded audio file
57+
3. Make a POST request to the API Gateway endpoint with the following JSON payload:
58+
59+
```bash
60+
curl -X POST https://your-api-endpoint/Prod/transcribe \
61+
-H "Content-Type: application/json" \
62+
-d '{"audio_url": "s3://your-bucket-name/audio.mp3"}'
63+
```
64+
4. The API will return a response with the transcription job name and status
65+
```json
66+
{
67+
"job_name": "transcribe-12345678-1234-5678-1234-567812345678",
68+
"status": "IN_PROGRESS"
69+
}
70+
```
71+
5. You can check the transcription results in the S3 bucket once the job is complete:
72+
73+
```bash
74+
aws transcribe get-transcription-job --transcription-job-name "job-name-from-response"
75+
```
76+
## Cleanup
77+
78+
1. Delete the stack
79+
```bash
80+
sam delete
81+
```
82+
83+
----
84+
Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.
85+
86+
SPDX-License-Identifier: MIT-0
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"title": "Amazon API Gateway to AWS Lambda to Amazon Transcribe using AWS SAM",
3+
"description": "This pattern creates a serverless API endpoint using API Gateway and Lambda to transcribe audio files stored in S3 using Amazon Transcribe.",
4+
"language": "Javascript",
5+
"level": "200",
6+
"framework": "SAM",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"When a POST request is made to the API Gateway endpoint with an S3 audio file URL, it triggers a Lambda function that starts a transcription job using Amazon Transcribe. The Lambda function sends the audio file location to Amazon Transcribe service, which processes the audio and generates text transcription that can be retrieved from the specified S3 bucket."
11+
]
12+
},
13+
"gitHub": {
14+
"template": {
15+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/apigw-lambda-transcribe-sam-js",
16+
"templateURL": "serverless-patterns/apigw-lambda-transcribe-sam-js",
17+
"projectFolder": "apigw-lambda-transcribe-sam-js",
18+
"templateFile": "template.yaml"
19+
}
20+
},
21+
"resources": {
22+
"bullets": [
23+
{
24+
"text": "Amazon S3",
25+
"link": "https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html"
26+
},
27+
{
28+
"text": "AWS Lambda",
29+
"link": "https://docs.aws.amazon.com/lambda/latest/dg/welcome.html"
30+
},
31+
{
32+
"text": "Amazon Transcribe",
33+
"link": "https://docs.aws.amazon.com/transcribe/latest/dg/what-is.html"
34+
}
35+
]
36+
},
37+
"deploy": {
38+
"text": [
39+
"sam build",
40+
"sam deploy --guided"
41+
]
42+
},
43+
"testing": {
44+
"text": [
45+
"See the GitHub repo for detailed testing instructions."
46+
]
47+
},
48+
"cleanup": {
49+
"text": [
50+
"sam delete"
51+
]
52+
},
53+
"authors": [
54+
{
55+
"name": "Achintya Veer Singh",
56+
"image": "https://avatars.githubusercontent.com/u/55053737?v=4",
57+
"bio": "Solutions Architect @ AWS",
58+
"linkedin": "www.linkedin.com/in/achintya-veer-singh-493403193",
59+
"twitter": "achintya_veer"
60+
}
61+
]
62+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { TranscribeClient, StartTranscriptionJobCommand } from "@aws-sdk/client-transcribe";
2+
import { randomUUID } from 'crypto';
3+
4+
export const lambda_handler = async (event, context) => {
5+
try {
6+
const requestBody = JSON.parse(event.body);
7+
const s3_url = requestBody.audio_url;
8+
9+
const transcribe = new TranscribeClient();
10+
const job_name = `transcribe-${randomUUID()}`;
11+
12+
const command = new StartTranscriptionJobCommand({
13+
TranscriptionJobName: job_name,
14+
Media: { MediaFileUri: s3_url },
15+
MediaFormat: 'mp3', // Adjust based on your needs
16+
LanguageCode: 'en-US' // Adjust based on your needs
17+
});
18+
19+
await transcribe.send(command);
20+
21+
return {
22+
statusCode: 200,
23+
body: JSON.stringify({
24+
job_name: job_name,
25+
status: 'IN_PROGRESS'
26+
})
27+
};
28+
29+
} catch (e) {
30+
return {
31+
statusCode: 500,
32+
body: JSON.stringify({
33+
error: e.toString()
34+
})
35+
};
36+
}
37+
};
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: Audio transcription using Amazon Transcribe
4+
5+
Resources:
6+
TranscribeFunction:
7+
Type: AWS::Serverless::Function
8+
Properties:
9+
CodeUri: src/
10+
Handler: app.lambda_handler
11+
Runtime: nodejs20.x
12+
Timeout: 900
13+
MemorySize: 128
14+
Policies:
15+
- Statement:
16+
- Effect: Allow
17+
Action:
18+
- transcribe:StartTranscriptionJob
19+
- transcribe:GetTranscriptionJob
20+
Resource: '*'
21+
- Statement:
22+
- Effect: Allow
23+
Action:
24+
- s3:GetObject
25+
- s3:PutObject
26+
Resource: !Sub 'arn:aws:s3:::${AudioBucket}/*'
27+
Environment:
28+
Variables:
29+
BUCKET_NAME: !Ref AudioBucket
30+
Events:
31+
TranscribeAPI:
32+
Type: Api
33+
Properties:
34+
Path: /transcribe
35+
Method: post
36+
37+
AudioBucket:
38+
Type: AWS::S3::Bucket
39+
Properties:
40+
CorsConfiguration:
41+
CorsRules:
42+
- AllowedHeaders: ['*']
43+
AllowedMethods: [GET, PUT, POST]
44+
AllowedOrigins: ['*']
45+
MaxAge: 3000
46+
47+
Outputs:
48+
TranscribeApi:
49+
Description: API Gateway endpoint URL
50+
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/transcribe"

0 commit comments

Comments
 (0)