If you want to build a native mobile app that serves free and paid users, stick around.
I'll show you the services you need in this episode.
How to Build This with Jillian Forde.
Today's episode comes from Shashwat.
He wants to build an app that requires people to play a game
in order for them to snooze their alarm.
I don't know about you, but I'm definitely not ready to play any games before I've had my morning coffee.
Let's hear from Shashwat.
Hi I am Shashwat and I would like to build an alarm app but with a twist.
So basically, every time a person sets an alarm. and it rings,
they would have to play a game for at least a minute or until they have to change the game
by achieving certain desired points.
This would help anyone who has a problem in maybe getting up in the morning
or has a habit of turning off their alarm as soon as it rings.
As completing a task before they can turn off the alarm would help them to awaken up
and would remove the urge to go back to sleep.
In this episode I'll also show you the service that you need to build a mobile app
that has free content and paid members-only content.
You will need to build an iOS app and an Android app.
These apps will be distributed on the Google Play and the Apple Store
which is also where you set up store billing and subscription features.
You'll provision the back end of your app using AWS Amplify.
Amplify is a set of tools and services that helps you build applications on AWS quickly.
You'll use separate Git repositories for iOS and Android.
The Android application would be the primary application
where you'll define the backend components.
The iOS application will then be able to pull the Amplify metadata
to use the same backend.
The same logic applies if iOS is the primary application.
The Android version would pull the Amplify metadata to use the same backend.
You can configure infrastructure in the cloud using the Amplify CLI or the console.
In the console you run the #amplify init command to initialize Amplify.
To add authentication, run #amplify add auth.
This will create an Amazon Cognito User Pool.
Cognito lets you add user signup, sign in and access control.
Cognito has a concept called User pools, which you can think of as a user directory.
The Cognito Identity Pool provides credentials for the client application
so your users can connect to AWS Services like AppSync or S3.
You exchange your identity tokens for temporary AWS credentials.
If you're happy with your setup, you can push it to AWS
to provision it with the CLI command: amplify push.
You'll use that command every time you want to update your cloud resources.
Your application will need a backend API to talk to a database to store data.
You can use amplify to provision an AWS AppSync API
that is backed by the NoSQL database Amazon DynamoDB.
AppSync is a fully managed service that makes it easy to develop GraphQL APIs.
DynamoDB is a fast and flexible NoSQL database for any scale.
Run amplify add api in the CLI to create your backend API and database.
Make sure you select the GraphQL type of API and Amazon Cognito User Pool for authentication.
You will be asked to set up your data model, but don't worry,
you can always change your configuration later.
Based on the data schema you created, Amplify can generate client-side libraries,
so it's easy to use your API.
Run amplify codegen models to generate your models.
The generated files are automatically imported in your project.
In order for your iOS app to use the same backend as the Android app,
you can pull the Amplify metadata using the command amplify pull in the CLI.
Now it's time to think about how your backend can distinguish between paying users and nonpaying users.
Only authenticated users that have paid through the Google or Apple Store
should be able to access premium content or premium features.
AWS Cognito Identity Pool allows both unauthenticated and authenticated users
to gain credentials to access AWS services.
You can offer some functionality to unauthenticated users early in their experience,
without creating friction, and then restrict access to premium features for authenticated users only.
Post authentication, Cognito allows you to trigger an AWS Lambda function
which can be used to save the user in DynamoDB and maintain a user profile.
Lambda allows you to run code without thinking about servers or clusters.
Make sure you use the username as a unique key in your DynamoDB table
to uniquely identify users.
These user profiles will track which in app features have been bought by a user.
For simple in-app purchases, your application will need to validate transactions.
It is strongly recommended to validate transactions on your backend
and not from your mobile app directly as this cannot be trusted.
After a user makes an in-app purchase, Apple will provide a receipt ID
and Google a purchase token.
You'll need to validate the receipt ID and purchase token in Lambda
before turning the purchase feature on.
In Shashwat's case, this purchase will unlock premium games.
Create an API gateway endpoint and a Lambda function to validate your receipt ID and purchase token.
API Gateway enables you to create, maintain and secure APIs at any scale.
The data to know which authenticated user has made a purchase
Can be found in the API call which has been authenticated by Cognito.
Once the token is validated, your Lambda function can update user records in DynamoDB
to indicate that this user is a paying customer
For in app subscriptions, asynchronous events from the Apple and Google Store
need to be captured to update your application status.
You can do this by creating a new API gateway endpoint to receive webhook events
and forward them to a Lambda function.
Apple and Google will call this webhook for events like failed renewals and subscription cancellations.
API Gateway will pass the input payload coming from the Apple and Google Store to the Lambda function.
You can use the receipt ID from Apple or purchase token from Google
to find the user in your database to update its status.
Now that I've had my morning coffee, I'm ready for your game, Shashwat.
Let's recap.
Use Amplify to provision your resources in the cloud.
Add authentication using the authentication module in Amplify
which will use Amazon Cognito behind the scenes.
Get and store user information using AWS AppSync with Amazon DynamoDB.
Control access to paid content or features with Amazon API Gateway with AWS Lambda.
Those are the features that you need to build an MVP of a mobile app
that has free and paid content.
Make sure you check out the resources below this video before you start building.
And remember, it doesn't matter when you start, as long as you just get started.
Thanks for hanging out with me, see you soon.
Thank you for watching.
Check out more about Solutions Architecture on AWS aws.amazon.com/architecture