Background
To align with the Fourth Industrial Revolution (4IR) and embrace rapid technological changes globally, I decided to deploy an NFTs Marketplace on AWS quickly. After some exploration, it indeed turned out to be very fast!
The choice of AWS (or any cloud provider) is because, as time and energy become limited, managing physical servers isn't feasible. Cloud infrastructure, as people say, is much more convenient!
We will use the Ethereum Ropsten Testnet with support from Amazon Managed Blockchain, along with a few other AWS services, all initiated via CloudFormation. The highest cost will likely be from Amazon Managed Blockchain, as it’s billed hourly on-demand, while other services mostly fall under the free tier.
Getting Started
Check the Node.js version; older versions can cause compatibility issues. On Ubuntu, the default is often version 10.x, so update if necessary: https://thocode.net/blog/cai-dat-node-js-phien-ban-moi-nhat-tren-ubuntu-wsl2/. CDK won’t run with outdated Node.js versions.
Clone the source and install dependencies:
git clone https://github.com/aws-samples/simple-nft-marketplace.git
npm install
Deploy Contract
We’ll use Hardhat to deploy an ERC721 contract written in Solidity on the Ethereum Ropsten Testnet. Hardhat is like the JDK for Java or .NET SDK for .NET.
In the /contract folder, run:
npm install
npx hardhat compile
Testing Locally
Open two terminals to simulate a server-client setup.
- First terminal: Run:
npx hardhat node
This generates 20 Accounts, each with 10,000 ETH. You can use a wallet like MetaMask or Trust Wallet, set to the testnet, and import a test account.
Example account:
Account #0: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266 (10000 ETH)
Private Key: 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
- Second terminal: Run:
npx hardhat console --network localhost
You should see: Contract deployed at 0x5FbDB2315678afecb367f032d93F642f64180aa3
In the Hardhat console, generate an NFT token using SimpleERC721 in contracts/SimpleERC721.sol
:
const SimpleERC721 = await ethers.getContractFactory('SimpleERC721');
const contract = await SimpleERC721.attach('<contract address>');
await contract.newItem('dummy');
You should see confirmation of success!
Returning to MetaMask, you'll see the transaction cost incurred.
If you check Terminal 1 (server), you’ll also see information like this.
Testing on Ethereum Ropsten Testnet
Log in to AWS Console > Managed Blockchain > Networks > Ethereum Testnet Ropsten > create a node > copy the HTTP Endpoint and insert it into the following command. Make sure it includes https, as missing it can cause errors.
export AMB_HTTP_ENDPOINT='<my-endpoint>'
Next, create an account to deploy the contract. Unlike the 20 accounts with 10K ETH above, this new account has no ETH, so you’ll need to find Ropsten Faucets to add some rETH to cover gas fees. Search Google for faucet sites; some are easy, others may require captcha verification.
npx hardhat account
Take the output and insert it here:
export PRIVATE_KEY='<0x...>'
Then, go to AWS Console > Identity and Access Management (IAM) to create an access key and secret access key. In IAM, click Manage access keys to switch to the old version if you're using IAMv2. Fill in the information here:
export AWS_ACCESS_KEY_ID='<...>'
export AWS_SECRET_ACCESS_KEY='<...>'
Run this command to deploy with Amazon Managed Blockchain:
npx hardhat run --network amb scripts/deploy-amb.js
If you’re using a region other than us-east-1, set it below and then rerun the deploy-amb.js file. Success will display “Contract deployed at,” along with the gas fee deduction.
export AWS_DEFAULT_REGION='<your-region>'
Deploy API for Backend
Install AWS CDK:
npm install -g aws-cdk
The commands below should be run in the /provision directory:
npm install
export CONTRACT_ADDRESS='<contract from (1)>'
cdk bootstrap
Running the above creates a **CDKToolkit **stack with CloudFormation to prepare for deployment.
Run the following to install dependencies and return:
cd lambda; npm install; cd -;
Copy the compiled contract (SimpleERC721.json) to /lambda/contracts/:
cp ../contract/artifacts/contracts/SimpleERC721.sol/SimpleERC721.json lambda/contracts/.
Build CDK:
npm run build
Check that environment variables are correctly set:
echo $AMB_HTTP_ENDPOINT
echo $CONTRACT_ADDRESS
If everything’s correct, deploy, and CloudFormation will create the SimpleNftMarketplaceStack stack:
cdk deploy SimpleNftMarketplaceStack
Encountering Node.js compatibility issues can lead to random errors if the supported CDK version isn’t updated. After resolving such an error with electron permissions, the correct solution is:
sudo npm uninstall -g electron
sudo npm install -g electron --unsafe-perm=true --allow-root
Successful deployment will output something like this:
SimpleNftMarketplaceStack.NftApiEndpoint = https://***.execute-api.ap-southeast-1.amazonaws.com/prod/
SimpleNftMarketplaceStack.UserPoolClientId = ***
SimpleNftMarketplaceStack.UserPoolId = ap-southeast-1***
4. Frontend
In the /marketplace folder, set environment variables as below:
cat <<EOF > .env.local
VUE_APP_AWS_REGION=<region>
VUE_APP_API_ENDPOINT=<api-endpoint>
VUE_APP_USER_POOL_ID=<user-pool>
VUE_APP_USER_POOL_WEB_CLIENT_ID=<web-client>
EOF
Run: sudo npm install
If you see an error like Response timeout while trying to fetch https://registry.npmjs.org/fs-extra, adjust the timeout: npm set timeout=100000
Clean the npm cache: npm cache clean --force
Then, run: sudo npm run serve
For errors related to vue-cli-service, install the necessary dependency and retry npm run serve: npm i @vue/cli-service
When you see the Local and Network addresses, open the browser to confirm it’s working correctly.
Cover Photo by Rapha Wilde on Unsplash.
Top comments (0)