Having error when I am trying to execute the python file in Ec2 (Windows) instance using lambda trigger

0

I am attempting to execute a Python file in an EC2 (Windows) instance through a lambda trigger function when a file is uploaded to an S3 bucket.

import boto3
import subprocess

def lambda_handler(event, context):
    instance_id = 'id-of-instance'
    
    directory = 'C:/Users/Administrator/Desktop/Automation'
    python_file = 'first.py'
    
    command = f'cd {directory} && python {python_file}'
    ec2 = boto3.client('ec2')

    response = ec2.send_command(
        InstanceIds=[instance_id],
        DocumentName='AWS-RunShellScript',
        Parameters={'commands': [command]}
    )

    return {
        'statusCode': 200,
        'body': response
    }

But it throws a error that is

Response
{
  "errorMessage": "'EC2' object has no attribute 'send_command'",
  "errorType": "AttributeError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 19, in lambda_handler\n    response = ec2.send_command(\n",
    "  File \"/var/runtime/botocore/client.py\", line 877, in __getattr__\n    f\"'{self.__class__.__name__}' object has no attribute '{item}'\"\n"
  ]
}

Function Logs
START RequestId: 9ac5d954-a371-4649-b88e-6a5971cbc2d8 Version: $LATEST
[ERROR] AttributeError: 'EC2' object has no attribute 'send_command'
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 19, in lambda_handler
    response = ec2.send_command(
  File "/var/runtime/botocore/client.py", line 877, in __getattr__
    f"'{self.__class__.__name__}' object has no attribute '{item}'"
END RequestId: 9ac5d954-a371-4649-b88e-6a5971cbc2d8
REPORT RequestId: 9ac5d954-a371-4649-b88e-6a5971cbc2d8	Duration: 2738.79 ms	Billed Duration: 2739 ms	Memory Size: 128 MB	Max Memory Used: 87 MB	Init Duration: 333.69 ms

Can anyone help me to resolve this issue

질문됨 8달 전330회 조회
1개 답변
2

Hi Vithushan,

send-command comes under SSM not EC2, you'd need to create boto3 client object for SSM.

Please refer SSM Send Command CLI Synopsis for more details.

Hope this helps.

Comment here if you have additional questions, happy to help.

Abhishek

profile pictureAWS
전문가
답변함 8달 전
profile pictureAWS
전문가
검토됨 8달 전
  • Do you have any additional questions?

  • After Creating the SSM. I am facing this issue { "errorMessage": "Could not connect to the endpoint URL: "https://ssm.us-west-2b.amazonaws.com/"", "errorType": "EndpointConnectionError", "stackTrace": [ " File "/var/task/lambda_function.py", line 60, in lambda_handler\n Parameters={'commands': [command]}\n", " File "/var/runtime/botocore/client.py", line 530, in _api_call\n return self._make_api_call(operation_name, kwargs)\n", " File "/var/runtime/botocore/client.py", line 944, in _make_api_call\n operation_model, request_dict, request_context\n", " File "/var/runtime/botocore/client.py", line 966, in _make_request\n return self._endpoint.make_request(operation_model, request_dict)\n", " File "/var/runtime/botocore/endpoint.py", line 119, in make_request\n return self._send_request(request_dict, operation_model)\n", " File "/var/runtime/botocore/endpoint.py", line 207, in _send_request\n exception,\n", " File "/var/runtime/botocore/endpoint.py", line 361, in _needs_retry\n request_dict=request_dict,\n", " File "/var/runtime/botocore/hooks.py", line 412, in emit\n return self._emitter.emit(aliased_event_name, **kwargs)\n", " File "/var/runtime/botocore/hooks.py", line 256, in emit\n return self._emit(event_name, kwargs)\n", " File "/var/runtime/botocore/hooks.py", line 239, in _emit\n response = handler(**kwargs)\n", " File "/var/runtime/botocore/retryhandler.py"

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

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

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

관련 콘텐츠