Skip to content

Instantly share code, notes, and snippets.

@ravi-tejarockon
Last active November 3, 2023 19:50
Show Gist options
  • Save ravi-tejarockon/bbc98a3c9bb5509424f214ebef63552f to your computer and use it in GitHub Desktop.
Save ravi-tejarockon/bbc98a3c9bb5509424f214ebef63552f to your computer and use it in GitHub Desktop.
cloudfront
function handler(event) {
// Redirect from apex to subdomain (e.g. www).
var request = event.request;
if (request.headers.host) {
var host = request.headers.host.value;
if (host === '<APEX_DOMAIN_HERE>') {
return {
statusCode: 301,
statusDescription: 'Moved Permanently',
headers: {
'location': { "value": `<SUBDOMAIN_HERE>${request.uri}` }
}
};
}
}
return event.request;
}
import boto3
def lambda_handler(event, context):
# Specify the ID of the EC2 instance you want to create an AMI from
instance_id = 'your-instance-id'
# Specify a name for your AMI
ami_name = 'Backup-{}'.format(instance_id)
# Create an EC2 client
ec2_client = boto3.client('ec2')
# Create an AMI from the specified EC2 instance
try:
response = ec2_client.create_image(
InstanceId=instance_id,
Name=ami_name,
NoReboot=True # Set to True if you want to avoid instance reboot during AMI creation
)
ami_id = response['ImageId']
print(f"Successfully created AMI: {ami_id}")
return ami_id
except Exception as e:
print(f"Error: {e}")
raise e
import boto3
def lambda_handler(event, context):
ec2 = boto3.client('ec2')
# Get instances with specific tag
instances = ec2.describe_instances(Filters=[{'Name': 'tag:Backup', 'Values': ['True']}])['Reservations']
# Create snapshots for attached volumes
for reservation in instances:
for instance in reservation['Instances']:
for volume in instance['BlockDeviceMappings']:
volume_id = volume['Ebs']['VolumeId']
description = 'Backup of volume ' + volume_id + ' from instance ' + instance['InstanceId']
# Create snapshot
ec2.create_snapshot(VolumeId=volume_id, Description=description)
return {
'statusCode': 200,
'body': 'Backup process executed successfully.'
}
import boto3
# Replace 'your-bucket-name' with the actual S3 bucket name
bucket_name = 'your-bucket-name'
# Initialize the S3 client
s3 = boto3.client('s3')
try:
# Attempt to list objects in the bucket
response = s3.list_objects_v2(Bucket=bucket_name)
print("Successfully listed objects in the bucket.")
except Exception as e:
print("Error: ", e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment