Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to attach function #30

Closed
faizanalhassan opened this issue Jan 2, 2022 · 9 comments · Fixed by #31
Closed

Unable to attach function #30

faizanalhassan opened this issue Jan 2, 2022 · 9 comments · Fixed by #31

Comments

@faizanalhassan
Copy link
Contributor

faizanalhassan commented Jan 2, 2022

Followed exact steps given in readme to run the code.

But got this error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\repos\chromeless\chromeless\chromeless.py", line 24, in attach
    self.codes[method.__name__] = inspect.getsource(
  File "C:\Users\faizan\AppData\Local\Programs\Python\Python39\lib\inspect.py", line 1024, in getsource
    lines, lnum = getsourcelines(object)
  File "C:\Users\faizan\AppData\Local\Programs\Python\Python39\lib\inspect.py", line 1006, in getsourcelines
    lines, lnum = findsource(object)
  File "C:\Users\faizan\AppData\Local\Programs\Python\Python39\lib\inspect.py", line 835, in findsource
    raise OSError('could not get source code')
OSError: could not get source code

I have not used sls earlier, just deployed to lambda directly, so maybe some steps are not written for devs who are unfamiliar with sls.

@umihico
Copy link
Owner

umihico commented Jan 3, 2022

@faizanalhassan

Thank you for reporting. I didn't know inspect.getsource can't work in interactive mode. I guess you can run if you save readme script on files. Could you try that?

#31 is not perfect solution, just telling people more friendly that you can't use this in interactive mode.

@faizanalhassan
Copy link
Contributor Author

faizanalhassan commented Jan 3, 2022

Thanks for your help. that error is gone.
However, the script is still not running. I am getting another error:

Function not found: arn:aws:lambda:us-west-2:<aws account id>:function:chromeless-server-prod

You can see the region its referring is us-west-2, but the command I ran was sls deploy --region us-east-2. Is there anything I am missing? Any env or updates in serverless.yml?

@faizanalhassan
Copy link
Contributor Author

My bad, there was another npm package named sls, which was doing trouble.
Uninstalled it.
Thanks again.

@faizanalhassan
Copy link
Contributor Author

faizanalhassan commented Jan 3, 2022

Lambda deployed successfully, but still getting same error:
arn:aws:lambda:us-west-2:<aws account id>:function:chromeless-server-prod
How do i tell Chromeless to target my region us-east-2?

@umihico
Copy link
Owner

umihico commented Jan 3, 2022

@faizanalhassan
I'm glad to hear that the former problems are gone.

What output will you get if you execute aws configure get region in shell?

First, you need to deploy by sls deploy --region us-east-2.
Second, setting environmental variable works like AWS_DEFAULT_REGION=us-west-2 python main.py
Or, hard-coding in script is also possible like below.

from chromeless import Chromeless
import boto3 # This
boto3.setup_default_session(region_name='us-east-2') # This

def get_title(self, url):
    self.get(url)
    return self.title

chrome = Chromeless()
chrome.attach(get_title)
print(chrome.get_title("https://example.com"))

If you don't mind setting us-east-2 as your default region so far, I'd recommend aws configure set region us-east-2 and above modifications are probably not necessary, but deployment.

@faizanalhassan
Copy link
Contributor Author

Yeah, actually I have multiple AWS profiles, so that might be causing this. I also set region_name in boto3, but the env way you shared is better AWS_DEFAULT_REGION, so will try that.
As always again thanks.

@faizanalhassan
Copy link
Contributor Author

faizanalhassan commented Jan 3, 2022

However, I suggest to accept aws key, secret and region in Chromeless.__init__, with default None, so the user can set through env, AWS local config, or explicitly pass to the constructor like in boto3. If you agree with this suggestion, I can do these updates and create a PR.

@umihico
Copy link
Owner

umihico commented Jan 3, 2022

Yes, I agree with your idea and PR is always welcome, thank you!

Instead of accepting credentials directly, I think __init__ should accept 'session', then session can take care of credentials or profile

from boto3.session import Session

session = Session(aws_access_key_id='<YOUR ACCESS KEY ID>',
                  aws_secret_access_key='<YOUR SECRET KEY>',
                  region_name='<REGION NAME>')
# or
session = Session(profile_name='<YOUR_PROFILE_NAME>')

@faizanalhassan
Copy link
Contributor Author

Ok doing with session object. Will update you in a day or two. Thanks for accepting my contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants