Created
April 14, 2020 23:08
-
-
Save dabit3/9cbc56bc89fea5dfa5928e583a009701 to your computer and use it in GitHub Desktop.
Serverless Lambda API with Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* This file contains two files */ | |
/* index.py */ | |
import json | |
def handler(event, context): | |
body = { | |
"message": "Hello from Lambda!" | |
} | |
response = { | |
"statusCode": 200, | |
"body": json.dumps(body), | |
"headers": { | |
"Content-Type": "application/json", | |
"Access-Control-Allow-Origin": "*" | |
} | |
} | |
return response | |
/* App.js */ | |
import React, { useState, useEffect } from 'react'; | |
import logo from './logo.svg'; | |
import './App.css'; | |
import { API } from 'aws-amplify' | |
function App() { | |
const [greeting, setGreeting] = useState(null) | |
async function fetchGreeting() { | |
const apiData = await API.get('mypythonapi', '/greeting') | |
setGreeting(apiData.message) | |
} | |
useEffect(() => { | |
fetchGreeting() | |
}, []) | |
return ( | |
<div className="App"> | |
<header className="App-header"> | |
<img src={logo} className="App-logo" alt="logo" /> | |
<h1>{greeting}</h1> | |
</header> | |
</div> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment