Rate this page:

How to Monitor Super SIM Connection Events using AWS ElasticSearch and Kibana

Twilio Event Streams is a new, unified mechanism to help you track your application’s interactions with Twilio products. Event Streams spans Twilio’s product line to provide a common event logging system that supports product-specific event types in a consistent way. You use a single API to subscribe to the events that matter to you.

Super SIM provides a set of event types, called Connection Events, which are focused on devices’ attempts to attach to cellular networks and, once they are connected, the data sessions they establish to send and receive information.

This tutorial will show you how to integrate Super SIM Connection Events into a typical business process: routing the data to AWS ElasticSearch so it can feed your Kibana dashboards. To do so, you’ll set up Event Streams to send Super SIM Connection Events to an AWS Kinesis Data Stream and into ElasticSearch via AWS Kinesis Firehose.

If you’d prefer to start with a more basic guide to using Super SIM connection events, or your use-case doesn’t require AWS, we have another tutorial that focuses on streaming Super SIM Connection Events to a webhook in your cloud.

With the AWS resources in place to read and feed events to Kibana, you’ll explore probing the event data, setting up visualizations to help you spot underlying trends in the data, and configuring a dashboard so you‘re ready to monitor event data regularly.

This tutorial assumes you’re working on a Linux box or a Mac, but it should also work under Windows if you’re running Windows Subsystem for Linux. Whatever platform you’re using, we’ll take it as a given that you’re familiar with the command line.

You don’t need to have experience of working with AWS accounts and processes, unless you plan to change or upgrade the resources configured during the tutorial. All of the AWS setup required by the tutorial is driven by a script, so you can focus on the flow rather than keying in code or navigating the AWS console. The script is fully commented so you can understand exactly what is to be installed.

If you’ve already set up Event Streams for another Twilio product, then you may prefer to jump straight to the documentation that describes the event types unique to Super SIM. No problem.

Let’s get started.

1. Review the requirements

To work through this tutorial, you’ll need your Twilio Account SID and Auth Token to log in with the Twilio CLI tool. If you don’t have these credentials handy, you can get them from the Console.

You’ll also need an AWS account set up with an AWS user that has resource creation permissions. You’ll make use of these in Steps 2 and 3.

The tutorial creates and uses AWS resources, so please be aware that this will come at a cost to your AWS account holder. We’ve made sure we used as few and as limited resources as possible. For more details, check out AWS’ pricing page.

You may also find that certain configurations are not available in your preferred AWS region, so you may need to modify the config you use accordingly. Make sure you thoroughly review the script in Step 3, which has all the details of the config you’ll use and will be where you’ll make any changes you need.

2. Set up your machine

You may have already completed some of these tasks for other tutorials you have followed, or as part of your own workflow, so just skip those steps. For each step, we’ve linked to the relevant setup guide, so just jump straight there if you need further assistance then hop back here when you’re done.

  1. Install and configure the Twilio CLI using this guide.
  2. Install and configure the AWS CLI. Amazon has full guidance for you.
  3. Install the JSON processor JQ from its website. JQ is used to process data in a script you’ll run shortly to validate the Events Stream Sink you set up.
  4. Install the Terraform CLI. Terraform lets you build out infrastructure using code. Because this tutorial involves the creation of many AWS resources of various types, you’ll use Terraform and a ready-to-go setup script to automate this process. Terraform’s developer, Hashicorp, has a guide to get you started with the installation process — come back here as soon as you’ve installed the CLI tool — there’s no need to go further.

3. Build out your AWS resources

In this step you’ll put in place all of the AWS resources you need and connect them together so that they’re ready to receive your Super SIM Connection Events and make them available to Kibana. You will create:

  • A Kinesis Data Stream as an entry point for Super SIM Connection Events.
  • An ElasticSearch domain to receive, store, and work with the events, which will be presented by Kibana.
  • A Kinesis Firehose to bridge the Data Stream and ElasticSearch.
  • An S3 Bucket as a store for records Firehose could not pass to ElasticSearch.
  • Assorted Roles and Policies to authorize Twilio to write to your stream, and for the AWS components to interact with each other.

