How to use Lambda to automatically download files uploaded to S3 to EC2 (Windows)

0

I want to use Lambda to automatically transfer files uploaded to S3 to EC2 (Windows). To do this, I have configured the following: Set s3:ObjectCreated:* as a trigger to invoke Lambda (Python). Start windows server 2022 and set IAM role to AdministratorAccess. Set IAM role of Lambda to AdministratorAccess.

These were implemented. However, when I execute the following in Python 3.7 and store the file in S3, it starts without error, but the file is not transferred to EC2.

Please give me some advice.

import logging
import boto3

LOGGER = logging.getLogger()
LOGGER.setLevel(logging.INFO)
ec2 = boto3.client('ec2')
ssm = boto3.client('ssm')
INSTANCE_ID = 'i-***'

def lambda_handler(event, context):
    try:
		#Get EC2 State
        ec2_resp = ec2.describe_instances(InstanceIds=[INSTANCE_ID]),
        ec2_state = ec2_resp['Reservations'][0]['Instances'][0]['State']['Name']
        if not ec2_state == "running":
            LOGGER.info('No EC2 is running')
            return "END"
		# RunPowerShellScript
        command = r"aws s3 sync s3://***/ C:/Users/Administrator/Desktop/***/"
        ssm.send_command(
            InstanceIds=[INSTANCE_ID],
            DocumentName="AWS-RunPowerShellScript",
            Parameters={
                "commands":[command],
                "executionTimeout":["3600"]
            },
        )
    
    except Exception as error:
        LOGGER.error(error)
        raise error
hiro
preguntada hace 9 meses1864 visualizaciones
3 Respuestas
1

Why are you using Lambda in this case? I would send the S3 notification to SQS and have a process polling the queue on the EC2 instance. When it receives a new message, it download the file and process it.

profile pictureAWS
EXPERTO
Uri
respondido hace 9 meses
profile pictureAWS
EXPERTO
iBehr
revisado hace 9 meses
  • Thank you for your response. I was thinking of using Lambda to perform the above action and then continue to operate Window Server with Run Command by lambda.

0
  • Mountpoint only support Linux today. You could use WSL or a container to run on Windows.

0

I implemented similar solution in the past using S3 --> EventBridge --> Lambda function which invoked a SSM command

Example:

    bucket_name = event['bucket_name']
    s3_object = event['object_key']
    instance_id = event['instance_id']
    destination_path = event['destination']

    command_string = f"Copy-S3Object -BucketName {bucket_name} -Key {s3_object} -LocalFile {destination_path}\{s3_object}"
    cmd_id = ssm_client.send_command(
         InstanceIds=[instance_id],
         DocumentName="AWS-RunPowerShellScript",
         Parameters={"commands": [command_string]},
         CloudWatchOutputConfig={"CloudWatchOutputEnabled": True}
     )

You will also need to verify that the EC2 instance has the proper IAM permissions assigned to access the S3 bucket and objects as well as the proper permissions for SSM management.

AWS
respondido hace 9 meses

No has iniciado sesión. Iniciar sesión para publicar una respuesta.

Una buena respuesta responde claramente a la pregunta, proporciona comentarios constructivos y fomenta el crecimiento profesional en la persona que hace la pregunta.

Pautas para responder preguntas