Skip to content

Commit 56f8209

Browse files
author
Ulfat
authored
Merge pull request #254 from electrocucaracha/add_terraform_scripts
Provide Terraform scripts
2 parents dddcb1a + 5888d41 commit 56f8209

File tree

10 files changed

+313
-0
lines changed

10 files changed

+313
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@ dclbench_stats.csv
2828
dclbench_stats_history.csv
2929
dclbench_exceptions.csv
3030
bench/txns
31+
terraform.tfstate*
32+
.terraform*

deployment/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Terraform deployment
2+
3+
[Terraform][1] is an open-source infrastructure as code software tool that
4+
codifies cloud APIs into declarative configuration files.
5+
6+
## Requirements
7+
8+
1. Install [Terraform][2] and [AWS][3] CLIs
9+
2. Install Ansible requirements. `pip install -r requirements.txt`
10+
3. Configure AWS access keys for CLI. `aws configure`
11+
4. Modify the [deploy ansible playbook](ansible/deploy.yml) accordingly.
12+
13+
## Deployment
14+
15+
The following instructions automates the provision of a DCL node in AWS Cloud.
16+
17+
```bash
18+
cd terraform/
19+
terraform init
20+
terraform apply -auto-approve
21+
```
22+
23+
[1]: https://www.terraform.io/
24+
[2]: https://learn.hashicorp.com/tutorials/terraform/install-cli
25+
[3]: https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html

deployment/ansible/deploy.yml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
# Copyright 2022 Samsung Corporation
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
- name: bootstrap DCL nodes
17+
hosts: all
18+
become: true
19+
roles:
20+
- bootstrap
21+
tasks:
22+
- name: get genesis.json file
23+
get_url:
24+
url: https://raw.githubusercontent.com/zigbee-alliance/distributed-compliance-ledger/master/deployment/persistent_chains/testnet-2.0/genesis.json
25+
dest: /var/lib/dcl/.dcl/config/genesis.json
26+
- name: start DCL service
27+
become: true
28+
service:
29+
name: dcld
30+
state: started
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
# Copyright 2022 Samsung Corporation
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
plugin: amazon.aws.aws_ec2
17+
regions:
18+
- us-west-1
19+
boto_profile: default
20+
filters:
21+
instance-state-name: running
22+
compose:
23+
ansible_host: public_ip_address
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
# Copyright 2022 Samsung Corporation
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
chain_id: test-net2
17+
moniker: tf-test-node
18+
# These accounts must be configured per node
19+
accounts:
20+
- name: user1
21+
passphrase: password123
22+
roles:
23+
- NodeAdmin
24+
- name: user2
25+
passphrase: secret1234
26+
roles:
27+
- Trustee

deployment/requirements.in

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2022 Samsung Corporation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
ansible # GPLv3
16+
boto3 # Apache-2.0
17+
botocore # Apache-2.0

deployment/requirements.txt

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#
2+
# This file is autogenerated by pip-compile with python 3.8
3+
# To update, run:
4+
#
5+
# pip-compile --output-file=requirements.txt requirements.in
6+
#
7+
ansible==5.4.0
8+
# via -r requirements.in
9+
ansible-core==2.12.3
10+
# via ansible
11+
boto3==1.21.18
12+
# via -r requirements.in
13+
botocore==1.24.18
14+
# via
15+
# -r requirements.in
16+
# boto3
17+
# s3transfer
18+
cffi==1.15.0
19+
# via cryptography
20+
cryptography==36.0.1
21+
# via ansible-core
22+
jinja2==3.0.3
23+
# via ansible-core
24+
jmespath==0.10.0
25+
# via
26+
# boto3
27+
# botocore
28+
markupsafe==2.1.0
29+
# via jinja2
30+
packaging==21.3
31+
# via ansible-core
32+
pycparser==2.21
33+
# via cffi
34+
pyparsing==3.0.7
35+
# via packaging
36+
python-dateutil==2.8.2
37+
# via botocore
38+
pyyaml==6.0
39+
# via ansible-core
40+
resolvelib==0.5.4
41+
# via ansible-core
42+
s3transfer==0.5.2
43+
# via boto3
44+
six==1.16.0
45+
# via python-dateutil
46+
urllib3==1.26.8
47+
# via botocore