The entire set up process is automated using Terraform code-as-infrastructure technology and an easy-to-understand script you’ll download shortly. Feel free to review the script before you run it.

  1. Open a terminal and run the following commands to prepare your working directory:
    mkdir supersim_events
    cd supersim_events
  2. Copy the first of the three scripts below and save it to your supersim_events directory as and then run chmod +x setup_aws to make the script executable. You’ll need to update the first line of the script with your preferred AWS region.
  3. Get the second script, save it as, and run chmod +x
  4. Copy the third script and save it to your supersim_events directory as This does not need to be made executable. It is read by the Terraform CLI and used to build a setup scheme. You should review this script to make sure you’re happy with its role and policy settings.
  5. Edit and set your AWS region where marked, eg. us-east-2.
  6. You’re now ready to put your AWS infrastructure into place. Remember, there is a cost implication for your account holder, which we’ve attempted to minimize by instantiating as few and as limited a set of AWS resources as possible. For more details, check out AWS’ pricing page.
  7. Run:
    This script sets the variables Terraform will use and calls two Terraform CLI commands, one to check the script, and the other to build the infrastructure from the script, which it does via the AWS CLI. It will output what it is going to generate, and then ask you how you’d like to proceed. Review its plan, then type in yes and hit Enter.
  8. As the AWS resources are created, you’ll receive progress reports in the terminal, and then it will output some key data: the External ID value, the stream ARN, and the role ARN that you’ll use to set up your Twilio Events Stream Sink, and the URL you’ll use to access Kibana. You’ll need to keep all of these values handy for future steps.

If you subsequently make changes to your Terraform script, just run terraform apply to see the changes Terraform will make and, if you agree, apply them. You don’t need to run again, but if you wish to, just make sure you replace the $(...) section in line 3 with the external ID output on the first run. If you change the external ID, either via the script or manually, you will need to recreate your Sink.

Here are the scripts, or just jump to the next step.

You can also find all of these files in our public GitHub repo.

Script #1 —

