Skip to content

Instantly share code, notes, and snippets.

@eduardcloud
Created September 19, 2017 10:14
Show Gist options
  • Select an option

  • Save eduardcloud/75a3a0b04e35b161567301caa8d561b0 to your computer and use it in GitHub Desktop.

Select an option

Save eduardcloud/75a3a0b04e35b161567301caa8d561b0 to your computer and use it in GitHub Desktop.

Revisions

  1. eduardcloud created this gist Sep 19, 2017.
    24 changes: 24 additions & 0 deletions LambdaEfsBackup.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    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)