Aws lambda函数失败 - Python

时间:2017-09-09 08:28:09

标签: python amazon-web-services boto boto3

这是一个非常奇怪的问题,我已经陷入其中,如果有人可以提供一些方向,我将非常感激。我试图从web_token.py模块访问request_url的值。

当我只尝试在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 = "testname@org.com"
ManagerEmail = "test1@eorg.com"
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

1 个答案:

答案 0 :(得分:1)

AWS Lambda Python环境包括boto3(和botocore)。它们不包括较旧的boto(boto3的前身),因此导入失败。

您可能会在上传中加入boto,但如果可以避免,则不建议将boto和boto3混合使用。使用其中一个,最好是boto3。