Opsgenie python sdk integration with AWS Lambda failed to create alert
Asked by Ahmad Al-Sajid:

Hello everyone,  

I am trying to create alerts from AWS Lambda using [python SDK](https://docs.opsgenie.com/docs/opsgenie-python-api-v2-1). When I execute the script from my PC or AWS EC2, it works fine and takes \~10 seconds (business logic + fetching data from others resources) to create all the alerts in Opsgenie. Now, when I try to execute it from AWS Lambda, the \`create_alert\` API times out. Below is the code that works in EC2 scripts but not in AWS Lambda.

```
try:
    body = CreateAlertPayload(
        message=message,
        alias=message,
        responders=[{
            'name': 'My Team',
            'type': 'team'
        }],
        tags=tags,
        priority=priority
    )
    create_response = alert_api.create_alert(create_alert_payload=body)
    print(f'offline device: {device}, request: {create_response.request_id}')
except ApiException as err:
    print(f"Exception when calling AlertApi->create_alert: {err}\n")
```

Is there something that I missed?


Answers:
author: Samir
body: Hi [@Ahmad Al-Sajid](/t5/user/viewprofilepage/user-id/3998380) ,

This seems to be more of a lambda specific issue. The one thing I can think of, is you need to have installed the opsgenie-sdk. So you will need to install the opsgenie-sdk locally (i.e. **pip install opsgenie-sdk**), and upload that to the lambda function dependencies so that the lambda function can use the SDK.

If you've done that already, are *any* of the opsgenie-sdk commands working from python?

Thanks,

Samir


author: Reham Adel
body: Hi [@Ahmad Al-Sajid](/t5/user/viewprofilepage/user-id/3998380)

could you please tell how did you solve this problem ?

comments: author: Ahmad Al-Sajid
body: Hi [@Reham Adel](/t5/user/viewprofilepage/user-id/4652713), we had to turn off SSL verification to make it work then. Still, it is not recommended.


author: Reham Adel
body: How did you turn off SSL verification?


author: Ahmad Al-Sajid
body: [https://docs.opsgenie.com/docs/opsgenie-python-sdk-configurations#:\~:text=Retry%20switch-,verify_ssl,-True](https://docs.opsgenie.com/docs/opsgenie-python-sdk-configurations#:~:text=Retry%20switch-,verify_ssl,-True)  

Set the value of \`verify_ssl\` to \`False\`


author: Reham Adel
body: thank you for your support


author: Reham Adel
body: Hi [@Ahmad Al-Sajid](/t5/user/viewprofilepage/user-id/3998380)  
i try to run code to create alarm in aws lambda but i get that error

"errorMessage": "You are not authorized to perform this action: (401)\\nReason: Unauthorized: apiKey is invalid or integration is disabled\\nHTTP response headers: HTTPHeaderDict({'Content-Type': 'application/json', 'Content-Length': '98', 'Connection': 'keep-alive', 'Date': 'Sun, 05 Dec 2021 21:11:20 GMT', 'X-Request-Id': 'b67d7a41-5be4-4eb3-b869-945e1330a85e', 'X-Response-Time': '0.0', 'Og-Apigw-Request-Id': 'fa4076ab-2eef-4c40-8ea9-88b7240ec1ac', 'X-Envoy-Upstream-Service-Time': '129', 'Expect-Ct': 'report-uri=\\"[https://web-security-reports.services.atlassian.com/expect-ct-report/opsgenie-edge-proxy\\](https://web-security-reports.services.atlassian.com/expect-ct-report/opsgenie-edge-proxy\)", max-age=86400', 'Strict-Transport-Security': 'max-age=63072000; preload', 'X-Content-Type-Options': 'nosniff', 'X-Xss-Protection': '1; mode=block', 'Atl-Traceid': 'a7cbcab36b582704', 'Report-To': '{\\"group\\": \\"endpoint-1\\", \\"max_age\\": 600, \\"endpoints\\": \[{\\"url\\": \\"[https://dj9s4kmieytgz.cloudfront.net\\](https://dj9s4kmieytgz.cloudfront.net\)"}\], \\"include_subdomains\\": true}', 'Nel': '{\\"report_to\\": \\"endpoint-1\\", \\"max_age\\": 600, \\"include_subdomains\\": true, \\"failure_fraction\\": 0.001}', 'Server': 'globaledge-envoy', 'X-Cache': 'Error from cloudfront', 'Via': '1.1 591fc133cda27edbedf7edb3f0231464.cloudfront.net (CloudFront)', 'X-Amz-Cf-Pop': 'LHR61-C1', 'X-Amz-Cf-Id': '2s6GACkFGfI4DZHtQNXxTL_VAz3o4mjnXhJzji4RUL9LuORAIsO6AA=='})\\nHTTP response body: {\\"message\\": \\"Could not authenticate\\", \\"took\\": 0.0, \\"requestId\\": \\"b67d7a41-5be4-4eb3-b869-945e1330a85e\\", \\"url\\": \\"[https://api.opsgenie.com/v2/alerts\\](https://api.opsgenie.com/v2/alerts\)"}\\n",

the part of authorization in the code

configuration = opsgenie_sdk.Configuration()  
# Configure API key authorization: GenieKey  
#configuration.api_key\['Authorization'\] = 'xxxxx'  
configuration.username = 'xxxxx'  
configuration.password = 'xxxxx'  
configuration.verify_ssl = 'False'  
conf = opsgenie_sdk.configuration.Configuration()  
api_instance = opsgenie_sdk.AccountApi(opsgenie_sdk.ApiClient(configuration))  
api_client = opsgenie_sdk.api_client.ApiClient(configuration=conf)  
alert_api = opsgenie_sdk.AlertApi(api_client=api_client)

could you help me please?  


author: Reham Adel

author: Ahmad Al-Sajid
body: Hi [@Samir](/t5/user/viewprofilepage/user-id/2168284),  
Thanks for your feedback. Strange thing is, it sometimes can connect to Opsgenie and create tickets, but, most of the times failing to connect and exits with the following error

```
[ERROR] RetryError: RetryError[<Future at 0x7fc1a71bf610 state=finished raised NewConnectionError>]
Traceback (most recent call last):
??File "/var/task/lambda_function.py", line 108, in lambda_handler
????create_response = alert_api.create_alert(create_alert_payload=body)
??File "/opt/python/opsgenie_sdk/api/alert/__init__.py", line 1136, in create_alert
????(data) = self.create_alert_with_http_info(create_alert_payload, **kwargs) # noqa: E501
??File "/opt/python/opsgenie_sdk/api/alert/__init__.py", line 1197, in create_alert_with_http_info
????return self.api_client.call_api(
??File "/opt/python/opsgenie_sdk/api_client.py", line 423, in call_api
????return self.__call_api(resource_path, method,
??File "/opt/python/opsgenie_sdk/api_client.py", line 218, in __call_api
????raise exception
??File "/opt/python/opsgenie_sdk/api_client.py", line 196, in __call_api
????response_data = self.retrying(fn=self.request, method=method, url=url,
??File "/opt/python/tenacity/__init__.py", line 430, in __call__
????do = self.iter(retry_state=retry_state)
??File "/opt/python/tenacity/__init__.py", line 379, in iter
????six.raise_from(retry_exc, fut.exception())
??File "<string>", line 3, in raise_from?
```

<br />

<br />

As it can connect to Opsgenie sometimes, I don't think it's an issue with the Lambda or the package installed in lambda. BTW, I'm using**opsgenie-sdk==2.1.4**   

Thanks.


Tags: cloud

lambda

python