AWSTemplateFormatVersion: "2010-09-09" Description: Amazon Transcribe Post Call Analytics - PCA Server - FFMPEG Download Transform: AWS::Serverless-2016-10-31 Parameters: SupportFilesBucketName: Type: AWS::SSM::Parameter::Value Default: SupportFilesBucketName FFMPEGZipName: Type: String Default: ffmpeg.zip ffmpegDownloadUrl: Type: String Default: https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-amd64-static.tar.xz Description: URL for ffmpeg binary distribution tar file download - see https://www.johnvansickle.com/ffmpeg/ Resources: ffmpegZipFunctionRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: Service: lambda.amazonaws.com Action: sts:AssumeRole ManagedPolicyArns: - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole Policies: - PolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Resource: !Sub - 'arn:aws:s3:::${bucket}*' - bucket: !Ref SupportFilesBucketName Action: - 's3:PutObject' PolicyName: ffmpegZipFunctionS3Policy ffmpegZipFunction: Type: AWS::Lambda::Function Properties: Handler: index.handler Runtime: python3.8 Role: !GetAtt 'ffmpegZipFunctionRole.Arn' Timeout: 300 MemorySize: 1024 Environment: Variables: FFMPEG_DOWNLOAD_URL: !Ref ffmpegDownloadUrl SUPPORT_FILES_BUCKET: !Ref SupportFilesBucketName FFMPEG_ZIP_NAME: !Ref FFMPEGZipName Code: ZipFile: | import os import boto3 import urllib.request import tarfile from zipfile import ZipFile import cfnresponse url = os.environ['FFMPEG_DOWNLOAD_URL'] bucket = os.environ['SUPPORT_FILES_BUCKET'] zip_file_name = os.environ['FFMPEG_ZIP_NAME'] def handler(event, context): responseData={} status = cfnresponse.SUCCESS if event['RequestType'] != 'Delete': try: os.chdir("/tmp") # download ffmpeg tar_file_name='ffmpeg-release-amd64-static.tar.xz' print(f"downloading ffmpeg from {url} to {tar_file_name}") urllib.request.urlretrieve(url, tar_file_name) # extract desired binary files binaries = ["ffmpeg","ffprobe"] tar = tarfile.open(tar_file_name) bin_dir="bin" for member in tar.getmembers(): basename = os.path.basename(member.name) if basename in binaries: member.name = f"{bin_dir}/{basename}" print(f"extracting {basename} to {member.name}") tar.extract(member) tar.close() # zip the bin directory with ZipFile(zip_file_name, 'w') as zipObj: for folderName, subfolders, filenames in os.walk(bin_dir): for filename in filenames: #create complete filepath of file in directory filePath = os.path.join(folderName, filename) # Add file to zip print(f"zipping {filePath}") zipObj.write(filePath) # Upload zipfile to S3 print(f"uploading {zip_file_name} to s3 bucket {bucket}") s3_client = boto3.client('s3') response = s3_client.upload_file(zip_file_name, bucket, zip_file_name) except Exception as e: print(e) responseData["Error"] = f"Exception thrown: {e}" status = cfnresponse.FAILED cfnresponse.send(event, context, status, responseData) ffmpegZip: Type: Custom::ffmpegZip Properties: ServiceToken: !GetAtt ffmpegZipFunction.Arn Outputs: FFMPEGZipName: Value: !Ref FFMPEGZipName