简体   繁体   English

从 AWS Lambda 为使用计划创建 API 密钥

[英]Creating API key for Usage Plan from AWS Lambda

I would like to create a new api key from lambda.我想从 lambda 创建一个新的 api 密钥。 I have usage plan with my Gateway API, created with CF like:我的网关 API 有使用计划,使用 CF 创建,例如:

MyApi:
    Type: AWS::Serverless::Api
    Properties:
        Auth:
            UsagePlan: 
                UsagePlanName: MyUsagePlan
                CreateUsagePlan: PER_API
                ...
        ...

Using this as a reference https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/APIGateway.html以此为参考https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/APIGateway.html

I guess the process in the lambda should be like this: - createApiKey - getUsagePlan - createUsagePlanKey我猜lambda中的流程应该是这样的: - createApiKey - getUsagePlan - createUsagePlanKey

In the lambda, I have MyApi id and I'm trying to fetch the api:在 lambda 中,我有 MyApi id,我正在尝试获取 api:

var apiGateway = new AWS.APIGateway({region: region});
const restApi = await new Promise((resolve, reject) => {
    apiGateway.getRestApi({restApiId: MYAPI_ID}, function(err, data) {
        if (err) {
            console.log('getRestApi err', err, err.stack);
            reject(err);
        } else {
            console.log('getRestApi', data);
            resolve(data);
        }
    });
});

But this gets timed out by my lambda.但这会被我的 lambda 超时。

If I try to input values manually, it gets timed out as well:如果我尝试手动输入值,它也会超时:

const keyParams = {
    keyId: 'xxxxxxxx',
    keyType: 'API_KEY',
    usagePlanId: 'yyyyyyyy'
  };
const apiKey = await new Promise((resolve, reject) => {
    apiGateway.createUsagePlanKey(keyParams, function (err, data) {
        if (err) {
            console.log('createUsagePlanKey err', err, err.stack);
            reject(err);
        } else {
            console.log('createUsagePlanKey', data);
            resolve(data);
        }
    });
});    

Why do every function call to api get timed out and nothing gets printed in console.log?为什么对 api 的每个 function 调用都会超时,并且在 console.log 中没有打印任何内容? Is my approach ok or how should I create the new api key for a user?我的方法可以吗,或者我应该如何为用户创建新的 api 密钥?

Edited: Timeout for lambdas is 10 seconds and they run in VPC已编辑:lambda 的超时时间为 10 秒,它们在 VPC 中运行

It sounds like you probably haven't configured your VPC to allow your Lambda function to access resources (like the AWS API) that exist outside the VPC.听起来您可能尚未将 VPC 配置为允许您的 Lambda function 访问存在于 VPC 之外的资源(如 AWS API)。 First, is it really necessary to run the function inside a VPC?首先,真的有必要在 VPC 中运行 function 吗? If not then removing it from the VPC should fix the issue.如果没有,那么从 VPC 中删除它应该可以解决问题。

If it is necessary to run the function in a VPC, then you will need to place your Lambda function inside a private subnet with a route to a NAT Gateway, or configure a VPC endpoint for the AWS services it needs to access.如果有必要在 VPC 中运行 function,那么您需要将 Lambda function 放置在私有子网中,该子网具有通往它需要访问的 AWS 服务端点的路由,或配置 VPC网关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以编程方式创建 aws API 密钥并将其添加到单个功能的使用计划中? - Programmatically creating aws API key and adding it to a usage plan in single function? 使用boto3更新AWS Lambda API密钥使用计划 - Update AWS Lambda API Key Usage Plans with boto3 使用 boto3 AWS api 网关将使用计划关联到 api 密钥 - Associate a usage plan to api key using boto3 AWS api gateway 在 AWS API 网关中,我可以在不附加 API 密钥的情况下使用使用计划吗? - In AWS API Gateway, can I use a Usage Plan without attaching an API Key? 在 aws api 网关中更改为新的 api 使用计划密钥不起作用 - Changing to new api key for usage plan in aws api gateway does not work 即使没有 API-key 作为授权机制,AWS 使用计划是否有效? - Is AWS usage plan in effect even without API-key as authorization mechanism? 使用 SAM 创建 API 密钥和使用计划 - Create an API Key and Usage Plan with SAM 将使用计划附加到 API 密钥不适用于“createUsagePlanKey” - Attaching an usage plan to an API Key is not working with `createUsagePlanKey` 在AWS上使用Lambda创建内部API - Creating an internal API with Lambda on AWS aws api 网关的使用计划限制高于默认限制 - aws api gateway higher usage plan limit than the default one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM