feat: AWS Lambda deployment script and fixes (#266)

* feat: add scripts to create lambda layers

* chore: deploy the new version of the contract to mumbai.

* feat: update environment variables used in the serverless config.

* feat: add JSON_RPC env var.

* feat: add deploy:dev command.

* feat: functional deployment script, configuration changes.

* feat: update config and the deploy script.

* docs: add the serverless stack diagram

* feat: move the ABI to the lib dir, update the build handler + some minor changes on the config files.

---------

Co-authored-by: root <root@LAPTOP-UJU1VIVI>
This commit is contained in:
Shredder 2023-06-09 22:13:03 +03:30 committed by GitHub
parent bce1e0a34a
commit 3f78a1af43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 2607 additions and 39 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 MiB

View File

@ -8,7 +8,7 @@
"invoke:build": "yarn build && serverless invoke local --function submitBuildInfo",
"prisma:generate": "npx prisma generate",
"prisma:pull": "npx prisma db pull --force",
"start": "serverless offline",
"start": "yarn build && serverless offline",
"generate:layers": "./scripts/prepare-prisma-client-lambda-layer.sh && ./scripts/prepare-libs-lambda-layer.sh && ./scripts/prepare-node-modules-lambda-layer.sh",
"deploy:dev": "yarn build && yarn generate:layers && yarn sls deploy --stage dev"
},
@ -22,6 +22,7 @@
"esbuild": "^0.17.12",
"json-schema-to-ts": "^2.7.2",
"serverless": "^3.28.1",
"serverless-dotenv-plugin": "^6.0.0",
"serverless-esbuild": "^1.42.0",
"serverless-offline": "^12.0.4",
"ts-node": "^10.9.1",
@ -35,7 +36,6 @@
"@prisma/client": "^4.13.0",
"@types/node": "^18.15.11",
"aws-sdk": "^2.1342.0",
"dotenv": "^16.0.3",
"prisma": "^4.13.0",
"ts-node": "^10.9.1",
"typescript": "^5.0.4",

View File

