Skip to content

Instantly share code, notes, and snippets.

@dabit3
Created April 14, 2020 23:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dabit3/9cbc56bc89fea5dfa5928e583a009701 to your computer and use it in GitHub Desktop.
Save dabit3/9cbc56bc89fea5dfa5928e583a009701 to your computer and use it in GitHub Desktop.
Serverless Lambda API with Python
/* 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