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
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"