Automatically Distribute Scheduled Reports for Amazon Connect
Data is critical in contact centers. Supervisors and managers rely on reports to tell them how their teams are performing and to plan for staffing requirements. Providing people with the data they need, when they need it, is vital. This blog post describes how to enable generated reports and send them automatically to users via email using:
This post also shows using AWS CloudFormation to simplify the deployment.
The solution in this post uses the native reporting capabilities in Amazon Connect to configure and schedule the report. That scheduled report is delivered to an S3 bucket that you specify in your Amazon Connect configuration. When the report is delivered to S3, it triggers an event that fires off a Lambda function. The Lambda function reads the event, retrieves the report from S3, and delivers it to the email addresses specified. All activity is logged to CloudWatch for tracking purposes.
Let’s get started.
To complete this setup, you need the following:
- An active AWS account.
- An Amazon Connect instance in either us-east-1(N. Virginia) or us-west-2(Oregon). Only these two are supported for this configuration as Amazon SES is only available in those Amazon Connect regions. After you create your instance, claim a phone number for it. For more information, see Getting Started with Amazon Connect.
- Amazon SES configured on your account. This solution uses Amazon SES to send the reports via email to their designated recipients. To send emails using SES, verify your From address to show that you own it. If you are in the sandbox, you also must verify your To addresses. You can verify email addresses or entire domains. For information about the verification process, see Verifying Identities in Amazon SES. For information about having your account removed from the sandbox, see Moving Out of the Amazon SES Sandbox.
- The appropriate IAM permissions to execute this CloudFormation template. This includes the rights to create IAM roles and Lambda functions. Additionally, you need the rights to execute CloudFormation templates, and to add an event to an S3 bucket. If you are unsure of your permissions, contact your company’s AWS administrator.
Building the Foundation
To simplify the steps involved in this configuration, I’ll use AWS CloudFormation. CloudFormation allows you to quickly deploy resources using templates. The template created for this post creates the IAM role and Lambda function.
Before you run the template, gather some information from your Amazon Connect instance. Specifically, the name of the S3 bucket where your exported reports are stored, and the bath to the reports folder. You can find this information in the Amazon Connect console, under the Data Storage section. The location should look similar to this:
Using this example, the bucket name is everything before the first forward slash, or “mybucketname.” And the reports path is everything after the first forward slash, or “connect/myinstancename/Reports”. To find these values:
- Open the Amazon Connect console.
- Choose the instance alias that you wish to use.
- In the left navigation, choose Data storage.
- In the Data Storage section, you will find the information as shown below.
To use the CloudFormation template:
- Open the AWS Management Console.
- Open the CloudFormation template. This opens the CloudFormation console .
- On the Select Template page, choose Next.
- On the Specify Details page, change the values for all parameters as described in the parameters section.
- Choose Next, and then on the Options page, keep all values and choose Next.
- On the Review page, review the warning about IAM role creation, and select the check box to acknowledge that AWS CloudFormation might create IAM resources.
- Choose Create.
- Wait for the status to update from CREATE_IN_PROGRESS to CREATE_COMPLETE. Once the deployment is complete, proceed with the next section, “Modifying the Deployment”.
Modifying the Deployment
Now that the foundation has been set, you must make a few changes for the configuration to work. Modify the IAM role that was created, add an event trigger to the S3 bucket, and configure Amazon Connect to run scheduled reports.
To modify the IAM role:
- Open the CloudFormation console.
- Choose the Stack named ConnectReportsDistributor.
- On the Stack Detail page, expand the Outputs section.
- Copy the IAM value.
- Open the IAM Console in a new tab in your browser.
- In the left navigation pane, choose Roles.
- In the Search box, paste the IAM value that you copied. The list of roles should filter down to the correct one.
- Click the Role name.
- Expand the ConnectReportsDistributor inline policy.
- Choose Edit policy.
- Choose the JSON tab.
- On line 22, replace replacewithyourbucket with the name of your bucket. It should look similar to the following image:
- Choose Review policy.
- Choose Save changes.
Setting Up the S3 Event
Now that the role has permissions to get files from the correct S3 bucket, you must set up an event on that bucket. This event triggers when new objects are placed in the bucket and executes the Lambda function that delivers the reports.
Next, let’s create that event.
- Open the S3 console.
- In the S3 console, find your bucket, and choose the bucket name.
- On the bucket detail page, choose the Properties tab.
- Scroll down to the Advanced settings section, and choose Events.
- In the Events window, choose Add notification.
- Give the notification a name, such as NewReportEvent.
- In the Events section, select the Put box.
- Leave all other checkboxes unchecked.
- In the Send to section, choose Lambda Function.
- In the Lambda drop down menu, choose the function that starts with ConnectReportsDistributor.
- Choose Save.
Everything is now set up and ready to handle your reports as they are exported. The last thing to do is schedule reports for delivery.
To schedule a report, first create and save the report. If you have never created and saved a report in Amazon Connect, you can find detailed instructions in the Historical Metrics Reports section of the user guide. When scheduling reports, it is important to make sure to set the prefix. This allows the Lambda function to identify where the reports should be delivered.
To schedule a report:
- Log in to your instance using the administrator account, or an account that has permissions to create and schedule reports.
- In the left navigation pane, go to Metrics and quality, Saved reports.
- Choose Historical metrics.
- Hold the cursor over the line that contains the report that you want to schedule. Choose the clock icon that appears to the right of the report name.
- Configure the Recurrence that you want for this report. An example of an hourly report is displayed in the following image.
- Choose the Delivery Options tab.
- In the Prefix box, enter one of the SearchKey values that you provided when you ran the CloudFormation template, such as Sales or Service. When your report is saved to the S3 bucket, the Lambda function is triggered, which checks the name of the incoming file to see if it matches any of your search keys. If it does, the function then assigns the correct email distribution and sends the email. If it does not, the function sends the report to the default email address specified. You should see the file name example expand to include the prefix.
- Choose Create.
Setup is complete! When the scheduled report is generated, the output is saved to the S3 bucket. This triggers the event, calls the Lambda function, and ultimately sends the email to the appropriate recipients with the report attached.
Making Further Changes
This solution provides a foundation to build on. The Lambda function is written to filter for two different distributions with a third for the default delivery. If you want to add more groups or filters, add environment variables for the keys (search criteria) and the recipient group, then add the new recipient and filter to the if statement that determines which address to use. For example, to add a third category of “Contracts” which should send reports to the email distribution of “firstname.lastname@example.org”, perform the following:
- Open the AWS Lambda console.
- Choose the Lambda function that starts with ConnectReportsDistributor-LambdaFunction
- In the function code, find the comment # Grab all of our environment variables and assign them to local vars
- In try section that follows, find group_2 = os.environ[‘key_2’]
- Add the following code on a new line after that: group_3 = os.environ[‘key_3’]
- Next, find recipient_2 = os.environ[‘recipient_2’]
- Add the following code on a new line after that: recipient_3 = os.environ[‘recipient_3’]
- Now find the comment block that begins with # Then check it against our defined options
- Below the comment block, find clean_group_2 = group_2.lower()
- Add the following code on a new line after that: clean_group_3 = group_3.lower()
- Next, find elif clean_group_2 in sanitized_filename:
- After the recipient = recipient_2 line, add the following code on a new line: elif clean_group_3 in sanitized_filename:
- Then add a new indented line with: recipient = recipient_3
- Go to the Environment variables section
- Add a new key named key_3 with a value of Contracts
- Add another new key named recipient_3 with a value of email@example.com
- Save the function
Repeat this process, incrementing the number appropriately, for each new group and distribution that you wish to use. For reference, you can download this modified version of the code.
In this blog post, you configured AWS services to deliver your scheduled reports from Amazon Connect to your users via email. You used CloudFormation to lay the groundwork, Amazon Connect to generate reports, IAM Roles to provide the security needed, S3 events to trigger the execution, and a Lambda function to prepare and send the email and attachment. This delivery mechanism provides your contact center managers with the data they need, when they need it, to provide outstanding customer service.