# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"). # You may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: Amazon Transcribe Live Call Analytics with Agent Assist - Amazon Chime SDK Voice Connector with SIPREC Parameters: CidrBlocks: Type: CommaDelimitedList Description: Comma Delimited list of CIDR blocks that will be allow listed to send traffic to the Amazon Chime SDK Voice Connector. CIDR blocks must be between /27 and /32 in length and use public IP ranges. Example 203.0.113.128/27, 198.51.100.76/32 Resources: CreateVCFunctionRole: Type: 'AWS::IAM::Role' Properties: AssumeRolePolicyDocument: Statement: - Action: 'sts:AssumeRole' Effect: Allow Principal: Service: lambda.amazonaws.com Version: 2012-10-17 ManagedPolicyArns: - !Join - '' - - 'arn:' - !Ref 'AWS::Partition' - ':iam::aws:policy/service-role/AWSLambdaBasicExecutionRole' CreateVCFunctionPolicy: Type: 'AWS::IAM::Policy' Properties: PolicyDocument: Statement: - Action: - chime:CreateVoiceConnector - chime:PutVoiceConnectorTermination - chime:PutVoiceConnectorStreamingConfiguration - chime:DeleteVoiceConnector - cloudformation:DescribeStacks - iam:CreateServiceLinkedRole - iam:PutRolePolicy Effect: Allow Resource: - '*' Version: 2012-10-17 PolicyName: CreateVCFunctionPolicy Roles: - !Ref CreateVCFunctionRole CreateVCFunction: Type: 'AWS::Lambda::Function' DependsOn: - CreateVCFunctionPolicy Properties: Role: !GetAtt - CreateVCFunctionRole - Arn Handler: index.on_event Runtime: python3.8 Timeout: 300 Code: ZipFile: | import boto3 import uuid import os import cfnresponse responseData = {} chime = boto3.client('chime-sdk-voice') cloudformation = boto3.resource("cloudformation") def create_vc(event): response = chime.create_voice_connector( Name='Trunk' + str(uuid.uuid1()), AwsRegion=os.environ['AWS_REGION'], RequireEncryption=False ) print(response) voiceConnectorId = response['VoiceConnector']['VoiceConnectorId'] responseData["voiceConnectorId"] = voiceConnectorId streamingConfiguration = { "DataRetentionInHours": 1, "Disabled": False, "StreamingNotificationTargets": [ { "NotificationTarget": 'EventBridge' } ] } chime.put_voice_connector_streaming_configuration( VoiceConnectorId=voiceConnectorId, StreamingConfiguration=streamingConfiguration ) if 'CIDRBlocks' in event['ResourceProperties']: params = { 'CpsLimit': 1, 'CallingRegions': [ 'US', ], 'CidrAllowedList': event['ResourceProperties']['CIDRBlocks'], 'Disabled': False } print(params) chime.put_voice_connector_termination( VoiceConnectorId=voiceConnectorId, Termination=params) chime.put_voice_connector_logging_configuration( VoiceConnectorId=voiceConnectorId, LoggingConfiguration={ 'EnableSIPLogs': True, 'EnableMediaMetricLogs': True } ) return responseData def delete_vc(event): stack_name = event["ResourceProperties"]["stackName"] stack = cloudformation.Stack(stack_name) outputs = {output["OutputKey"]: output["OutputValue"] for output in stack.outputs} if stack.outputs: voice_connector = outputs["VoiceConnector"] chime.delete_voice_connector(VoiceConnectorId=voice_connector) def on_event(event, context): print(event) if event['RequestType'] == 'Create': try: responseData = create_vc(event) cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData) except Exception as e: error = f'Exception thrown: {e}. Please see https://github.com/aws-samples/amazon-transcribe-live-call-analytics/blob/main/TROUBLESHOOTING.md for more information.' print(error) cfnresponse.send(event, context, cfnresponse.FAILED, {}, reason=error ) elif event['RequestType'] == 'Delete': try: delete_vc(event) responseData = {'Message': 'Deleting VC. Returning success status.'} cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData) except Exception as e: error = f'Exception thrown: {e}. Please see https://github.com/aws-samples/amazon-transcribe-live-call-analytics/blob/main/TROUBLESHOOTING.md for more information.' print(error) cfnresponse.send(event, context, cfnresponse.FAILED, {}, reason=error ) else: responseData = {'Message': 'Update is no-op. Returning success status.'} cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData) # Trigger Lambda function to create Amazon Chime App & Asterisk Instance creation SIPRECVoiceConnectorResource: Type: AWS::CloudFormation::CustomResource Properties: ServiceToken: !GetAtt CreateVCFunction.Arn stackName: Ref: AWS::StackName CIDRBlocks: !If [CIDRBlockExists, !Ref CidrBlocks, !Ref 'AWS::NoValue'] UpdateReplacePolicy: Delete DeletionPolicy: Delete Conditions: CIDRBlockExists: !Not - !Equals - !Join ['', !Ref CidrBlocks] - '' Outputs: VoiceConnector: Value: Fn::GetAtt: - SIPRECVoiceConnectorResource - voiceConnectorId