Practice with AWS

*Please install the app on at least two obniz and place Logtta CO2 near the obniz.
Also, please make sure to create an instance on Amazon EC2.

When using only master

Connect to Amazon EC2 using SSH.

$ ssh ec2-user@{EC2 address} -i ~/.ssh/{key}

Follow the instructions on the next page.

Tutorial: Setting Up Node.js on an Amazon EC2 Instance

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
$ . ~/.nvm/nvm.sh
$ nvm install 16.13.0
$ node -e "console.log('Running Node.js ' + process.version)"

Clone https://github.com/obniz/obniz-app-sdk_logtta-co2_demo and run it while setting environment variables.

The Typescript file is run directly as Node.js using ts-node.

$ sudo yum install git
$ git clone https://github.com/obniz/obniz-app-sdk_logtta-co2_demo.git
$ cd obniz-app-sdk_logtta-co2_demo
$ npm ci
$ APP_TOKEN={app token} LOGTTA_CO2_ADDRESS={MAC address of Logtta CO2} npx ts-node src/index.ts

The "app token" is the token you get when you create a Hosted App in obniz Cloud in App implementation.

Load balancing using master/slave

IP address configuration (LAN).

master: 172.31.40.186

slave: 172.31.35.144

Create another master-only configuration and install Redis on the master.

You can also use Amazon ElastiCache, but we'll install it on the master to simplify the explanation.

# master server
$ sudo amazon-linux-extras install redis6
$ redis-server --protected-mode no # Start with a lower security level for simplicity (disconnecting ssh during startup will also terminate Redis).

Edit the master security group so that it is accessible from the slave.

security-rule

slave startup.

# slave server
$ git checkout scaling
$ INSTANCE_TYPE=slave REDIS_URL=redis://172.31.40.186:6379 APP_TOKEN={app token} LOGTTA_CO2_ADDRESS={MAC address of Logtta CO2} npx ts-node src/index.ts

master startup.

# master server
$ git checkout scaling
$ INSTANCE_TYPE=master APP_TOKEN={app token} LOGTTA_CO2_ADDRESS={MAC address of Logtta CO2} npx ts-node src/index.ts
# Now you can see that obniz is assigned to both slave and master and load balanced.

⚠Note that start the slave first, or the master will be taken by the slave which the master contains.

If a slave stops while a worker is running, it will be automatically allocated to another slave.

In this example, the slave which the master contains will be allocated.