diff --git a/README.md b/README.md index 0fa6b7d..4720a53 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,24 @@ -## Attach EFS volume to Multiple EC2 instances +## Attach EFS to Multiple EC2 Instances using Terraform +Attach the AWS EFS filesystem to multiple AWS EC2 instances running on different AZs. To automate the whole process from creating an EFS filesystem to attaching it to the EC2 instances, we will use Terraform. + + +### Following are the steps we will follow to achieve our goal: + +1. Create an AWS VPC with two public subnets on two different AZs. + +2. Create two Security Groups. one is for EC2 instances which will allow inbound SSH traffic on port 22, and another one is for EFS mount targets which will allow inbound traffic on port 2049 only from the EC2 instances security group. And both security groups will allow outbound traffic to any port from anywhere. + +3. Create an EFS file system. + +4. Configure EFS mount targets along with the security group created for EFS mount targets. + +5. Generate a custom script that will help us mount EFS on EC2 instances. + +6. Create AWS key pair so that we can SSH into the EC2 instances. + +7. Deploy two EC2 instances on different subnets created on different AZs. While providing the EC2 instances execute the custom script we created for mounting EFS using terraform remote-exec provisioners. + + \ No newline at end of file diff --git a/efs_mount.sh b/efs_mount.sh new file mode 100644 index 0000000..030edaa --- /dev/null +++ b/efs_mount.sh @@ -0,0 +1,8 @@ +#! /bin/bash +sudo yum update -y +sudo mkdir -p content/test/ +sudo yum -y install amazon-efs-utils +sudo su -c "echo 'fs-0c4c5164674de43ca:/ content/test/ efs _netdev,tls 0 0' >> /etc/fstab" +sudo mount content/test/ +df -k + diff --git a/main.tf b/main.tf index 235d51f..efcee6c 100644 --- a/main.tf +++ b/main.tf @@ -99,6 +99,7 @@ resource "aws_efs_file_system" "file_system_1" { } } +################## Create EFS mount targets ################ resource "aws_efs_mount_target" "mount_targets" { count = 2 file_system_id = aws_efs_file_system.file_system_1.id @@ -106,6 +107,21 @@ resource "aws_efs_mount_target" "mount_targets" { security_groups = [aws_security_group.efs_sg.id] } +################## Generating Script for Mounting EFS ################## +resource "null_resource" "generate_efs_mount_script" { + + provisioner "local-exec" { + command = templatefile("efs_mount.tpl", { + efs_mount_point = var.efs_mount_point + file_system_id = local.file_system_id + }) + interpreter = [ + "bash", + "-c" + ] + } +} + ################## SSH key generation ################## resource "tls_private_key" "ssh" { algorithm = "RSA" @@ -164,21 +180,6 @@ resource "aws_instance" "public_hosts" { } } -################## Generating Script for Mounting EFS ################## -resource "null_resource" "generate_efs_mount_script" { - - provisioner "local-exec" { - command = templatefile("efs_mount.tpl", { - efs_mount_point = var.efs_mount_point - file_system_id = local.file_system_id - }) - interpreter = [ - "bash", - "-c" - ] - } -} - ################## Clean Up Existing Script ################## resource "null_resource" "clean_up" {