Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Norris Chebl
Norris Chebl

Posted on

     

Multiple Frontends with AWS Amplify

Goal

Build a React content management system that persists data (videos, images, etc.) to multiple React Native mobile apps.

Utilizing the followingAWS Services for the backend:

  • Hosting: Amplify Console
  • Database: DynamoDB
  • User Authentication: Cognito
  • Storage: S3

UtilizingReact for the website andReact Native for the mobile apps.

Step 1: Create your GraphQL API

Run the following command:

amplify add api
Enter fullscreen modeExit fullscreen mode

Edit your schema:

typeUser@model(queries:{get:"getUser"},mutations:{create:"registerUser",update:"updateUser"},subscriptions:null){id:ID!username:String!email:String!registered:Booleanavatar:S3Object!createdAt:String}typePost@model@searchable@auth(rules:[{allow:owner,identityClaim:"sub"}]){id:ID!name:String!description:String!tags:[String]thumbnail:S3Object!video:S3Object!owner:StringcreatedAt:StringupdatedAt:String}typeS3Object{bucket:String!region:String!key:String!}
Enter fullscreen modeExit fullscreen mode

Deploy the API:

amplify api push
Enter fullscreen modeExit fullscreen mode

You should now have agraphql folder in your project that lists all of yourqueries,mutations andsubscriptions.

You can view your database in the DynamoDB dashboard.

Step 2: Add Authentication

amplify add auth
Enter fullscreen modeExit fullscreen mode

Create user groups:

❯ Manual configurationDo you want to add User Pool Groups? (Use arrow keys)❯ Yes? Provide a name for your user pool group: Admins? Do you want to add another User Pool Group Yes? Provide a name for your user pool group: Editors? Do you want to add another User Pool Group No? Sort the user pool groups in order of preference …  (Use <shift>+<right/left> to change the order)  Admins  Editors
Enter fullscreen modeExit fullscreen mode

You can view user information in the Cognito dashboard.

Configure your Amplify application:

npm install aws-amplify
Enter fullscreen modeExit fullscreen mode

InApp.js:

importAmplify,{Auth}from'aws-amplify';importawsconfigfrom'./aws-exports';Amplify.configure(awsconfig);
Enter fullscreen modeExit fullscreen mode

For more details on adding theAuthentication UI, read theAmplify docs.

Step 3: Add Storage

amplify add storage
Enter fullscreen modeExit fullscreen mode
? Please select from one of the below mentioned services (Use arrow keys)❯ Content (Images, audio, video, etc.)  NoSQL Database
Enter fullscreen modeExit fullscreen mode
amplify push
Enter fullscreen modeExit fullscreen mode

Note: If you would like public read access for your files, be sure toset the appropriate permissions.

Configure:

importAmplify,{Storage}from'aws-amplify';importawsconfigfrom'./aws-exports';Amplify.configure(awsconfig);
Enter fullscreen modeExit fullscreen mode

You can view your files in the S3 dashboard.

For more details on addingStorage, read theAmplify docs.

Step 4: The fun part

Now let's connect the backend you created to a different frontend.

  1. Go to your Amplify Console.
  2. Click on the app you're working on.
  3. Click "Backend Environments".
  4. Click "Edit Backend".

Copy the line of code that looks like this:

amplify pull --appId ************* --envName *******
Enter fullscreen modeExit fullscreen mode

and run that command in your new frontend environment.

Important: Choose‘No’ when asked if you want to modify or add new categories to your backend. Choosing ‘Yes’ will work, but it is highly recommended that you don't have your backend stored in two separate repositories for this can lead to unintended consequences with multiple sources of truth.

You will now have access to youramplifyconfiguration folder in src. This means you can now use the following in this second frontend:

importawsconfigfrom'./aws-exports';Amplify.configure(awsconfig);
Enter fullscreen modeExit fullscreen mode

You can now also copy over thegraphql folder from your initial frontend over to your second frontend to utilize all of yourqueries,mutations andsubscriptions.

Note: Whenever updating your API in your backend, make sure to copy over the new graphql files into your new frontend.

