In this video, you’ll see how to set up custom metered billing with AWS Data Exchange for APIs.
With this solution, you can build APIs that identify custom metered dimensions
that you can use for billing so you can offer flexible pay-as-you-go licenses to
AWS Data Exchange subscribers.
We’ll begin in the AWS Lambda console,
where we’ll create a function that can return the current time.
We’ll author the function from scratch.
Let’s give it a name.
For the runtime, we’ll select Python 3.9.
We’ll leave the rest of the options as their defaults and create the function.
Next, we’ll paste our function code into the editor.
This code looks at the query string parameters that were passed into the
function and returns the time zone based on whether EST or PST was entered.
Let’s deploy the code.
Now that the Lambda function is ready, we’ll integrate it with API Gateway.
Here, we’ll build a public REST API that can be called by AWS Data Exchange.
We’ll create a new API.
We’ll give it a name and description.
We’ll leave the endpoint type set to Regional, and create the API.
Next, we’ll create a resource for our API.
Let’s name the resource data.
The resource path updated automatically.
We’ll leave the other options as their defaults, and create the resource.
Next, let’s create a GET method on the resource.
We’ll select the check mark to confirm the selection.
Next, we’ll select the option to use Lambda Proxy integration.
Let’s specify the function we created earlier and save the changes.
We’ll give API Gateway permission to invoke our Lambda function.
Next, we’ll select the GET Method Request to set up authorization on the method.
We’ll select AWS Identity and Access Management (AWS IAM).
We’ll add a URL query string parameter to query a specific time zone.
Next, we’ll set up a resource policy that allows AWS Data Exchange to invoke
this API when a subscriber makes a request.
For demonstration purposes, we’ll paste in the policy.
This policy only allows access to the service principal for AWS Data Exchange
and only calls from the specified source account.
Let’s save the policy.
Next, let’s go back to the Resources page to verify the API and Lambda integration.
We’ll choose the GET method and configure a test call.
We’ll specify the time zone as EST and then run the test.
In the response body, we can see API response data giving the time in EST.
Within the response header, we can see our two custom keys, compute and time
zone, which identify the metered costs.
Currently, each key has a value of 1.
This looks correct, so let’s deploy the API.
Let’s open the invoke URL for the deployment.
As expected, the API is missing an authorization token because we set
the authorization type to AWS IAM.
To deploy the API, we'll need to list it as an AWS Data Exchange data set.
Let's do that now.
On the Owned data sets page, we’ll create a new data set.
For the data set type, we’ll select Amazon API Gateway API.
Next, we’ll define a name and description for the data set.
We’ll create the data set and continue.
Next, we’ll add our API assets to the revision.
We’ll select the Amazon API Gateway API we just created.
Next we’ll select the API stage we deployed earlier.
Under Document API for subscribers,
we’ll specify the information that subscribers will see.
Let’s modify the name to make it more descriptive for the subscribers.
Next, let’s import the open API 3.0 specification from Amazon API Gateway.
The specification has been automatically imported.
Let’s add the API stage.
An import job was created for the API asset.
Let’s review the data set.
The API asset has been added.
Let’s finalize the data set.
Next, we'll locate the invoke URL that corresponds to the API data set.
Let’s open the revision we just created.
Next, we’ll open the API to view its endpoints.
The first endpoint is the one we deployed with API Gateway that
can be called by AWS Data Exchange.
The second endpoint was created by AWS Data Exchange and can be called by subscribers.
Let’s invoke this URL to test the end-to-end integration.
For demonstration purposes, we'll do this from AWS CloudShell.
We'll begin typing the command.
We need to pass in our data set ID,
which we can copy from the data set overview in AWS Data Exchange.
Next, we’ll pass in the revision ID and the asset ID.
Finally, we’ll specify the method, path, and query string parameters.
We’ll set the time zone to EST.
Let’s invoke our API asset.
The response includes the expected key values and displays the time in EST.
Now, let's test the response when we set the time zone to PST.
The response includes our expected key values and displays the time in PST.
You’ve just seen how to set up custom metered billing with AWS Data Exchange for APIs.
You can learn more about this topic in the description and links for this video.
Thanks for watching. Now it’s your turn to try.