AWS re:Post을(를) 사용하면 다음에 동의하게 됩니다. AWS re:Post 이용 약관

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

질문됨 6달 전490회 조회
3개 답변
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
전문가
답변함 6달 전
profile picture
전문가
검토됨 6달 전
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
전문가
답변함 6달 전
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
전문가
답변함 6달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