• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Archives

auto

EFS Tags Auto Assign Backup Tags

January 17, 2021

Here’s a script that scans all EFS systems in several AWS accounts and regions and randomly assigns backup tags to EFS systems that are missing backup tags. This is assuming EFS is using AWS Backup service using tags to apply backup policies. If there are no backup tags, an EFS gets assigned a randomly picked backup policy.

#!/bin/bash
# log file
output="test.log"
tmpfil="temp.txt"
# empty file
> $output
# set random array
arr[0]="efs-0000"
arr[1]="efs-0400"
arr[2]="efs-0800"
arr[3]="efs-1200"
arr[4]="efs-1600"
arr[5]="efs-2000"
rand=$[ $RANDOM %6 ]
backup=${arr[$rand]}
# set accounts and regions
declare -a account=("default" "one" "two" "three" "four" "five")
declare -a region=("us-east-1" "us-east-2" "us-west-1" "us-west-2")
for i in "${account[@]}"; do
  echo "===================" >> $output
  echo $i >> $output
  echo "===================" >> $output
  for j in "${region[@]}"; do
    echo $j >> $output
        aws efs describe-file-systems \
        --query "FileSystems[*].[FileSystemId,Tags[?Key=='aws-backup']|[0].Value]" \
        --profile $i \
        --region $j \
        --output text > $tmpfil
        while read -r id tag; do
      if [[ $tag == "" ]]; then
        aws efs tag-resource \
        --resource-id $id \
        --tags Key="aws-backup",Value=${arr[$rand]} \
        --profile $i \
        --region $j >> $output
        echo "Added backup tag $backup to $id" >> $output
      elif [[ $tag == "no-backup" ]]; then
        echo "Backup tag is already set to no-backup on $id." >> $output
      else
        echo "No backup tag changes applied to $id." >> $output
      fi
    done < $tmpfil
  done
done
rm $tmpfil

#!/bin/bash # log file output="test.log" tmpfil="temp.txt" # empty file > $output # set random array arr[0]="efs-0000" arr[1]="efs-0400" arr[2]="efs-0800" arr[3]="efs-1200" arr[4]="efs-1600" arr[5]="efs-2000" rand=$[ $RANDOM %6 ] backup=${arr[$rand]} # set accounts and regions declare -a account=("default" "one" "two" "three" "four" "five") declare -a region=("us-east-1" "us-east-2" "us-west-1" "us-west-2") for i in "${account[@]}"; do echo "===================" >> $output echo $i >> $output echo "===================" >> $output for j in "${region[@]}"; do echo $j >> $output aws efs describe-file-systems \ --query "FileSystems[*].[FileSystemId,Tags[?Key=='aws-backup']|[0].Value]" \ --profile $i \ --region $j \ --output text > $tmpfil while read -r id tag; do if [[ $tag == "" ]]; then aws efs tag-resource \ --resource-id $id \ --tags Key="aws-backup",Value=${arr[$rand]} \ --profile $i \ --region $j >> $output echo "Added backup tag $backup to $id" >> $output elif [[ $tag == "no-backup" ]]; then echo "Backup tag is already set to no-backup on $id." >> $output else echo "No backup tag changes applied to $id." >> $output fi done < $tmpfil done done rm $tmpfil

Filed Under: Cloud Tagged With: auto, aws, backup, efs, set, tags

AWS EFS Userdata

December 25, 2018

If you want to auto mount the EFS volumes when the instance is created, add the following script to userdata:

#cloud-config
repo_update: true
repo_upgrade: all
 
packages:
- amazon-efs-utils
 
runcmd:
- file_system_id_01=fs-12345678
- file_system_id_02=fs-34593405
- efs_directory_01=/mnt/efs
- efs_directory_02=/mnt/efs2
 
- mkdir -p ${efs_directory_01}
- mkdir -p ${efs_directory_02}
 
- echo "${file_system_id_01}:/ ${efs_directory_01} efs tls,_netdev" >> /etc/fstab
- echo "${file_system_id_02}:/ ${efs_directory_02} efs tls,_netdev" >> /etc/fstab
 
- mount -a -t efs defaults

#cloud-config repo_update: true repo_upgrade: all packages: - amazon-efs-utils runcmd: - file_system_id_01=fs-12345678 - file_system_id_02=fs-34593405 - efs_directory_01=/mnt/efs - efs_directory_02=/mnt/efs2 - mkdir -p ${efs_directory_01} - mkdir -p ${efs_directory_02} - echo "${file_system_id_01}:/ ${efs_directory_01} efs tls,_netdev" >> /etc/fstab - echo "${file_system_id_02}:/ ${efs_directory_02} efs tls,_netdev" >> /etc/fstab - mount -a -t efs defaults

Just add more if you have more mounts.

Filed Under: Cloud Tagged With: auto, aws cli, efs, mount

  • Home
  • About
  • Archives

Copyright © 2023