
*Please install the app on at least two obniz and place [Logtta CO2](https://obniz.com/sdk/parts/LogttaCO2/README.md) near the obniz.
Also, please make sure to create an instance on Amazon EC2.

### When using only master

Connect to Amazon EC2 using SSH.

```bash
$ 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](https://docs.aws.amazon.com/ja_jp/sdk-for-javascript/v2/developer-guide/setting-up-node-on-ec2-instance.html)


```bash
$ 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](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.

```bash
$ 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](/guides/obniz-for-full-scale-use/intermediate-level/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.

```bash
# 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](../images/security-rule.png)

**slave startup**.

```bash
# 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**.

```bash
# 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.
