Created
October 17, 2019 13:38
-
-
Save GusAntoniassi/26396dac3e1a58ded76f37a124ab2d3f to your computer and use it in GitHub Desktop.
[SECOMP 2019 Mandic] Exemplo de lambda function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
def lambda_handler(event, context): | |
region = 'us-east-2' | |
volumeId = 'vol-ALTERE-AQUI' | |
# Conectar-se à região | |
ec2 = boto3.client('ec2', region_name=region) | |
# Pegar o ID do volume | |
result = ec2.describe_volumes( Filters=[{'Name': 'volume-id', 'Values': [ volumeId ]}]) | |
if not result['Volumes']: | |
raise Exception('Nenhum volume encontrado com o ID {}'.format(volumeId)) | |
for volume in result['Volumes']: | |
print("Backing up %s in %s" % (volume['VolumeId'], volume['AvailabilityZone'])) | |
# Criar snapshot | |
result = ec2.create_snapshot(VolumeId=volume['VolumeId'],Description='Criado pelo lambda de testes do Workshop AWS da Mandic') | |
# Pegar o resource da snapshot para adicionar as tags | |
ec2resource = boto3.resource('ec2', region_name=region) | |
snapshot = ec2resource.Snapshot(result['SnapshotId']) | |
# Adicionar tags ao volume | |
snapshot.create_tags(Tags=[ | |
{ | |
'Key': 'Name', | |
'Value': 'Backup' | |
}, { | |
'Key': 'Type', | |
'Value': 'secomp' | |
} | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment