无服务器框架中的参考 API 网关

问题描述 投票:0回答:1

我正在使用以下 serverless.yaml 配置,以及

serverless-iam-roles-per-function
插件。

service: tester
frameworkVersion: "3"

plugins:
  - serverless-iam-roles-per-function

provider:
  name: aws
  runtime: nodejs16.x
  stage: dev
  region: eu-west-3
  httpApi:
    cors: true

custom:
  APIGatewayConnection: "arn:aws:execute-api:eu-west-3:ACCOUNTID:*/@connections/*"

functions:
  Checker:
    handler: lambda/checker.handler
    iamRoleStatementsName: Checker-Role
    iamRoleStatements:
      - Effect: Allow
        Action:
          - execute-api:ManageConnections
        Resource:
          - ${self:custom.APIGatewayConnection}
    events:
      - httpApi:
          method: GET
          path: /check

不是在自定义变量中写入 API 网关的 ARN,而是如何在 iamRoleStatements 中动态引用它?

amazon-web-services aws-api-gateway serverless-framework aws-serverless
1个回答
0
投票

要了解 intrinsic function 引用可以返回什么,我们需要检查您正在使用的 CloudFormation 资源。

在你的例子中,

AWS::ApiGateway::RestApi
只返回两件事:

  • RestApiId
  • RootResourceId

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-restapi.html#aws-resource-apigateway-restapi-return-values

无论 Serverless Framework 自动生成的资源名称如何,您都必须手动构建 ARN。 ARN 模式是:

arn:[partition]:execute-api:[region]:[account-id]:[api-id]/[stage]/[route-key]

arn:aws:execute-api:[region]:[account-id]:[api-id]/[stage]/[METHOD_HTTP_VERB]/[Resource-path]

https://docs.aws.amazon.com/apigateway/latest/developerguide/arn-format-reference.html

https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-control-access-using-iam-policies-to-invoke-api.html

你可以在这个页面查看Serverless Framework的命名规范列表:

https://www.serverless.com/framework/docs/providers/aws/guide/resources

因为我们需要引用Serverless Framework生成的CloudFormation资源,所以需要使用CloudFormation内在函数将上面的ARN转化为:

custom:
  APIGatewayConnection: !Sub arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${HttpApi}/${HttpApiStage}/@connections/*

回到你的问题。没有动态方式来引用此资源 ARN。您需要手动构建 ARN。

© www.soinside.com 2019 - 2024. All rights reserved.