如何将新的 Lambda 函数部署到现有 RestApi 的 Stage?

Chr*_* H. 5 typescript api-gateway aws-cdk

所以,我遇到了很多线索,但没有一个能解决我的问题。我正在尝试使用现有的 DEV-Stage 将 Lambda 函数部署到现有的 RestApi。我的 API 是这样构建的:

this.RestAPIDev = new gw.RestApi(this, 'StagingGatewayDev', {
  restApiName: 'gateway-DEV',
  deploy: true,
  deployOptions: {
    stageName: 'DEV',
    description: 'DEV Stage',
    variables: { ALIAS: 'DEV' },
  }
});
Run Code Online (Sandbox Code Playgroud)

Lambda Stack 独立于其自己的存储库中,包括管道和 lambda 代码。我尝试通过添加资源和方法将这些 Lambda 函数部署到此现有网关,并假设因为我在deploy:true网关上设置,所以它将自行部署。这是 Lambda 堆栈:

const restApi = gw.RestApi.fromRestApiAttributes(this, 'RestApi', {
  restApiId: props.restApiId,
  rootResourceId: props.restApiRoot,
});

const authenticate = new lambda.Function(this, 'AuthenticateFunction', {
  code: this.lambdaCode, // is a property
  runtime: lambda.Runtime.NODEJS_12_X,
  handler: 'src/api/authenticate.handler',
  description: 'API: Authenticates the token',
  role: <executionRole>, // is a property
});

const authenticateAlias = new lambda.Alias(this, 'AuthenticateAlias', {
  aliasName: 'DEV',
  version: new lambda.Version(this, 'AuthenticateVersion', {
    lambda: authenticate,
  }),
});

restApi.root.addResource('authenticate').addMethod(
  'POST',
  new gw.LambdaIntegration(authenticateAlias, {}),
); 
Run Code Online (Sandbox Code Playgroud)

但舞台选项卡上没有更新...

我还尝试了创建新部署和阶段的解决方案,但收到此阶段已存在的错误(我当然知道)。我还尝试创建一个新的部署并添加deployment.resources.stageName到其中,但这不适用于我的 CDK 版本。

这有什么线索吗?

提前致谢!

Mil*_*anG 0

我相信该阶段是在您定义 API 的堆栈中创建和部署完成的。在 lambda 堆栈中,不执行新部署。

我建议您不要将 API 导入到 lambda 堆栈中,而是将 lambda 导入到 API 堆栈中,并在 API 堆栈中定义 lambda 支持的资源。