DOP334-R - Cloud Native App Dev with AWS Amplify and React
https://git.io/JeD21
The following demonstration includes the use of AWS Cloud9, a cloud-based IDE, to build a fully serverless cloud native application in React using the Amplify toolchain as well as the Amplify Javascript library.
Through this demo, you will see how easy it is to build a fully functional, secure cloud native application in hours rather than in days or weeks.
AWS Amplify is a Javascript library for frontend and mobile developers building cloud-enabled applications. The default implementation works with AWS, but it is designed to be open and pluggable for any custom backend or service.
Cloud9 Setup
When starting with a fresh Cloud9 environment, you will need to install a few tools for use with this demo. A convenience script has been included with this repo to help you get up and running.
git clone https://github.com/dave-malone/react-amplified-demo dop334
curl -s -L https://git.io/JeM6t | bash
Install Amplify and Configure your environment for use with the Amplify CLI
npm install -g @aws-amplify/cli
echo "amplify version: $(amplify --version)"
amplify configure
Create the project React project and begin integrating with AWS via Amplify
yarn create react-app react-amplified
cd react-amplified
amplify init
amplify run
amplify add hosting
amplify status
amplify publish
- At this point, there's nothing really interesting about this app. Let's require auth for accessing our webapp, and add user signup / signin capabilities
Enable User Signup / Signin
amplify add auth
amplify push
If you do not run this command, theaws-exports.js
file needed in the next steps will not get generatedyarn add aws-amplify aws-amplify-react
- in index.js add:
import Amplify from 'aws-amplify'
import config from './aws-exports'
import { withAuthenticator } from 'aws-amplify-react';
Amplify.configure(config);
const AppWithAuth = withAuthenticator(App);
ReactDOM.render(<AppWithAuth />, document.getElementById('root'));
amplify publish
- What if users want to use their existing identity? No problem. In index.js:
const federated = {
google_client_id: '',
facebook_app_id: '',
amazon_client_id: ''
};
ReactDOM.render(<AppWithAuth federated={federated} />, document.getElementById('root'));
- Our users can now sign up and sign in. Sadly, they can't sign out.
- in index.js:
const AppWithAuth = withAuthenticator(App, { includeGreetings: true });
Create an S3 backed photo album
amplify add storage
amplify push
- Replace the contents of App.js with:
import React, { Component } from 'react';
import './App.css';
import { Storage } from 'aws-amplify';
import { S3Album } from 'aws-amplify-react';
Storage.configure({ level: 'private' });
class App extends Component {
constructor(props){
super(props)
this.albumRef = React.createRef()
this.uploadFile = this.uploadFile.bind(this);
}
uploadFile = (evt) => {
const file = evt.target.files[0];
const key_prefix = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)
const key = `${key_prefix}-${file.name}`
Storage.put(key, file).then(() => {
this.albumRef.current.list()
})
}
render() {
return (
<div className="App">
<p> Pick a file</p>
<input type="file" onChange={this.uploadFile} />
<S3Album level="private" path='' ref={this.albumRef}/>
</div>
)
}
}
export default App;
amplify publish
Cleanup
amplify delete
- Delete S3 buckets
- Delete IAM user amplify-*
- Delete Cloud9 Instance