From 93bac7a421e2d1d4336a2eea71cc9440bbf4b0a4 Mon Sep 17 00:00:00 2001 From: Maksim Dogonov Date: Fri, 17 Nov 2023 13:50:35 +0400 Subject: [PATCH] Add files for modules --- README.md | 13 +++++ cloud_init.yml.tftpl | 78 +++++++++++++++++++++++++ examples/sui-node/main.tf | 1 + examples/sui-node/versions.tf | 6 ++ instance.tf | 104 ++++++++++++++++++++++++++++++++++ main.tf | 3 + vars.tf | 40 +++++++++++++ versions.tf | 10 ++++ 8 files changed, 255 insertions(+) create mode 100644 cloud_init.yml.tftpl create mode 100644 examples/sui-node/main.tf create mode 100644 examples/sui-node/versions.tf create mode 100644 instance.tf create mode 100644 main.tf create mode 100644 vars.tf create mode 100644 versions.tf diff --git a/README.md b/README.md index a538897..5431006 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,15 @@ # terraform-aws-sui-node Terraform module for deploy sui node on AWS + + +```bash +# Configure AWS credentional +aws configure + +# Search Linux image if need +aws ec2 describe-images --owners amazon --filters "Name=name,Values=debian-11-amd64*" --query "Images | [0].ImageId" --output text\n + +# Create SSH keypair +aws ec2 create-key-pair --key-name sui-node-key --query 'KeyMaterial' --output text > aws-sui-node-key.pem + +``` \ No newline at end of file diff --git a/cloud_init.yml.tftpl b/cloud_init.yml.tftpl new file mode 100644 index 0000000..37ed9eb --- /dev/null +++ b/cloud_init.yml.tftpl @@ -0,0 +1,78 @@ +#cloud-config +#users: +# - name: devops +# groups: users, admin +# sudo: ALL=(ALL) NOPASSWD:ALL +# shell: /bin/bash +# ssh_authorized_keys: +# - ssh-rsa +package_update: true +package_upgrade: true +packages: + - vim + - ca-certificates + - curl + - gnupg + - wget +write_files: + - path: /bin/docker-up + content: | + #!/usr/bin/bash + docker-compose up -d + - path: /etc/systemd/system/docker-up.service + content: | + [Unit] + Description=SUI NODE Startup + + [Service] + Type=simple + ExecStart=/bin/docker-up + WorkingDirectory=/opt/service + + [Install] + WantedBy=default.target + - path: /etc/profile.d/service-env.sh + content: | + export APP_VERSION=${version} + - path: /opt/service/docker-compose.yml + content: | + version: '3.9' + services: + sui-node: + container_name: sui-node + image: mysten/sui-node:${version} + command: ["sui-node", "--config-path", "/opt/sui/config/fullnode.yaml"] + restart: always + #environment: + ports: + - "8084:8084/udp" + - "9000:9000" + - "9184:9184" + volumes: + - ./volumes/root:/root + - ./volumes/fullnode.yaml:/opt/sui/config/fullnode.yaml:ro + - ./volumes/genesis.blob:/opt/sui/config/genesis.blob:ro + - ./volumes/suidb:/opt/sui/db:rw + logging: + driver: "json-file" + options: + max-size: 10m + max-file: "3" +runcmd: + - echo '${opt} /opt/service/volumes xfs discard,nofail,defaults 0 0' >> /etc/fstab + - mkdir -p /opt/service/volumes && mount /opt/service/volumes + - install -m 0755 -d /etc/apt/keyrings + - curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg + - chmod a+r /etc/apt/keyrings/docker.gpg + - echo "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null + - apt update && apt -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin + - wget https://github.com/docker/compose/releases/download/v2.22.0/docker-compose-linux-x86_64 + - mv docker-compose-linux-x86_64 /bin/docker-compose && chmod a+x /bin/docker-compose + - echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf + - chmod u+x /bin/docker-up + - systemctl enable docker + - systemctl daemon-reload && systemctl enable docker-up.service + - wget https://github.com/MystenLabs/sui/raw/main/crates/sui-config/data/fullnode-template.yaml -O /opt/service/volumes/fullnode.yaml + - wget https://github.com/MystenLabs/sui-genesis/raw/main/${sui_network}/genesis.blob -O /opt/service/volumes/genesis.blob + - sed -i 's/127.0.0.1/0.0.0.0/' /opt/service/volumes/fullnode.yaml + - shutdown -r now \ No newline at end of file diff --git a/examples/sui-node/main.tf b/examples/sui-node/main.tf new file mode 100644 index 0000000..75db792 --- /dev/null +++ b/examples/sui-node/main.tf @@ -0,0 +1 @@ +terraform {} diff --git a/examples/sui-node/versions.tf b/examples/sui-node/versions.tf new file mode 100644 index 0000000..0852ff3 --- /dev/null +++ b/examples/sui-node/versions.tf @@ -0,0 +1,6 @@ +terraform { + required_providers { + } + + required_version = ">= 1.0.2" +} \ No newline at end of file diff --git a/instance.tf b/instance.tf new file mode 100644 index 0000000..6b3b31d --- /dev/null +++ b/instance.tf @@ -0,0 +1,104 @@ +# Create a VPC +resource "aws_vpc" "my_vpc" { + cidr_block = "10.0.0.0/16" + enable_dns_support = true + enable_dns_hostnames = true + + tags = { + Name = var.vpc_name + } +} + +resource "aws_eip" "ip_ip_env" { + instance = aws_instance.my_instance.id + domain = "vpc" +} + +resource "aws_internet_gateway" "vpc_gw" { + vpc_id = aws_vpc.my_vpc.id + +} + +resource "aws_route_table" "route_table" { + vpc_id = aws_vpc.my_vpc.id + route { + cidr_block = "0.0.0.0/0" + gateway_id = "${aws_internet_gateway.vpc_gw.id}" + } + +} +resource "aws_route_table_association" "subnet_association" { + subnet_id = aws_subnet.public_subnet.id + route_table_id = aws_route_table.route_table.id +} + +# Create a public subnet +resource "aws_subnet" "public_subnet" { + vpc_id = aws_vpc.my_vpc.id + cidr_block = "10.0.1.0/24" + availability_zone = var.aws_availability_zone # Set your desired availability zone + + tags = { + Name = var.vpc_name + } +} + +resource "aws_security_group" "allow_all" { + name = "${var.vpc_name}-allow-all-sg" + vpc_id = "${aws_vpc.my_vpc.id}" + ingress { + cidr_blocks = [ + "0.0.0.0/0" + ] + from_port = 22 + to_port = 22 + protocol = "tcp" + } + // Terraform removes the default rule + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } +} + +data "aws_ami" "latest_debian_linux" { + most_recent = true + owners = ["amazon"] + + filter { + name = "name" + values = [ var.aws_image_name ] + } +} + +# Create an EC2 instance in the public subnet +resource "aws_instance" "my_instance" { + ami = data.aws_ami.latest_debian_linux.id + instance_type = var.aws_instance_type + subnet_id = aws_subnet.public_subnet.id + user_data = templatefile("${path.module}/cloud_init.yml.tftpl", { + opt = "" + version = var.app_version + sui_network = var.sui_network + }) + key_name = var.ssh_key_name # Update with your key pair name + + vpc_security_group_ids = [aws_security_group.allow_all.id] + +# lifecycle { +# replace_triggered_by = [ +# user_data, +# key_name +# ] +# } + + tags = { + Name = "${var.vpc_name}" + } +} + +output "host_ip" { + value = aws_eip.ip_ip_env.public_ip +} \ No newline at end of file diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..5d581d7 --- /dev/null +++ b/main.tf @@ -0,0 +1,3 @@ +provider "aws" { + region = var.aws_region # Set your desired AWS region +} diff --git a/vars.tf b/vars.tf new file mode 100644 index 0000000..c9c425e --- /dev/null +++ b/vars.tf @@ -0,0 +1,40 @@ +variable "aws_region" { + default = "us-west-2" + description = "AWS region" +} + +variable "aws_availability_zone" { + default = "us-west-2a" + description = "AWS region" +} + +variable "aws_image_name" { + description = "The AMI IMAGE NAME for the EC2 instance" + default = "debian-11-amd64*" # Update with the desired AMI +} + +variable "ssh_key_name" { + description = "The name of the key pair for the EC2 instance" + default = "" # Update with your key pair name +} + +variable "vpc_name" { + default = "sui-node" + description = "vpc name prefix" +} + +variable "aws_instance_type" { + default = "t2.micro" + description = "AWS instance type" +} + +# Version docker containers +variable "app_version" { + default = "testnet" + description = "Docker app version" +} + +variable "sui_network" { + default = "testnet" + description = "SUI network mainnet / testnet / devnet " +} \ No newline at end of file diff --git a/versions.tf b/versions.tf new file mode 100644 index 0000000..bdf6a1f --- /dev/null +++ b/versions.tf @@ -0,0 +1,10 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 5.0" + } + } + + required_version = ">= 1.0.2" +}