October 2, 2018

"Hey Siri, what's my AWS spend this month?"

By William Welbes

Use Siri Shortcuts and AWS Lambda to get your monthly spend

One of the most underappreciated features of iOS 12 is Siri Shortcuts. Shortcuts gives users the ability to construct actions that interact with different parts of iOS to perform functions for a user. Retrieve data, calculate some output, combine simple actions from multiple apps... the possibilities abound. It can be a powerful way to add some very personalized actions on your device to get at data quicker and easier.

Shortcuts app icon

If you're familiar with the popular Workflow app on iOS you'll feel right at home. Apple purchased Workflow last year and has grown it into Siri Shortcuts. If you previously had the Workflow app installed, you'll notice it's now called Shortcuts and is indeed the Apple app. If you had any workflows setup, they should be loaded in the new app as well.

As an AWS solution manager, it would be great to be able to know the current AWS spend for the month without having to log in to the console or wait for a scheduled report. Has your serverless app gone viral and Lambda functions are firing off left and right? Has someone in IT fired up a bunch of EC2 instances and the meter's ticking away? Let's create a shortcut so that we can just ask Siri what our AWS spend is this month.

AWS Lambda and API Gateway

First, we need to get access to the AWS monthly spend in a simple way that can be accessed via an HTTP request from our shortcut. AWS has a Cost Explorer API that allows you to easily access cost summary data for your account.

Let's create an AWS Lambda function that can retrieve the desired value from the Cost Explorer API and return it via API Gateway with a simple GET method. We will limit the Lambda function to have role level access to read the cost explorer data and return only the current month value via an HTTP method. We'll use a simple token to access the HTTP method via a header value. Siri Shortcuts will pass a header value on a GET request and pull the spend amount out of the response JSON and provide it as output.

Log in to AWS and create a new Lambda function in the AWS console. Use the Author from scratch template and give your function a name like costSummary. We'll use node.js for the runtime. Choose to create a new custom role and a popup will appear that is pre-populated with Lambda execution permissions. Give your new role a name like lambdaCostSummary since we will also grant it read permission on the cost summary API shortly. Once you've created your role, go back to creating the Lambda function and select the newly created role and click Create function.

Simply paste the following javascript into the Lambda function editor:

This function sets up the parameters for the cost explorer API to get cost between the first day of the current month and the first day of the next month since the start date is inclusive and the end date is exclusive in the API. Using the CostExplorer API via the AWS Javascript SDK, the script retrieves the JSON data and then parses out the value for the current month into a simple JSON response that is sent via API gateway to the caller.

Before this function will work, we need to setup the role to allow executing the Cost Explorer API. It's worth noting that the call to the API to get cost data costs a penny each time it's called. So don't go crazy calling it, but it's worth an occasional cent for the convenience.

Navigate to Identity and Access Management (IAM) roles in the AWS console. Click on the lambdaCostExplorer role that you created earlier. Under Permissions, attach a new policy to allow the Lambda function to access the cost explorer API. Use the following policy JSON to grant read access to the Cost Explorer - GetCostAndUsage method.

    "Version": "2012-10-17",
    "Statement": [
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "ce:GetCostAndUsage",
            "Resource": "*"

Now that the role for the Lambda function has access to the cost explorer API, we should be able to execute our Lambda function via the built in Test function in the console. However, in order to access this information from an HTTP get call, we'll need to configure API Gateway for this Lambda function.

Open the costSummary function in the Lambda console. Add API Gateway as a trigger. Choose an open API endpoint that uses a token. This will keep things simple for the shortcut on iOS, just passing in a key to the endpoint. Choose to use an API key so that the endpoint is not simply wide open to anyone to run. When you have the API Gateway endpoint set up, make note of the API endpoint URL and API key. You'll need those items to set up the Siri Shortcut

At this point, you should be able to use a tool like Postman to execute an HTTP GET request on the endpoint to view the resulting JSON. Set the x-api-key header value to the API key you created for your endpoint.

Create a Siri Shortcut

Now that we have a simple functioning AWS endpoint that will give us the current monthly cost, let's add a shortcut. Download the Shortcuts app from the App Store.

The Shortcuts app uses a visual scripting type interface to build the shortcut functionality. Apple has included a lot of different options that let you tie into existing apps and iOS functionality as well as some scripting type functionality that allows you to do things like variables, loops, and processing of inputs and outputs. Create a new shortcut in the app and you'll see a blank canvas that you can use to compose the shortcut actions. For our shortcut, we just want to fetch the contents of the API Gateway URL and pull the amount value out of the JSON and show it to the user via an alert or Siri response.

There's a search bar that allows you to search for elements to included in the shortcut script. If you search for URL, you'll find an entry for a URL to be used as input to the next part of the script. This is followed by an action to Get Contents of URL where you specify the URL and tap on advanced to add in the x-api-key header with the key that you generated for the endpoint.

Siri Shortcut Screenshot 1 Siri Shortcut Screenshot 2

After retrieving the contents of the URL, we use Get Dictionary from Input to convert the simple JSON to a dictionary that can be used to pull out the amount value. Next, the amount value is pulled out using Get Dictionary Value by the amount key. Finally, the value is displayed in an alert or returned to Siri via the Show Result block.

If you've configured things correctly, you should now be able to run the shortcut via the play button at the top of the edit UI or by just tapping it on the main list. You can also access shortcuts in the home screen widget. In order to have Siri answer for this shortcut, tap on the edit options button to the right of the play button at the top while editing a shortcut. There you'll see an option to add the shortcut to Siri. You'll be prompted to give Siri a voice command for it.

Now that you've configured Siri, you should be able to simply ask "Hey Siri, what is my AWS spend this month?" and Siri will run your shortcut and show your spend. If you're driving and connected via CarPlay, just ask Siri and you'll get an update on the go! Pull over before attempting to turn off any EC2 instances :)

Shortcuts are also shareable, so I've included a shortcut file here so you can download and edit the shortcut on your iOS 12 device inside the Shortcuts app. The API endpoint and header values should be replaced with your actual values.

Shortcuts are a powerful way to create personalized interactions on your device without even having to create an app. There are lots of other ways that Lambda could be leveraged to provide some interactive functionality kicked off by a simple shortcut on your device. What kind of shortcut will you create?

William Welbes - Developer

About William Welbes

Will is a Solutions Architect and consultant at Centare with a passion for building great software. With over 15 years of experience in software development, he has helped clients develop solutions across many different industries.