export TF_VAR_your_aws_region='<YOUR_AWS_REGION>'
export TF_VAR_external_id=$(openssl rand -hex 40)
export TF_VAR_your_computer_external_ip=$(curl -s
terraform init
terraform validate
terraform apply

Script #2 —

Download this script from the Event Streams documentation.

Script #3 —

 * Define Terraform variables
 * These are set in the '' script
variable "your_aws_region" {
  type = string

// This is required to give your computer access to Kibana
variable "your_computer_external_ip" {
  type    = string

// Randomly generated string to verify connections from Twilio
variable "external_id" {
  type = string

 * Base Terraform setup
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.42.0"

  required_version = ">= 0.15.0"

provider "aws" {
  profile = "default"
  region  = var.your_aws_region

 * Set up policies

// Create a Policy to permit Twilio to write records to our Kinesis Stream
resource "aws_iam_policy" "supersim_kinesis_stream_record_write_policy" {
  name           = "supersim-kinesis-stream-record-write-policy"
  policy         = jsonencode({
    Version      = "2012-10-17"
    Statement    = [
        Effect   = "Allow"
        Resource = "*"
        Action   = [
        Effect   = "Allow"
        Resource = "*"
        Action   = [

// Create a policy to provide read access to ElasticSearch Kibana
// NOTE We limit access to your computer's external (eg. router) IP address,
//      which is required for web access to Kibana
resource "aws_elasticsearch_domain_policy" "supersim_elasticsearch_kibana_access_policy" {
  domain_name     = aws_elasticsearch_domain.supersim_elastic_search_kibana_domain.domain_name
  access_policies = jsonencode({
    Version       = "2012-10-17"
    Statement     = [
        Action    = [
        Effect    = "Allow"
        Resource  =  "${aws_elasticsearch_domain.supersim_elastic_search_kibana_domain.arn}/*"
        Principal = {
          "AWS": "*"
        Condition = {
          "IpAddress": {
            "aws:SourceIp": [

// Set up a policy to manage Firehose's access to various resources:
//   * To write records to ElasticSearch
//   * To read from ElasticSearch (may not be necessary)
//   * To write to S3 records it could not write to ElasticSearch
//   * To read records from the Kinesis Data Stream
//   * To access EC2 resources for data transfer (may not be necessary)
resource "aws_iam_policy" "supersim_firehose_rw_access_policy" {
  name           = "supersim-firehose-rw-access-policy"
  policy         = jsonencode({
    Version      = "2012-10-17"
    Statement    = [
        Effect   = "Allow"
        Action   = [
        Resource = [
        Effect   = "Allow"
        Action   = [
        Resource = [
        Effect   = "Allow"
        Action   = [
        Resource = [
        Effect   = "Allow"
        Action   = [
        Resource = aws_kinesis_stream.supersim_connection_events_stream.arn
        Effect   = "Allow"
        Action   = [
        Resource = "*"

 * Set up roles

// Create a Role Twilio will assume to access the Stream
resource "aws_iam_role" "supersim_twilio_access_role" {
  name                 = "supersim-twilio-access-role"
  assume_role_policy   = jsonencode({
    Version            = "2012-10-17"
    Statement          = [
        Action         = "sts:AssumeRole"
        Effect         = "Allow"
        Principal      = {
          "AWS" = "arn:aws:iam::177261743968:root"
        Condition      = {
          StringEquals = {
            "sts:ExternalId" = var.external_id

// Create a Role Firehose will assume to access ElasticSearch
resource "aws_iam_role" "supersim_firehose_access_role" {
  name               = "supersim-firehose-access-role"
  assume_role_policy = jsonencode({
    Version          = "2012-10-17"
    Statement        = [
        Effect       = "Allow"
        Action       = "sts:AssumeRole"
        Principal    = {
          "Service" = ""

 * Attach policies to roles

// Attach the Stream write Policy to the Twilio access Role
resource "aws_iam_role_policy_attachment" "supersim_attach_write_policy_to_twilio_access_role" {
  role       =
  policy_arn = aws_iam_policy.supersim_kinesis_stream_record_write_policy.arn

// Attach the resource read/write/access Policy to the Firehose access Role
resource "aws_iam_role_policy_attachment" "supersim_attach_rw_policy_to_firehose_access_role" {
  role       =
  policy_arn = aws_iam_policy.supersim_firehose_rw_access_policy.arn

 * Set up AWS resources

// Set up a Kinesis Stream to receive streamed events
// NOTE One shard is sufficient to the tutorial and testing
resource "aws_kinesis_stream" "supersim_connection_events_stream" {
  name        = "supersim-connection-events-stream"
  shard_count = 1

// Create our Elastic Search Domain
// This uses minimal server resources for the tutorial, but
// a real-world application would require greater resources
resource "aws_elasticsearch_domain" "supersim_elastic_search_kibana_domain" {
  domain_name           = "supersim-es-kibana-domain"
  elasticsearch_version = "7.10"

  cluster_config {
    instance_type  = "t2.small.elasticsearch"
    instance_count = 1

  ebs_options {
    ebs_enabled = true
    volume_type = "standard"
    volume_size = 25

  domain_endpoint_options {
      enforce_https = true
      tls_security_policy = "Policy-Min-TLS-1-2-2019-07"

// Create an S3 Bucket
// This is used by Firehose to dump records it could not pass
// to ElasticSearch. In a real-world app, you might also choose
// to store all received records
data "aws_canonical_user_id" "current_user" {}

resource "aws_s3_bucket" "supersim_failed_report_bucket" {
    bucket        = "supersim-failed-report-bucket"
    grant {
      id          =
      type        = "CanonicalUser"
      permissions = ["FULL_CONTROL"]

// Create a Kinesis Firehose to link the Kinesis Data Stream (input)
// to ElasticSearch (output)
resource "aws_kinesis_firehose_delivery_stream" "supersim_firehose_pipe" {
  name        = "supersim-firehose-pipe"
  destination = "elasticsearch"

  kinesis_source_configuration {
    kinesis_stream_arn = aws_kinesis_stream.supersim_connection_events_stream.arn
    role_arn           = aws_iam_role.supersim_firehose_access_role.arn

  elasticsearch_configuration {
    domain_arn     = aws_elasticsearch_domain.supersim_elastic_search_kibana_domain.arn
    role_arn       = aws_iam_role.supersim_firehose_access_role.arn
    index_name     = "super-sim"

    processing_configuration {
      enabled = "false"

  s3_configuration {
    role_arn        = aws_iam_role.supersim_firehose_access_role.arn
    bucket_arn      = aws_s3_bucket.supersim_failed_report_bucket.arn
    buffer_interval = 60
    buffer_size     = 1

 * Outputs -- useful values printed at the end
output "EXTERNAL_ID" {
  value       = var.external_id
  description = "The External ID you will use to create your Twilio Event Streams Sink"

output "KIBANA_WEB_URL" {
  value       = aws_elasticsearch_domain.supersim_elastic_search_kibana_domain.kibana_endpoint
  description = "The URL you will use to access Kibana"

  value       = var.your_computer_external_ip

  value       = aws_kinesis_stream.supersim_connection_events_stream.arn

  value       = aws_iam_role.supersim_twilio_access_role.arn

4. Configure Twilio Event Streams 1: create a Sink

A Sink is an Event Stream destination. To set up a Sink, you create a Sink resource using the Event Streams API. Event Streams currently support two Sink types: AWS Kinesis and webhooks. You’ll use the former, which you created and set up in the previous step.

In your terminal, run the following command. The details you need to add to the command — <YOUR_KINESIS_STREAM_ARN>, <YOUR_KINESIS_ROLE_ARN>, and <EXTERNAL_ID> — were output by Terraform at the end of the previous step.

twilio api:events:v1:sinks:create --description SuperSimSink \
  --sink-configuration '{"arn":"<YOUR_KINESIS_STREAM_ARN>", "role_arn":"<YOUR_KINESIS_ROLE_ARN>", "external_id":"<EXTERNAL_ID>"}' \
  --sink-type kinesis

The command creates a Sink and outputs the new Sink’s SID, which you’ll need to paste into the code in the next step. If you get an error, check that you entered the command correctly.

5. Configure Twilio Event Streams 2: validate the Sink

Kinesis Sinks need to be validated before events can be delivered to them, in contrast to webhook Sinks. To validate a Sink, you need to tell the Sink that you want to test it. This automatically sends a test message to the Sink, which you’ll then retrieve from the Sink itself and use to confirm that the Sink is operational. This validates the Sink.

  1. In one terminal tab or window, run the script:
    ./ supersim-connection-events-stream
    It will wait for records to enter the stream and display them when they arrive.
  2. Open a second terminal tab or window and run the following command to get Twilio to send a test message. Replace <SINK_SID> with the SID value you got in Step 4.
    twilio api:events:v1:sinks:test:create --sid <SINK_SID>
  3. Switch back to the first terminal and you’ll shortly see a block of JSON code. Look for the data key and note the value of its nested test_id key. You’re done with the validation script, so hit ctrl-c to quit from it.
  4. Execute the following command, using your Sink SID and the test ID returned just now:
    twilio api:events:v1:sinks:validate:create --sid <SINK_SID> \
     --test-id <TEST_ID>
    This should display the result valid in the terminal — your Sink has been validated and is ready to receive Super SIM Connection Events.

6. Configure Twilio Event Streams 3: subscribe to events

Event Streams uses a publish-subscribe (aka ‘pub-sub’) model: you subscribe to the events that interest you, and Twilio will publish those events to your Sink.

There are six Super SIM Connection event types, each identified by a reverse domain format string:

Event Type ID String
Attachment Accepted com.twilio.iot.supersim.connection.attachment.accepted
Attachment Rejected com.twilio.iot.supersim.connection.attachment.rejected
Attachment Failed com.twilio.iot.supersim.connection.attachment.failed
Data Session Started
Data Session Updated
Data Session Ended

The types are largely self explanatory. The exception is Attachment Failed, which is a generic ‘could not connect’ error that you may encounter when your device tries to join a cellular network.

In a typical connection sequence, you would expect to receive: one Attachment Accepted, one Data Session Started, and then multiple Data Session Updated events. When your device disconnects, you’ll receive a Data Session Ended event at that point.

Now let’s set up some subscriptions.

To get events posted to your new Sink, you need to create a Subscription resource. This essentially tells Twilio what events you’re interested in. Once again, copy and paste the following command, adding in your Sink SID from Step 4:

twilio api:events:v1:subscriptions:create \
  --description "Super SIM events subscription" \
  --sink-sid <SINK_SID> \
  --types '{"type":"com.twilio.iot.supersim.connection.attachment.accepted","schema_version":1}' \
  --types '{"type":"com.twilio.iot.supersim.connection.attachment.rejected","schema_version":1}' \
  --types '{"type":"com.twilio.iot.supersim.connection.attachment.failed","schema_version":1}' \
  --types '{"type":"","schema_version":1}' \
  --types '{"type":"","schema_version":1}' \
  --types '{"type":"","schema_version":1}'

You specify your chosen event types as --types arguments: each one is a JSON structure that combines the type as an event ID string and the events schema you want to use.

The code will output the new Subscription resource’s SID and confirm the subscription has been applied. The events you’ve subscribed to will now start flowing into ElasticSearch as your Super SIMs connect to cellular networks and, bring up and tear down data sessions.

We’ve included the Data Session Updated event because it will allow you to monitor your Super SIMs’ data usage in Kibana, but please be aware that this event is emitted every six minutes for every Super SIM in your account so you can quickly use up your free event allowance. If this might be an issue for you, consider omitting the last line of the command.

To halt the flow of events, you can also remove the Subscription from your Sink. To do so, run the following command; you’ll need the Subscription SID output when you set up the Subscription:

twilio api:events:v1:subscriptions:remove \

7. Monitor events in Kibana 1 — explore the data

The URL you use to access Kibana was output at the end of Step 3. If it's not still visible in your terminal, navigate to the working directory, run terraform show, and scan the results for the kibana_endpoint key — its value will give you the URL you need.

If you know your way around Kibana, you can jump out of the tutorial at this point if you wish. The remainder focuses on using Kibana to explore and chart the incoming Connection Events data. You should, however, check out the Super SIM Connection Events documentation, which describes the information contained within each event.

  1. Staying with us? Good. Paste the Kibana URL into a browser and hit Enter.
  2. Click on the hamburger menu at the top left and select Stack Management under Management.
  3. Click on Create index pattern:
  4. Under Index pattern name, enter super-sim* and then click Next Step.
  5. Under Time field, select data.timestamp, and then click Create Index Pattern. What you’ve done is establish the data fields Kibana can use and the particular field that it uses to track the flow and order of incoming events. Let’s see what we can do with these events.
  6. Click on the hamburger menu at the top left and select Discover under Kibana. This will show you the most recent records received. How recent is set by the date value at the top right of the screen; you can click this to change the time period in focus:
  7. You can click Refresh to update the list of events, but it’s better to click the calendar icon next to the date range, add a period under Refresh every, and then hit Start. This will refresh the data automatically:
  8. At the left, under Available fields, is a selection of all the data types available in each Super SIM connection event. Scroll down a little and locate data.sim_iccid. Move your mouse over it to reveal a + symbol, which you should click.
  9. The events are now segmented by each of your Super SIMs’ ICCIDs. Look back at the left-hand list of data fields and add to the table (click on the + that appears alongside it). You can now see the networks your SIMs have connected through:
  10. Go to Available fields and add data.rat_type to the table. Now you can see what cellular technologies your devices used to connect:
  11. Pick a Super SIM and move your mouse to one of its rows. You’ll see + and - icons appear at the right of the data.sim_iccid column. Click the + and Kibana will show only events experienced by that SIM:
  12. Filters like the one you just set up are listed above the chart; click on any filter’s X symbol to clear it. You can do the same with column headings to remove them from the table. The << and >> symbols you’ll see when you move your mouse over a column heading allow you to reorder the columns. If you like, click Save — it’s in the menu at the top right — and save your search parameters for use again.

8. Monitor events in Kibana 2 — looking for patterns

  1. Click on the hamburger menu at the top left and select Visualize under Kibana.
  2. Click Create visualization.
  3. In the New Visualization dialog, locate Vertical Bar (it’s toward the bottom of the list) and select it.
  4. Under New Line / Choose a source, click super-sim*.
  5. You’re going to visualize how much data each of your Super SIMs have downloaded. This will be the Y axis value; you’ll list the SIMs on the X axis. First, click on Y-axis Count under Metrics. Then, under Aggregation, click Count, scroll down through the list of options and click Sum.
  6. Click on Select a Field, start typing down, and select data.data_download when it’s suggested:
  7. Click Add under Buckets. Click on X-axis in the pop-up that appears, and then, under Aggregation, click on Terms.
  8. Click on the Field text field and start typing icc — Kibana will suggest data.sim_iccid.keyword, so select it:
  9. Click Update at the bottom right of the screen. You’ll now see a chart showing how much data in bytes each Super SIM has downloaded:
  10. Depending on the number of Super SIMs you have, you might need to adjust the Size setting under Buckets > X-axis, and the date range you’re using (this works just the way you saw in Step 7).
  11. Let’s include upload data volumes too. Click Add under Metrics and select Y-axis. Under Aggregation, click Sum.
  12. Under Field, click Select a field, start typing upload and select data.data_upload when it’s suggested:
  13. Click Update. You’ll see something like this:
  14. Again, you might need to adjust the date range to see meaningful data.

9. Monitor events in Kibana 3 — build a dashboard

Setting up visualizations for occasional use is all very well, but what you really want to do is add them to a dashboard that you can check regularly. Let’s do that now with the visualization you just made.

  1. Click on Save at the top right. Give the visualization the name Super SIM Data Usage and click the Save button.
  2. Click on the hamburger menu at the top left and select Dashboard under Kibana.
  3. Click Create new dashboard.
  4. Kibana invites you to create a new dashboard widget — or “object” — but let’s use the visualization you just created. Click Add in the menu at the top right, and under Add panels select Super SIM Data Usage — the visualization you just made.
  5. Click the X in the top right of the panel to close it:
    1.Click Save in the top right menu to save the dashboard so it’s accessible next time you visit.

Next steps

Well done — you’ve come a long way. You’ve used Terraform to put in place your AWS infrastructure and then set up Twilio Event Streams to first create a connection to your infrastructure, called a Sink. You’ve subscribed to a series of Super SIM Connection Events which have then begun to flow from Twilio to your AWS setup as your IoT devices’ Super SIMs connect to networks and transfer data.

You’ve used Kibana to explore the events data that has been received, from all your SIMs right down to single SIM, and you’ve learned how to create graphs to help you spot trends in your devices’ data usage. You’ve added that graph to a dashboard you can use regularly to monitor the behavior of the devices in your fleet.

We’ve only just scratched the surface of what Kibana can do, but having real, meaningful data to work with — all those Super SIM Connection Events — will make it much easier to explore the rest to see how it can help your business.

Try adding some more visualizations to your dashboard that will help you understand which radio technologies your devices are using to connect.

Imagine you’re fielding a support query from a customer. Drill down to examine how a specific Super SIM or small group of SIMs behaved around a certain point in time.

Want to know more? Kibana’s developer, Elastic, has a bunch of tutorials which will walk you through common scenarios and help you grow your knowledge.

We can’t wait to see what business intelligence tools you build with Super SIM Connection Events!

Rate this page:

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.

Thank you for your feedback!

Please select the reason(s) for your feedback. The additional information you provide helps us improve our documentation:

Sending your feedback...
🎉 Thank you for your feedback!
Something went wrong. Please try again.

Thanks for your feedback!