【问题标题】:API Name is always the Stack Name in SAMAPI 名称始终是 SAM 中的堆栈名称
【发布时间】:2019-07-29 19:38:54
【问题描述】:

我正在使用 AWS CLI 部署 SAM 模板。

AWS Api 名称设置为与 CloudFormation 堆栈名称相同。根据下面的模板内容,我希望 Api 被称为“用户”。

是否可以设置 API 名称?

SAM 模板:

  MyApi:
    Type: AWS::Serverless::Api
    Properties:
      Name: Users
      StageName: default

使用其他信息更新(用于部署的完整模板和 AWS CLI 命令):

模板:

AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31

Resources:
  HelloFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: main
      Runtime: go1.x
      Events:
        GetEvent:
          Type: Api
          Properties:
            Path: /
            Method: post
            #RestApiId: !Ref ApiGateway1

  LambdaInvokePermission:
    Type: AWS::Lambda::Permission
    Properties:
      FunctionName: !GetAtt
        - HelloFunction
        - Arn
      Action: 'lambda:InvokeFunction'
      Principal: apigateway.amazonaws.com
      SourceAccount: !Ref 'AWS::AccountId'

  MyApi:
    Type: AWS::Serverless::Api
    Properties:
      Name: Users
      StageName: default
      EndpointConfiguration: REGIONAL
      DefinitionBody:
        swagger: "2.0"
        info:
          title: "TestAPI"
        paths:
          /:
            post:
              x-amazon-apigateway-integration:
                uri:
                  Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HelloFunction.Arn}/invocations
                responses: {}
                httpMethod: "POST"
                type: "aws_proxy"
Outputs:
  FunctioArn:
    Value: !GetAtt  HelloFunction.Arn
    Export:
      Name: HelloFunctionArn

CLI 命令:

aws cloudformation package --template-file template.yml 
--output-template-file samtemplate.yaml --s3-bucket (bucketname) 

aws cloudformation deploy --template-file samtemplate.yaml 
--stack-name apisample-stack  --capabilities CAPABILITY_IAM

【问题讨论】:

  • 基于此代码片段是不可能的。如您所料,它将创建一个名为 Users 的 API。您必须在模板中的某处引用 StackName。你能提供更多吗?
  • @pyb,您删除的那些标签实际上是正确的。这是一个关于 AWS SAM 的问题,因为它指的是 SAM 资源类型之一。
  • 还有@jhurtas,你为什么说你使用的是CloudFormation而不是SAM?我假设您知道您提供了一个 SAM 模板?
  • 对不起@AlexHarvey,我的错。
  • 我更新了问题以澄清我正在使用 sam 模板。

标签: aws-lambda amazon-cloudformation aws-serverless aws-sam aws-sam-cli


【解决方案1】:

您使用 Name 属性是设置 API 名称的正确方法。

但是,我认为您对该模板的作用感到困惑。如您所写,此模板创建了两个 API - “隐式 API”,然后是您明确声明的名为“用户”的 API。

注意:

  • 隐式 API 的名称取自堆栈名称。 (这似乎是您所观察到的。)

  • 显式 API 的名称取自 Name 属性。

如果您不想创建隐式 API 而是自己显式定义它 - 似乎是这样 - 那么您只需要引用它:

  Events:
    GetEvent:
      Type: Api
      Properties:
        Path: /
        Method: post
        RestApiId: !Ref MyApi # Add this line

这将导致创建一个 API,并将其命名为“Users”。

【讨论】:

  • 可耻的是,这就是问题所在。我只注意到一个 API,但有 2 个。
  • 哦,不丢人。这种隐含的 API 内容令人困惑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-25
  • 2022-10-19
  • 2018-06-15
  • 1970-01-01
  • 1970-01-01
  • 2021-10-31
  • 2017-04-01
相关资源
最近更新 更多