Skip to content

Commit

Permalink
EFS script generated
Browse files Browse the repository at this point in the history
  • Loading branch information
shamimice03 committed Feb 25, 2023
1 parent 3e00e6c commit e664237
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 17 deletions.
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

<!-- ## Attach EFS volume to Multiple EC2 instances
```
#! /bin/bash
Expand All @@ -19,4 +39,4 @@ This appears to be an entry in the /etc/fstab file on a Linux system.
The entry specifies that the EFS (Elastic File System) should be mounted at a specific mount point (specified by "efs-mount-point"), with the file system ID being "file-system-id". The options specified for the mount include "_netdev" (which indicates that the filesystem is a network device and should not be mounted until the network is available), "tls" (which enables Transport Layer Security for data in transit), and "iam" (which enables the use of AWS Identity and Access Management (IAM) credentials for authentication).
The final "0 0" specifies the dump and file system check order options, respectively. A "0" for these options indicates that they should be skipped.
```
``` -->
8 changes: 8 additions & 0 deletions efs_mount.sh
Original file line number Diff line number Diff line change
@@ -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

31 changes: 16 additions & 15 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,29 @@ 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
subnet_id = local.public_subnets[count.index]
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"
Expand Down Expand Up @@ -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" {

Expand Down

0 comments on commit e664237

Please sign in to comment.