Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Backup EFS file-system to S3 with lambda function
import boto3
import time
region = 'eu-west-1'
user_data_script = """#!/bin/bash
instanceid=$(curl http://169.254.169.254/latest/meta-data/instance-id)
cd /
mkdir moodledata
mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 fs-xxxxxxxxxxc.efs.eu-west-1.amazonaws.com:/ moodledata
tar czf mooodledata-backup-$(date +%d-%m-%Y_%H-%M).tar.gz /moodledata
aws s3 mv mooodledata-backup-*.tar.gz s3://xxxxxxxxx/
aws ec2 terminate-instances --instance-ids $instanceid --region eu-west-1 """
# Amazon Linux (ami-ebd02392)
def lambda_handler(event, context):
ec2 = boto3.client('ec2', region_name=region)
new_instance = ec2.run_instances(
ImageId='ami-ebd02392',
MinCount=1,
MaxCount=1,
KeyName='xxxxx-key',
InstanceType='t2.micro',
SecurityGroups=['default'],
IamInstanceProfile={'Name':'EFSBackupRole'},
UserData=user_data_script)
@genedragon

This comment has been minimized.

Copy link

@genedragon genedragon commented Jan 31, 2020

In case you want to run this in reverse (S3 to EFS), here's the code. Simply trigger on any object put in the S3 bucket.

import boto3
import time
region = 'us-east-1'
user_data_script1  = """#!/bin/bash 
instanceid=$(curl http://169.254.169.254/latest/meta-data/instance-id) 
cd /
mkdir mountDir
mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 fs-xxxxxxxx.efs.us-east-1.amazonaws.com:/ mountDir
aws s3 cp s3://someBucket-us-east-1/@ mountDir/@
aws ec2 terminate-instances --instance-ids $instanceid --region """ + region

# Amazon Linux (ami-062f7200baf2fa504)
def lambda_handler(event, context):
    for record in event['Records']:
        user_data_script2 = user_data_script1.replace('@', record['s3']['object']['key'])
    print(user_data_script2)
    
    ec2 = boto3.client('ec2', region_name=region)
    new_instance = ec2.run_instances(
                        ImageId='ami-062f7200baf2fa504',
                        MinCount=1,
                        MaxCount=1,
                        InstanceType='t2.micro',
                        SecurityGroups=['default'],
                        IamInstanceProfile={'Name':'EFS-client-FULL'},
                        UserData=user_data_script2)
@EduardCloud

This comment has been minimized.

Copy link
Owner Author

@EduardCloud EduardCloud commented Feb 1, 2020

Great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment