DEV Community

Cover image for Deploying an NFTs Marketplace Solution on AWS Super Fast
Anh Dung
Anh Dung

Posted on

Deploying an NFTs Marketplace Solution on AWS Super Fast

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.

Image description
Source: AWS Sample

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.

Image description

Clone the source and install dependencies:

git clone https://github.com/aws-samples/simple-nft-marketplace.git
npm install
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Testing Locally

Open two terminals to simulate a server-client setup.

  • First terminal: Run:
npx hardhat node
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • Second terminal: Run:
npx hardhat console --network localhost
Enter fullscreen mode Exit fullscreen mode

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');
Enter fullscreen mode Exit fullscreen mode

You should see confirmation of success!

Image description

Returning to MetaMask, you'll see the transaction cost incurred.
Image description

If you check Terminal 1 (server), you’ll also see information like this.

Image description

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>'
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Take the output and insert it here:

export PRIVATE_KEY='<0x...>'
Enter fullscreen mode Exit fullscreen mode

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='<...>'
Enter fullscreen mode Exit fullscreen mode

Run this command to deploy with Amazon Managed Blockchain:

npx hardhat run --network amb scripts/deploy-amb.js
Enter fullscreen mode Exit fullscreen mode

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>'
Enter fullscreen mode Exit fullscreen mode

Deploy API for Backend

Install AWS CDK:

npm install -g aws-cdk
Enter fullscreen mode Exit fullscreen mode

The commands below should be run in the /provision directory:

npm install
export CONTRACT_ADDRESS='<contract from (1)>'
cdk bootstrap
Enter fullscreen mode Exit fullscreen mode

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 -;
Enter fullscreen mode Exit fullscreen mode

Copy the compiled contract (SimpleERC721.json) to /lambda/contracts/:

cp ../contract/artifacts/contracts/SimpleERC721.sol/SimpleERC721.json lambda/contracts/.
Enter fullscreen mode Exit fullscreen mode

Build CDK:

npm run build
Enter fullscreen mode Exit fullscreen mode

Check that environment variables are correctly set:

echo $AMB_HTTP_ENDPOINT
echo $CONTRACT_ADDRESS
Enter fullscreen mode Exit fullscreen mode

If everything’s correct, deploy, and CloudFormation will create the SimpleNftMarketplaceStack stack:

cdk deploy SimpleNftMarketplaceStack
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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***
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)