Here's a Python program that takes user input for creating EBS snapshots, initializing volumes, and changing encryption keys for EBS volumes using the AWS SDK (Boto3).
import boto3
# Initialize AWS clients
ec2 = boto3.client('ec2')
def create_snapshot():
# Take user input for the volume ID and description
volume_id = input("Enter the EBS volume ID for snapshot creation: ")
description = input("Enter a description for the snapshot: ")
# Create an EBS snapshot
snapshot = ec2.create_snapshot(
VolumeId=volume_id,
Description=description
)
print(f"Snapshot {snapshot['SnapshotId']} created.")
def initialize_volume():
# Take user input for the volume ID and description
volume_id = input("Enter the EBS volume ID for initialization: ")
description = input("Enter a description for the volume initialization: ")
# Initialize the EBS volume from a snapshot
response = ec2.create_volume(
AvailabilityZone='us-east-1a', # Replace with your desired availability zone
SnapshotId='your-snapshot-id', # Replace with the snapshot ID to initialize from
Description=description
)
print(f"Volume {response['VolumeId']} initialized from snapshot.")
def change_encryption_key():
# Take user input for the volume ID and new KMS encryption key ARN
volume_id = input("Enter the EBS volume ID for encryption key change: ")
kms_key_arn = input("Enter the new KMS encryption key ARN: ")
# Modify the volume's encryption key
ec2.modify_volume(
VolumeId=volume_id,
KmsKeyId=kms_key_arn
)
print(f"Encryption key for volume {volume_id} changed to {kms_key_arn}.")
if __name__ == '__main__':
while True:
print("EBS Volume Management Menu:")
print("1. Create Snapshot")
print("2. Initialize Volume from Snapshot")
print("3. Change Encryption Key")
print("4. Exit")
choice = input("Select an option (1/2/3/4): ")
if choice == '1':
create_snapshot()
elif choice == '2':
initialize_volume()
elif choice == '3':
change_encryption_key()
elif choice == '4':
break
else:
print("Invalid choice. Please select a valid option.")
Before running this program, make sure to configure your AWS credentials, region, and customize the AvailabilityZone in the initialize_volume function. You'll also need to replace your-snapshot-id
with the actual snapshot ID you want to use for volume initialization.
This program provides a simple command-line menu for creating snapshots, initializing volumes from snapshots, and changing encryption keys for EBS volumes. It demonstrates how to take user input and use Boto3 to interact with AWS EBS resources.
To enable users to share their EBS snapshots with other AWS accounts and ensure that the shared snapshots are copyable, you can use the AWS SDK for JavaScript (AWS Amplify) to manage permissions and sharing settings. Here’s a JavaScript code snippet to achieve this:``
Nov 15, 2023Creating a shell script that logs the execution of health check automation steps and sends notifications in case of issues can be achieved using the logger utility for logging and, for notifications, you can use email, text messages, or any other notification service of your choice. Below is a simple shell script that logs execution and sends notifications via email using the mail command:``
Nov 15, 2023To develop a Python script that displays a menu for users to select a new encryption key from a list of available keys, you can use the AWS SDK for Python (Boto3) to retrieve the list of keys dynamically. Below is an example script that demonstrates this:
Nov 15, 2023Creating a custom Amazon Machine Image (AMI) from an existing EC2 instance can be done through the AWS Management Console, AWS CLI, or AWS SDKs, but you can also automate this process using a shell script. Below is a simple shell script that assists in creating a custom AMI from an existing EC2 instance using the AWS CLI:
Nov 15, 2023or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up