@ -1,5 +1,6 @@
generator client {
provider = "prisma-client-js"
binaryTargets = ["native", "rhel-openssl-1.0.x"]
}
datasource db {

87
serverless/scripts/deploy.sh Executable file
View File

@ -0,0 +1,87 @@
#!/bin/bash
bold=$(tput bold)
normal=$(tput sgr0)
source .env
echo "${bold}Starting the deployment process${normal}"
echo "${bold}Installing dependencies via Yarn${normal}"
yarn
if [[ -z "${DATABASE_URL}" ]]; then
printf "%s" "Enter the Mongo Database URL: "
read -r DB_URL
export DATABASE_URL=$DB_URL
fi
if [[ -z "${JSON_RPC}" ]]; then
printf "%s" "Enter the JSON RPC endpoint: "
read -r JSON_RPC
export JSON_RPC=$JSON_RPC
fi
if [[ -z "${CONTRACT_ADDRESS}" ]]; then
printf "%s" "Enter the contract address: "
read -r CONTRACT_ADDRESS
export CONTRACT_ADDRESS=$CONTRACT_ADDRESS
fi
if [[ -z "${PRIVATE_KEY}" ]]; then
printf "%s" "Enter the private key: "
read -r PRIVATE_KEY
export PRIVATE_KEY=$PRIVATE_KEY
fi
if [[ -z "${AWS_ACCESS_KEY_ID}" ]]; then
printf "%s" "Enter the AWS access key ID: "
read -r AWS_ACCESS_KEY_ID
export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
fi
if [[ -z "${AWS_SECRET_ACCESS_KEY}" ]]; then
printf "%s" "Enter the AWS secret access key: "
read -r AWS_SECRET_ACCESS_KEY
export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
fi
echo "${bold}Copying the Prisma schema file to function directories${normal}"
cp prisma/schema.prisma dist/serverless/src/functions/builds/
cp prisma/schema.prisma dist/serverless/src/functions/mints/
echo "${bold}Running the build command${normal}"
yarn build
echo "${bold}Copying the rhel openssl engine to dist/${normal}"
cp node_modules/.prisma/client/libquery_engine-rhel-openssl-1.0.x.so.node dist/serverless/src/functions/mints
cp node_modules/.prisma/client/libquery_engine-rhel-openssl-1.0.x.so.node dist/serverless/src/functions/builds
echo "${bold}Copying the .env file to dist/${normal}"
cp .env src/
echo "${bold}Copying the Prisma schema file to function directories${normal}"
cp prisma/schema.prisma dist/serverless/src/functions/builds/
cp prisma/schema.prisma dist/serverless/src/functions/mints/
echo "${bold}Generating Prisma Client${normal}"
yarn prisma:generate
echo "${bold}Creating layer zip files${normal}"
/bin/bash ./scripts/prepare-libs-lambda-layer.sh
/bin/bash ./scripts/prepare-prisma-client-lambda-layer.sh
/bin/bash ./scripts/prepare-node-modules-lambda-layer.sh
echo "${bold}Deploying to AWS lambda${normal}"
yarn sls deploy --stage dev --verbose
# step 0 -> run yarn
# step 1 -> take params (env variables)
# step 2 -> build tsc files with yarn build
# step 3 -> run yarn generate:prisma
# step 4 -> run the other shell scripts for layer generation
# step 5 -> run the deployment command to aws
# TODO the .env file needs to move to the inside of the serverless dir in dist (zip file)
# TODO Prisma.schema files aren't being packaged with the functions in the nfa-serverless.zip file in .serverless.

View File

@ -6,6 +6,7 @@ plugins:
- serverless-offline
provider:
timeout: 150
name: aws
runtime: nodejs18.x
stage: ${opt:stage, 'prd'}
@ -26,6 +27,10 @@ layers:
TopicPrismaAwsPrismaClient:
path: lambda-layers-prisma-client
package: {
patterns: ["dist/**/*.prisma", "**/libquery_engine-rhel-openssl-1.0.x.so.node", "dist/serverless/.env", '!node_modules/**'],
}
custom:
esbuild:
bundle: true
@ -40,46 +45,42 @@ custom:
functions:
submitBuildInfo:
handler: src/functions/builds/handler.submitBuildInfo # Change `src` to `dist` for deployment
# Deployment:
handler: ./dist/serverless/src/functions/builds/handler.submitBuildInfo # TODO This will not work, need to change to nfa-serverless/dist/serverless/src/functions/builds/handler.submitBuildInfo
# Local development:
# handler: ./src/functions/builds/handler.submitBuildInfo
events:
- http:
path: build
method: post
cors: true
request:
parameters:
querystrings:
githubOrg: true
githubRepo: true
commitHash: true
author: true
timestamp: true
ipfsHash: true
tokenId: true
environment:
environment: # TODO They won't be loaded from the shell environment, need to find a way to pass them from the deployment script
NODE_ENV: production
DATABASE_URL: ${env:DATABASE_URL}
CONTRACT_ADDRESS: ${env:CONTRACT_ADDRESS}
PRIVATE_KEY: ${env:PRIVATE_KEY}
JSON_RPC: ${env:JSON_RPC}
# DATABASE_URL: ${env:DATABASE_URL}
# CONTRACT_ADDRESS: ${env:CONTRACT_ADDRESS}
# PRIVATE_KEY: ${env:PRIVATE_KEY}
# JSON_RPC: ${env:JSON_RPC}
layers:
- { Ref: TopicAwsNodeModulesLambdaLayer }
- { Ref: TopicAwsLibsLambdaLayer }
- { Ref: TopicPrismaAwsPrismaClientLambdaLayer }
submitMintInfo:
handler: src/functions/mints/handler.submitMintInfo
# Deployment:
handler: ./dist/serverless/src/functions/mints/handler.submitMintInfo # TODO This will not work, need to change to nfa-serverless/dist/serverless/src/functions/mints/handler.submitMintInfo
# Local development:
# handler: ./src/functions/mints/handler.submitMintInfo
events:
- http:
path: mint
method: post
cors: true
environment:
environment: # TODO They won't be loaded from the shell environment, need to find a way to pass them from the deployment script
NODE_ENV: production
DATABASE_URL: ${env:DATABASE_URL}
CONTRACT_ADDRESS: ${env:CONTRACT_ADDRESS}
PRIVATE_KEY: ${env:PRIVATE_KEY}
JSON_RPC: ${env:JSON_RPC}
# DATABASE_URL: ${env:DATABASE_URL}
# CONTRACT_ADDRESS: ${env:CONTRACT_ADDRESS}
# PRIVATE_KEY: ${env:PRIVATE_KEY}
# JSON_RPC: ${env:JSON_RPC}
layers:
- { Ref: TopicAwsNodeModulesLambdaLayer }
- { Ref: TopicAwsLibsLambdaLayer }

View File

@ -27,7 +27,8 @@ export const submitBuildInfo = async (
ipfsHash: data.ipfsHash,
domain: data.domain,
};
console.log(buildInfo);
// Add build record to the database, if it's not already added
const buildRecord = await prisma.builds.findMany({
where: {
@ -37,8 +38,12 @@ export const submitBuildInfo = async (
domain: buildInfo.domain,
},
});
console.log(buildRecord);
if (buildRecord.length == 0) {
console.log('here i am');
await prisma.builds.create({
data: {
githubRepository: buildInfo.githubRepository,

View File

@ -14,22 +14,20 @@ export const submitMintInfo = async (
///context: APIGatewayEventRequestContext
): Promise<APIGatewayProxyResult> => {
try {
if (event.body === null) {
if (event.body === null || event.body === undefined) {
return formatJSONResponse({
status: 422,
message: 'Required parameters were not passed.',
});
}
const id = v4();
/**if (!verifyAlchemySig(event.headers.xalchemywork)) {
throw new Error('Invalid sig');
}**/
const eventBody = JSON.parse(event.body);
const topics = eventBody.event.data.block.logs[1].slice(1, 3);
const topics = eventBody.event.data.block.logs[1].topics.slice(1, 4);
const hexCalldata = eventBody.event.data.block.logs[1].data;
const decodedLogs = web3.eth.abi.decodeLog(
[
{
@ -131,9 +129,9 @@ export const submitMintInfo = async (
ipfsHash: decodedLogs.ipfsHash,
domain: decodedLogs.externalURL,
};
initPrisma();
// Check if there is any build associated with the repository, commit hash, tokenId, and ipfsHash
const build = await prisma.builds.findMany({

File diff suppressed because one or more lines are too long

View File

@ -1,8 +1,5 @@
import Web3 from 'web3';
import * as abiFile from '../../../contracts/deployments/goerli/FleekERC721.json';
import * as dotenv from 'dotenv';
dotenv.config();
import * as abiFile from './FleekERC721.json';
if (
process.env.PRIVATE_KEY === undefined ||
@ -15,7 +12,6 @@ const contract_address = abiFile.address;
export const abi = abiFile.abi as any;
export const web3 = new Web3(process.env.JSON_RPC);
export const account = web3.eth.accounts.wallet.add(process.env.PRIVATE_KEY);
web3.eth.setProvider(Web3.givenProvider);
export const nfaContract = new web3.eth.Contract(abi, contract_address);
export const account = web3.eth.accounts.privateKeyToAccount(
process.env.PRIVATE_KEY
);

View File

@ -2765,6 +2765,11 @@ dom-walk@^0.1.0:
resolved "https://registry.yarnpkg.com/dom-walk/-/dom-walk-0.1.2.tgz#0c548bef048f4d1f2a97249002236060daa3fd84"
integrity sha512-6QvTW9mrGeIegrFXdtQi9pk7O/nSK6lSdXW2eqUspN5LWD7UTji2Fqw5V2YLjBpHEoU9Xl/eUWNpDeZvoyOv2w==
dotenv-expand@^10.0.0:
version "10.0.0"
resolved "https://registry.yarnpkg.com/dotenv-expand/-/dotenv-expand-10.0.0.tgz#12605d00fb0af6d0a592e6558585784032e4ef37"
integrity sha512-GopVGCpVS1UKH75VKHGuQFqS1Gusej0z4FyQkPdwjil2gNIv+LNsqBlboOzpJFZKVT95GkCyWJbBSdFEFUWI2A==
dotenv-expand@^9.0.0:
version "9.0.0"
resolved "https://registry.npmjs.org/dotenv-expand/-/dotenv-expand-9.0.0.tgz"
@ -5580,6 +5585,15 @@ serve-static@1.15.0:
parseurl "~1.3.3"
send "0.18.0"
serverless-dotenv-plugin@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/serverless-dotenv-plugin/-/serverless-dotenv-plugin-6.0.0.tgz#c3e0c2e7fb79641433497085eecc30fe150b5ca0"
integrity sha512-8tLVNwHfDO0sBz6+m+DLTZquRk0AZq9rzqk3kphm1iIWKfan9R7RKt4hdq3eQ0kmDoqzudjPYBEXAJ5bUNKeGQ==
dependencies:
chalk "^4.1.2"
dotenv "^16.0.3"
dotenv-expand "^10.0.0"
serverless-esbuild@^1.42.0:
version "1.42.0"
resolved "https://registry.npmjs.org/serverless-esbuild/-/serverless-esbuild-1.42.0.tgz"