2017-09-09 75 views
-1

这是一个非常奇怪的问题,我陷入了,真的很感激,如果有人可以提供一些方向。我试图从web_token.py模块访问request_url的值。Aws lambda函数失败 - Python

当我只尝试通过pycharm分别运行web_token.py并打印request_url时,它工作正常并生成url。我压缩这些文件并将其上传到lambda函数,但是当测试它时,我得到一个错误“无法导入模块'retrieve_accounts':没有名为boto.sts的模块”。我甚至尝试将web_token.py的代码放入retrieve_accounts.py中,但得到相同的错误。我确信我缺少一些非常基本的东西,看起来boto.sts在运行python脚本时没有被识别。有人可以请提供一些指导。谢谢!

retrieve_accounts.py

import boto3 
import web_token 

def get_account(event, context): 

client = boto3.client('dynamodb') 
NameID = "[email protected]" 
ManagerEmail = "[email protected]" 
response = client.scan(
    TableName='Sandbox-Users', 
    ScanFilter={ 
     'NameID': { 
      'AttributeValueList': [ 
       { 
        'S': NameID, 
       }, 
      ], 
      'ComparisonOperator': 'EQ' 
     } 
    } 
) 
if response["Count"] > 0: 
    client = boto3.client('dynamodb') 
    response = client.get_item(
     Key={ 
      'NameID': { 
       'S': NameID, 
      }, 
      'ManagerEmail': { 
       'S': ManagerEmail, 
      }, 
     }, 
     TableName='Sandbox-Users', 
    ) 
    return web_token.request_url ----------->here 

else: 
    response = client.put_item(
     Item={ 
      'NameID': { 
       'S': NameID, 
      }, 
      'ManagerEmail': { 
       'S': ManagerEmail, 
      } 
     }, 
     TableName='Sandbox-Users' 
    ) 
    return "Create Account" 

web_token.py

import httplib 
import urllib, json 
from boto.sts import STSConnection -------->Error here 

sts_connection = STSConnection() 
assumed_role_object = sts_connection.assume_role(
role_arn="arn:aws:iam::454084028794:role/AMPSandboxRole", 
role_session_name="AssumeRoleSession" 
) 

# Step 3: Format resulting temporary credentials into JSON 

json_string_with_temp_credentials = '{' 
json_string_with_temp_credentials += '"sessionId":"' + 
assumed_role_object.credentials.access_key + '",' 
json_string_with_temp_credentials += '"sessionKey":"' + 
assumed_role_object.credentials.secret_key + '",' 
json_string_with_temp_credentials += '"sessionToken":"' + 
assumed_role_object.credentials.session_token + '"' 
json_string_with_temp_credentials += '}' 

# Step 4. Make request to AWS federation endpoint to get sign-in token. 
Construct the parameter string with the sign-in action request, a 12-hour session duration, and the JSON 
    document with temporary credentials as parameters. 

request_parameters = "?Action=getSigninToken" 
    request_parameters += "&SessionDuration=43200" 
    request_parameters += "&Session=" + 
    urllib.quote_plus(json_string_with_temp_credentials) 
    request_url = "/federation" + request_parameters 
    conn = httplib.HTTPSConnection("signin.aws.amazon.com") 
    conn.request("GET", request_url) 
    r = conn.getresponse() 
    # Returns a JSON document with a single element named SigninToken. 
    signin_token = json.loads(r.read()) 
    request_parameters = "?Action=login" 
    request_parameters += "&Issuer=sandbox.com" 
    request_parameters += "&Destination=" + 
    urllib.quote_plus("https://console.aws.amazon.com/") 
    request_parameters += "&SigninToken=" + signin_token["SigninToken"] 
    request_url = "https://signin.aws.amazon.com/federation" + 
    request_parameters 
+3

你为什么在那里混合boto和boto3?坚持与boto3像'import boto3; client = boto3.client('sts')' – ydaetskcoR

回答

1

AWS LAMBDA Python的环境包括boto3(和botocore)。他们不包括老boto(boto3的先驱),因此导入失败。

您可能会在您的上传中包含boto,但如果您可以避免将boto和boto3混合,建议不要这样做。使用一个或另一个,最好是boto3。

+0

非常感谢你的指导,我很感激。 –