This module can be used for building infrastructure for iot-project on AWS. It's also responsible for the deployment process of cloud software.
Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions. Configuration files describe to Terraform the components needed to run a single application or your entire datacenter.
(via: https://www.terraform.io/intro/index.html)
You will need terraform 0.14.9. It can be installed in two ways:
- (Perffered way) You can download terraform binary from this website - please select appropriate version for Your OS. Downloaded file should be placed in this directory (iot-starter/terraform)
- You can install terraform via package manager on Unix like systems (assuming it’s available). To install it on the Fedora Linux follow this link (remember to install version 0.14.9)
You can check your installation with this command (if the terraform was downloaded as a binary file, remember to run this command from "iot-starter/terraform" directory):
-
For Linux:
terraform --version
-
For Windows:
terraform.exe -version
These requirements are needed to build and bundle visualization. Nodejs can be installed in two ways:
-
From the website. Npm should be installed automatically along with nodejs.
-
Via the package manager. For Fedora, to install both packages execute the following command:
sudo dnf install nodejs
Please remember that in case of using Windows during installation of nodejs, it is required to check: "Automatically install the necessary tools" which will install Chocolatey on your computer. You can check if installation succeed with these commands:
node --version
npm --version
Project requires to install node.js in version >= 10.0. To update npm to the latest version:
npm install -g npm@latest
or to update to the most recent release:
npm install -g npm@next
If you are on Windows then you must take additional steps. Run cmd with administrative privileges and run three below commands: (They may take some time)
npm install --global --production [email protected]
npm config set python python2.7
Next you need to set the path for MSBuild.exe. The location of this file may vary. Usually this file resides in:
"C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\XX.X\Bin\MSBuild.exe"
or:
"C:\Program Files (x86)\Microsoft Visual Studio\2015\BuildTools\MSBuild\XX.X\Bin\MSBuild.exe"
Where XX.X is a version and may also vary. If you found the file in 2017 type (remember to change XX.X value!):
npm config set msvs_version 2017
npm config set msbuild_path "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\XX.X\Bin\MSBuild.exe"
If it's the latter, change 2017 to 2015 in above commands.
Enter main directory of the project (iot-starter):
cd iot-starter
Run the following commands to install requirements:
pip install -r requirements.txt
- Prepare
ACCESS_KEY
andSECRET_KEY
, which can be obtained from AWS IAM console. Download AWS CLI from https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html to connect AWS with your account and run following command:
aws configure
If you have trouble generating 'credentials' see the Additional Information and Help section.
You need to create your own s3 bucket in AWS. The whole procedure was described in this website: https://docs.aws.amazon.com/AmazonS3/latest/user-guide/create-bucket.html
Next, you should change "bucket" value to the name of the created bucket for s3 backend in main.tf in terraform directory. (AWS policy says that s3 bucket name should be unique in the world). Value "region" refers to one of AWS regions. Remember to change "region" according to region where you have set your s3 bucket.
terraform {
backend "s3" {
bucket = "YOUR_BUCKET_NAME"
key = "mobile-iot.wizzdev.tfstate"
region = "eu-west-2"
}
}
Due to security reasons you have to modify files devel.tfvars
(developement environment settings) and production.tfvars
(production environment settings) in the terraform/environments
directory.
Changing ESP_HARD_LOGIN, ESP_HARD_PASSWORD, owner and project values is obligatory.
Otherwise, your AWS infrastructure could be accessed by unauthorized devices.
-
Values of "ESP_HARD_LOGIN" and "ESP_HARD_PASSWORD" are independent from the AWS account and should be unique - a new connection is created with these credentials.
-
"project" variable value should be unique for each infrastructure building.
-
Region of S3 bucket created for Terraform state synchronization is independent of these settings and so you can change the "region" variable according to your preferences. All available regions are listed on this webpage: (https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.RegionsAndAvailabilityZones.html)
Example:
mode = "production"
project = "my-mobile-iot"
region = "eu-central-1"
owner = "your_email_adrress"
ESP_HARD_PASSWORD = "TopSecretPassword"
ESP_HARD_LOGIN = "TopSecretLogin"
(Remember to adjust this command depending on your terraform installation!)
terraform init
terraform workspace new production
You can also configure devel
workspace, that can be used for testing and trying new modifications.
Here is a short instruction about how to build your cloud infrastructure for this project.
- Linux:
export VERSION="production";
- Windows:
set VERSION=production
cd scripts
python build_all.py
cd ..
For Linux type $VERSION instead of %VERSION%
terraform workspace select %VERSION%
terraform apply -var-file=./environments/%VERSION%.tfvars
Since Terraform's communication with AWS is not 100% perfect, the following steps are necessary when first establishing a cloud architecture:
- Go to AWS console.
- Choose IoT Core service from the menu.
- Select Act/Rules and then choose the existing rule on "Edit".
- Change SQL version to another, then save. After that, restore the first value and save again.
The above steps are primarily needed when the readings sent to AWS do not appear in the visualization
If any problems occur, please rerun terraform. Another solution to deal with errors is to destroy terraform and build again.
You need to gain programmatic access to your AWS account from the computer if you want to automatically build your infrastructure with terraform. To do that:
- Go to: https://aws.amazon.com/console/ > IAM service (Identity and Access Management) > Users.
- Find your name and click on it. You should be redirected to a detailed view of your account.
- Switch to "Security credentials tabs".
- Click on the "Create access key" button.
- Remember both keys because the secret key will not be able to get the secret key from AWS system.