【问题标题】:Create API key in AWS API Gateway from AWS Lambda using boto3使用 boto3 从 AWS Lambda 在 AWS API Gateway 中创建 API 密钥
【发布时间】:2017-01-22 03:25:33
【问题描述】:

我正在使用 AWS Lambda 函数来使用 Boto3 创建 API 密钥。

用以下方法在本地测试成功:

import boto3

client = boto3.client('apigateway')

response = client.create_api_key(
    name='test_user_from_boto',
    description='This is the description',
    enabled=True,
    generateDistinctId=True,
    value='',
    stageKeys=[{
        'restApiId':'aaa',
        'stageName':'beta'
    }]
)

将字典返回为expected 没有问题。返回字典包含一个 value 键,它具有生成的 api 键值,这就是我所追求的。

在 AWS Lambda 中执行类似操作时,返回字典不包含 value 键。

这是我的 Lambda 处理函数。

import boto3


api_id = 'zzz'
plan_id_map = {
    'trial': 'aaa', 
    'basic': 'bbb', 
    'professional': 'ccc'
}

def handler(event, context):
    user_name = event['user_name']
    stage = event['stage']
    plan = event['plan']

    client = boto3.client('apigateway')
    api_key_response = client.create_api_key(
        name=user_name, 
        description='', 
        enabled=True, 
        # generateDistinctId=True, # including this argument throws an error
        # value='', # including this argument throws an error
        stageKeys=[{
            'restApiId': api_id, 
            'stageName': stage
        }]
    )

    user_key_id = api_key_response['id']
    user_api_key = api_key_response['value'] # throws a key error here

    plan_response = client.create_usage_plan_key(
        usagePlanId=plan_id_map[plan],
        keyId=user_key_id,
        keyType='API_KEY')    

    return {
        'user_name': user_name,
        'user_key_id': user_key_id,
        'user_api_key': user_api_key
    }

打印api_key_response的结果如下:

{
    u'name': u'test_user_from_lambda', 
    'ResponseMetadata': {
        'HTTPStatusCode': 201, 
        'RequestId': 'b8298d38-7aec-11e6-8322-5bc341fc4b73', 
        'HTTPHeaders': {
            'x-amzn-requestid': 'b8298d38-7aec-11e6-8322-5bc341fc4b73',
            'date': 'Thu, 15 Sep 2016 02:33:00 GMT', 
            'content-length': '203', 
            'content-type': 'application/json'
        }
    },
    u'createdDate': datetime.datetime(2016, 9, 15, 2, 33, tzinfo=tzlocal()), 
    u'lastUpdatedDate': datetime.datetime(2016, 9, 15, 2, 33, tzinfo=tzlocal()), 
    u'enabled': True, 
    u'id': u'xyzxyz', 
    u'stageKeys': [u'abcabc/beta']
}

尝试使用get_api_key 时,出现参数验证错误:

get_api_key_response = client.get_api_key(
    apiKey='585yw0f1tk',
    includeValue=True
)

Unknown parameter in input: "includeValue", must be one of: apiKey: ParamValidationError

是否修改了 AWSboto3 模块以排除 value 键?生成的api key如何返回?

【问题讨论】:

  • 它会抛出什么错误?
  • 这不是错误,键 value 只是在 AWS Lambda 实现的字典中返回,否则它会返回。
  • 那么api_key_response 在那个时候是什么样子的?尝试打印它
  • 用结果更新了 OP

标签: python amazon-web-services aws-lambda aws-api-gateway boto3


【解决方案1】:

此处的差异可归因于您的 Lambda 环境与您的开发环境中 AWS 开发工具包的不同版本。

在较新版本的 SDK 中,作为安全措施,某些响应中会省略 API 密钥值。您可以通过使用 includeValue=True 单独调用 get_api_key 来检索 API 密钥值

【讨论】:

  • 我已经尝试将get_api_key 与includeValue=True 参数一起使用,而includeValue 参数验证失败。见 OP。
  • 我应该注意到我在我的开发环境中使用 boto3 v1.3.1,这与 AWS 实现相同。
  • 我建议在您的 Lambda 函数中捆绑最新版本的 SDK (1.4)。 Lambda 在 SDK 版本上往往落后。见docs.aws.amazon.com/lambda/latest/dg/…
猜你喜欢
  • 2017-05-01
  • 2020-10-09
  • 2020-09-12
  • 2020-10-05
  • 2019-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-25
相关资源
最近更新 更多