Ao usar o AWS re:Post, você concorda com os AWS re:Post Termos de uso

Copy snapshot from one account to another account in same region

0

Hi I need a copy snapshot from account 111AAA to account 222BBB in us-east-1 [ one account to another account] in boto3 could you please help me on this

  • please accept the answer if it was useful

feita há 7 meses492 visualizações
3 Respostas
1

Lambda is limited (15 minutes max), and this may not be enough for a backup copy. Please consider using AWS Backup and Backup policies

I made an article about it https://www.automat-it.com/multi-account-backup-copy-in-aws/

profile picture
ESPECIALISTA
respondido há 7 meses
profile picture
ESPECIALISTA
avaliado há 6 meses
1

Hi,

Why don't you just use the existing Cross-Account Snapshot Copy Policies: see https://docs.aws.amazon.com/ebs/latest/userguide/event-policy.html

They do exactly what you want with full automation.

Best,

Didier

profile pictureAWS
ESPECIALISTA
respondido há 7 meses
0

Question mentions snapshot but does not explicitly say EBS. Snapshots could also be created for several databases and below answer is for EBS.

import boto3
import os

def lambda_handler(event, context):
    # Get the account number from an environment variable
    target_account = os.getenv('TARGET_ACCOUNT')

    # Initialize the EC2 client
    ec2 = boto3.client('ec2')

    # Get the snapshot ID from the event input
    snapshot_id = event.get('snapshot_id')
    if not snapshot_id:
        print("Error: No snapshot ID provided in the event")
        return {
            'statusCode': 400,
            'body': 'Error: No snapshot ID provided in the event'
        }

    try:
        # Share the snapshot with the target account
        response = ec2.modify_snapshot_attribute(
            SnapshotId=snapshot_id,
            Attribute='createVolumePermission',
            OperationType='add',
            UserIds=[target_account]
        )

        print(f"Shared snapshot {snapshot_id} with account {target_account}")
        return {
            'statusCode': 200,
            'body': 'Snapshot shared successfully'
        }
    except Exception as e:
        print(f"Error sharing snapshot: {e}")
        return {
            'statusCode': 500,
            'body': f"Error sharing snapshot: {e}"
        }

Here's how the code works:

  1. The function first retrieves the target account number from an environment variable named TARGET_ACCOUNT. This assumes that you have set this environment variable in your Lambda function configuration.

  2. You'll need to replace 'your-snapshot-id' with the actual ID of the snapshot you want to share.

  3. The modify_snapshot_attribute method is used to share the snapshot with the target account. The Attribute parameter is set to 'createVolumePermission', which allows the target account to create volumes from the shared snapshot. The OperationType parameter is set to 'add' to grant the permission, and the UserIds parameter is a list containing the target account ID.

  4. If the operation is successful, the function returns a success response with a status code of 200 and a message indicating that the snapshot was shared successfully.

  5. If an exception occurs during the process, the function returns an error response with a status code of 500 and the error message.

Make sure to replace the 'your-snapshot-id' placeholder with the actual ID of the snapshot you want to share, and ensure that the TARGET_ACCOUNT environment variable is set correctly in your Lambda function configuration.

AWS
ESPECIALISTA
respondido há 6 meses

Você não está conectado. Fazer login para postar uma resposta.

Uma boa resposta responde claramente à pergunta, dá feedback construtivo e incentiva o crescimento profissional de quem perguntou.

Diretrizes para responder a perguntas