AWS Lambdas: Netlify With Create React App
Project Init
npm install netlify-cli -g
npx create-react-app netlify-with-create-react-app && cd $_
1. Log Hello World
netlify init
netlify functions:create hello-world
netlify dev
⚠️ Something is already running on port xxxx.
|
killall node
|
2. Deploy Hello World
netlify deploy
⚠️ Error: No such directory › Did you forget to run a build?
|
Set your Directory to Deploy as blank or "." during init, or through the web dashboard in Site Settings: Build & Deploy.
|
3. Pass URL Params
exports.handler = async (event, context) => {
const name = event.queryStringParameters.name || "World";
return {
statusCode: 200,
body: `Hello ${name}`,
};
};
4. Install NPM Packages
npm i @astrajs/collections
5. Set Env Values
const handler = async (event) => {
try {
const region = process.env.ASTRA_DB_REGION
const subject = event.queryStringParameters.name || 'World'
return {
statusCode: 200,
body: JSON.stringify({ message: `Hello, ${subject}. Region: ${region}` }),
}
} catch (error) {
return { statusCode: 500, body: error.toString() }
}
}
netlify dev
6. Test Token
const { createClient } = require("@astrajs/collections");
const handler = async (event) => {
try {
const region = process.env.ASTRA_DB_REGION
// create an Astra client
const astraClient = await createClient({
astraDatabaseId: process.env.ASTRA_DB_ID,
astraDatabaseRegion: process.env.ASTRA_DB_REGION,
applicationToken: process.env.ASTRA_DB_APPLICATION_TOKEN,
});
const subject = event.queryStringParameters.name || 'World'
return {
statusCode: 200,
body: JSON.stringify({
message: `Hello, ${subject}.
Region: ${region}.
Astra Token: ${astraClient.restClient.applicationToken}.` }),
}
} catch (error) {
return { statusCode: 500, body: error.toString() }
}
}
module.exports = { handler }
7. Deploy Authenticated
netlify deploy