From 24aa7742455eeded92d214355f4cb47478b579c6 Mon Sep 17 00:00:00 2001 From: maxim Date: Fri, 26 Mar 2021 13:51:36 +0600 Subject: [PATCH] use efs instead of ebs to store mongodb datas --- .../layer1-aws/examples/aws-ec2-pritunl.tf | 17 ++-- terraform/modules/aws-ec2-pritunl/backup.tf | 25 ++++++ terraform/modules/aws-ec2-pritunl/dlm.tf | 84 ------------------- terraform/modules/aws-ec2-pritunl/efs.tf | 26 ++++++ terraform/modules/aws-ec2-pritunl/iam.tf | 41 +++++++-- terraform/modules/aws-ec2-pritunl/main.tf | 45 +++------- terraform/modules/aws-ec2-pritunl/output.tf | 2 +- .../aws-ec2-pritunl/security_groups.tf | 51 +++++------ .../aws-ec2-pritunl/templates/user-data.sh | 40 ++------- .../modules/aws-ec2-pritunl/variables.tf | 25 ++++-- 10 files changed, 163 insertions(+), 193 deletions(-) create mode 100644 terraform/modules/aws-ec2-pritunl/backup.tf delete mode 100644 terraform/modules/aws-ec2-pritunl/dlm.tf create mode 100644 terraform/modules/aws-ec2-pritunl/efs.tf diff --git a/terraform/layer1-aws/examples/aws-ec2-pritunl.tf b/terraform/layer1-aws/examples/aws-ec2-pritunl.tf index f08f81ce..e5f8153b 100644 --- a/terraform/layer1-aws/examples/aws-ec2-pritunl.tf +++ b/terraform/layer1-aws/examples/aws-ec2-pritunl.tf @@ -1,20 +1,27 @@ module "pritunl" { source = "../modules/pritunl" + environment = local.env vpc_id = module.vpc.vpc_id public_subnets = module.vpc.public_subnets - pritunl_sg_rules = [ + ingress_with_cidr_blocks = [ { protocol = "6" from_port = 443 to_port = 443 - cidr_blocks = ["8.8.8.8/32"] # the list of IPs that will have access to the web console + cidr_blocks = "127.0.0.1/32" # IP address that will have access to the web console }, { protocol = "17" - from_port = 19739 #this is a port that we will set in pritunl server configuration (after installation) + from_port = 19739 # this is a port that we will set in pritunl server configuration (after installation) to_port = 19739 - cidr_blocks = ["0.0.0.0/0"] - } + cidr_blocks = "0.0.0.0/0" + }, + { + protocol = "6" + from_port = 80 + to_port = 80 + cidr_blocks = "127.0.0.1/32" # IP address that will have access to the web console + }, ] } diff --git a/terraform/modules/aws-ec2-pritunl/backup.tf b/terraform/modules/aws-ec2-pritunl/backup.tf new file mode 100644 index 00000000..78cb6e92 --- /dev/null +++ b/terraform/modules/aws-ec2-pritunl/backup.tf @@ -0,0 +1,25 @@ +resource "aws_backup_vault" "this" { + name = var.name +} + +resource "aws_backup_plan" "this" { + name = "${var.name}_backup_plan" + rule { + rule_name = "${var.name}_backup_plan_efs" + target_vault_name = aws_backup_vault.this.name + schedule = "cron(0 1 * * ? *)" + lifecycle { + delete_after = 30 + } + } +} + +resource "aws_backup_selection" "efs" { + iam_role_arn = module.backup_role.this_iam_role_arn + name = "${var.name}_backup_selection_efs" + plan_id = aws_backup_plan.this.id + + resources = [ + aws_efs_file_system.this.arn + ] +} diff --git a/terraform/modules/aws-ec2-pritunl/dlm.tf b/terraform/modules/aws-ec2-pritunl/dlm.tf deleted file mode 100644 index 64d5c59e..00000000 --- a/terraform/modules/aws-ec2-pritunl/dlm.tf +++ /dev/null @@ -1,84 +0,0 @@ -resource "aws_iam_role" "dlm_lifecycle_role" { - name = "${var.name}-dlm-lifecycle-role" - - assume_role_policy = <> /etc/security/limits.conf INSTANCE_ID=$(curl http://169.254.169.254/latest/meta-data/instance-id) + # Attach EIP aws ec2 associate-address --instance-id $${INSTANCE_ID} --allocation-id ${eipalloc} --allow-reassociation -# Attach volume for MongoDB -aws ec2 detach-volume --volume-id ${volume_id} - -until aws ec2 attach-volume --volume-id ${volume_id} --instance-id $${INSTANCE_ID} --device /dev/sdf; do - sleep 10 - echo "Trying to attach volume" -done - -# wait for EBS volume to attach -DATA_STATE="unknown" -until [[ $${DATA_STATE} == "attached" ]]; do - DATA_STATE=$(aws ec2 describe-volumes \ - --filters \ - Name=attachment.instance-id,Values=$${INSTANCE_ID} \ - Name=attachment.device,Values=/dev/sdf \ - --query Volumes[].Attachments[].State \ - --output text) - echo 'waiting for volume...' - sleep 5 -done - -echo 'EBS volume attached!' - # Change source-destination checking aws ec2 modify-instance-attribute --instance-id $${INSTANCE_ID} --source-dest-check "{\"Value\": false}" -sleep 5 -FILESYSTEM=$(file -s /dev/nvme1n1 | grep filesystem) - -if [[ $${#FILESYSTEM} -eq 0 ]]; then - mkfs -t ext4 /dev/nvme1n1 -fi - - tee /etc/yum.repos.d/mongodb-org-4.2.repo << EOF [mongodb-org-4.2] name=MongoDB Repository @@ -70,9 +43,10 @@ gpg --keyserver hkp://keyserver.ubuntu.com --recv-keys 7568D9BB55FF9E5287D586017 gpg --armor --export 7568D9BB55FF9E5287D586017AE645C0CF8E292A > key.tmp; rpm --import key.tmp; rm -f key.tmp yum -y install pritunl mongodb-org -mount /dev/nvme1n1 $${MONGODB_DATA_DIR} +# Mount EFS filesystem +mount -t efs -o tls ${efs_id}:/ $${MONGODB_DATA_DIR} chown -R mongod:mongod $${MONGODB_DATA_DIR} -echo "/dev/nvme1n1 $${MONGODB_DATA_DIR} ext4 defaults,nofail 0 2" >> /etc/fstab +echo "${efs_id}:/ $${MONGODB_DATA_DIR} efs _netdev,tls 0 0" >> /etc/fstab systemctl enable mongod pritunl pritunl set-mongodb 'mongodb://localhost:27017/pritunl' diff --git a/terraform/modules/aws-ec2-pritunl/variables.tf b/terraform/modules/aws-ec2-pritunl/variables.tf index b04d4997..e17e95f2 100644 --- a/terraform/modules/aws-ec2-pritunl/variables.tf +++ b/terraform/modules/aws-ec2-pritunl/variables.tf @@ -1,8 +1,6 @@ variable "vpc_id" {} variable "public_subnets" {} -variable "availability_zone" { - default = "us-east-1a" -} + variable "name" { default = "pritunl" } @@ -13,12 +11,29 @@ variable "instance_type" { default = "t3.small" } -variable "pritunl_sg_rules" { +variable "encrypted" { + default = true +} + +variable "kms_key_id" { + default = null +} +variable "ingress_with_source_security_group_id" { + type = list(object({ + protocol = string + from_port = string + to_port = string + security_groups = string + })) + + default = [] +} +variable "ingress_with_cidr_blocks" { type = list(object({ protocol = string from_port = string to_port = string - cidr_blocks = list(string) + cidr_blocks = string })) default = []