deployment/terraform/main.tf

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = ">= 3.72"
6+
}
7+
}
8+
}
9+
10+
# Configure the AWS Provider
11+
provider "aws" {
12+
region = var.region
13+
}
14+
15+
module "dcl_sg" {
16+
source = "terraform-aws-modules/security-group/aws"
17+
version = "~> 4.0"
18+
19+
name = "dcl-security_group"
20+
description = "Security group for accessing DCL nodes from outside"
21+
vpc_id = module.network_lab.vpc_id
22+
23+
ingress_cidr_blocks = ["0.0.0.0/0"]
24+
ingress_rules = ["all-icmp", "ssh-tcp"]
25+
egress_rules = ["all-all"]
26+
ingress_with_cidr_blocks = [
27+
{
28+
from_port = 26656
29+
to_port = 26656
30+
protocol = "tcp"
31+
description = "DCL p2p"
32+
cidr_blocks = "0.0.0.0/0"
33+
},
34+
{
35+
from_port = 26657
36+
to_port = 26657
37+
protocol = "tcp"
38+
description = "DCL RPC"
39+
cidr_blocks = "0.0.0.0/0"
40+
},
41+
]
42+
}
43+
44+
resource "aws_instance" "dcl_node" {
45+
ami = data.aws_ami.ubuntu.id
46+
instance_type = "c5.4xlarge"
47+
48+
subnet_id = element(module.network_lab.public_subnets, 0)
49+
vpc_security_group_ids = [module.dcl_sg.security_group_id]
50+
51+
key_name = aws_key_pair.key_pair.id
52+
monitoring = true
53+
54+
root_block_device {
55+
encrypted = true
56+
volume_size = 20
57+
}
58+
59+
connection {
60+
type = "ssh"
61+
host = self.public_ip
62+
user = var.ssh_username
63+
private_key = file(var.ssh_private_key_path)
64+
}
65+
66+
provisioner "remote-exec" {
67+
inline = [
68+
"sudo apt-get update",
69+
"sudo apt-get install -y --no-install-recommends python3",
70+
]
71+
}
72+
73+
provisioner "local-exec" {
74+
command = "ansible-playbook -i ../ansible/inventory/aws_ec2.yml -u ${var.ssh_username} ../ansible/deploy.yml"
75+
environment = {
76+
ANSIBLE_HOST_KEY_CHECKING = "False"
77+
}
78+
}
79+
80+
metadata_options {
81+
http_endpoint = "enabled"
82+
http_tokens = "required"
83+
}
84+
}
85+
86+
data "aws_ami" "ubuntu" {
87+
most_recent = true
88+
owners = ["099720109477"]
89+
90+
filter {
91+
name = "name"
92+
values = ["ubuntu-minimal/images/hvm-ssd/ubuntu-focal-20.04-amd64-minimal-*"]
93+
}
94+
95+
filter {
96+
name = "virtualization-type"
97+
values = ["hvm"]
98+
}
99+
}
100+
101+
resource "aws_key_pair" "key_pair" {
102+
public_key = file(var.ssh_public_key_path)
103+
}
104+
105+
data "aws_availability_zones" "available" {
106+
state = "available"
107+
}
108+
109+
module "network_lab" {
110+
source = "terraform-aws-modules/vpc/aws"
111+
version = "~> 3.0"
112+
113+
name = "dcl-network"
114+
cidr = "10.0.0.0/16"
115+
116+
azs = [data.aws_availability_zones.available.names[0]]
117+
private_subnets = ["10.0.1.0/24"]
118+
public_subnets = ["10.0.101.0/24"]
119+
enable_nat_gateway = true
120+
}

deployment/terraform/outputs.tf

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "ssh_console" {
2+
value = format("ssh -o 'StrictHostKeyChecking=no' ubuntu@%s", aws_instance.dcl_node.public_ip)
3+
}

deployment/terraform/variables.tf

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
variable "ssh_public_key_path" {
2+
description = "SSH public key file path"
3+
default = "~/.ssh/id_rsa.pub"
4+
}
5+
6+
variable "ssh_private_key_path" {
7+
description = "SSH private key file path"
8+
default = "~/.ssh/id_rsa"
9+
}
10+
11+
variable "ssh_username" {
12+
description = "SSH username"
13+
default = "ubuntu"
14+
}
15+
16+
variable "region" {
17+
description = "AWS Region"
18+
default = "us-west-1"
19+
}

0 commit comments

Comments
 (0)