You did it!

You can now use AWS Amplify to access your backend data with multiple frontends. This can especially be useful when you need to share data between web and mobile.

If you have any questions, please feel free to send me a message or ask in the comments!

Top comments(11)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
agapaudi profile image
Mau Peon
  • Joined

Hi, How can you deploy different frontends with same backend. Amplify only let me use same repository with multiple branches, can I use multiple repositories?

CollapseExpand
 
railwaygundora profile image
RailwayGunDora
I'm just a mechanical engineer
  • Location
    Johannesburg
  • Work
    Front end developer at Wits
  • Joined

That's an issue, thanks for bringing it up. You actually need to be in the same repo and have different branches there.

CollapseExpand
 
adnanraml profile image
Adnan
  • Joined

I think this link answer your question
docs.amplify.aws/cli/teams/multi-f...

CollapseExpand
 
a9udn9u profile image
Quan Gan
  • Joined

That link doesn't answer Mau's question.

By "deploy" if you mean runningamplify publish to deploy to Amplify's static hosting then the answer is no, you can't publish frontends in different repositories. Even if the Amplify cli allows you to do that, your repos will override each other.

Thread Thread
 
adnanraml profile image
Adnan
  • Joined

Hi,
yes I was having the same question you have.
I think the solution is to add multiple branches in the frontend envirmoent and do the sub-domain configration with amplify framework in the console .

CollapseExpand
 
oleksiisytar profile image
Oleksii Sytar
  • Location
    Vinnytsia, Ukraine
  • Joined

According to this note:Note: Whenever updating your API in your backend, make sure to copy over the new graphql files into your new frontend. Do I need to copy files every time I have made changes? Maybe we have some way to automate this?

CollapseExpand
 
bcsgbl profile image
bcs-gbl
  • Joined
• Edited on• Edited
  1. If is started a project with "amplify init" command everyting is OK.
  2. But if started with "amplify pull" (for example on a second frontend APP), I've got this error:

ˇˇˇ
amplify pull --appId d45659npivltuns --envName test
EPERM: operation not permitted, rename 'D:\web-nodejs\test\amplify' -> 'D:\web-nodejs\test\amplify-backup'
Error: EPERM: operation not permitted, rename 'D:\web-nodejs\test\amplify' -> 'D:\web-nodejs\test\amplify-backup'
at Object.renameSync (fs.js:741:3)
ˇˇˇ

Same error if just run an "amplify pull".

Im on Windows10, latest
I tried to:

  • reinstall npm and libs, with clearing all cache, dirs, so on
  • reinstall amplify-cli
  • reinstall amplify-cli with --unsafe-perm=true prameterAnd try this whole workflow:
  • with admin or simple user roles,
  • different consloles,
  • in WSL2
  • different & fully clean directories,

but i got tha same error.

If I copy the first APP's amplify directory to the second, every "amplify pull" command works..
Now, I have no idea..

CollapseExpand
 
djom202 profile image
Jonathan Olier
  • Joined

Why do you need to create a user model? I'm supposing that you can use the userid from Cognito.

CollapseExpand
 
skydowx profile image
skydowx
  • Joined

You can use the userid from cognito. But sometimes you might want to make connections with other entities in the database. In that situation, it is useful to also make a user entity in the database.

type User @model {...posts: [Post]! @connection...}
Enter fullscreen modeExit fullscreen mode
CollapseExpand
 
pedroapfilho profile image
Pedro Filho
Curious by nature, entrepreneur and JavaScript enthusiast.
  • Email
  • Location
    London
  • Work
    Software Engineer at Game Analytics
  • Joined

Isn’t it better to have a project only for the amplify backend?

CollapseExpand
 
arshadcomp profile image
arshadcomp
PHP, MySQL, Node, Vue Developer. Have good knowledge of AWS
  • Location
    India
  • Joined

If I have two Vue frontends for the same backend. Will amplify publish push the frontend code in same repo or different?

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software Engineer
  • Location
    San Diego, CA
  • Work
    Software Engineer
  • Joined

More fromNorris Chebl

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp