首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SAM部署正在更新API网关响应,但未部署到stage

SAM部署正在更新API网关响应,但未部署到stage
EN

Stack Overflow用户
提问于 2020-07-30 12:53:04
回答 2查看 593关注 0票数 1

我注意到,我的SAM部署正在使用我对自定义API网关响应所做的更改来更新API Gateway的配置,但实际上并没有将它们部署到API Gateway阶段以使其生效。在SAM部署之后,如果我进入API网关控制台,选择我的API,打开Actions菜单,选择deploy API,选择my stage,然后点击deploy the changes,do go live go。要让SAM deploy将更新的配置部署到stage,我还需要执行其他步骤吗?

我做了一个重现这个问题的例子,下面是我的template.yml

代码语言:javascript
运行
复制
AWSTemplateFormatVersion: 2010-09-09
Transform:
  - AWS::Serverless-2016-10-31
  - AWS::CodeStar

Parameters:
  ProjectId:
    Type: String
    Description: CodeStar projectId used to associate new resources to team members
  CodeDeployRole:
    Type: String
    Description: IAM role to allow AWS CodeDeploy to manage deployment of AWS Lambda functions
  Stage:
    Type: String
    Description: The name for a project pipeline stage, such as Staging or Prod, for which resources are provisioned and deployed.
    Default: "Prod"

Resources:
  MyApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: !Sub "${Stage}"

  MissingAuthGatewayResponse:
    Type: AWS::ApiGateway::GatewayResponse
    Properties:
      ResponseTemplates:
        application/json: "{'message': 'Not found.'}"
      ResponseType: MISSING_AUTHENTICATION_TOKEN
      RestApiId: !Ref MyApi
      StatusCode: "403"

  HelloWorld:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: !Sub "awscodestar-${ProjectId}-lambda-HelloWorld"
      Handler: index.handler
      Runtime: python3.7
      Role:
        Fn::GetAtt:
          - LambdaExecutionRole
          - Arn
      Events:
        GetEvent:
          Type: Api
          Properties:
            RestApiId: !Ref MyApi
            Path: /
            Method: get

  LambdaExecutionRole:
    Description: Creating service role in IAM for AWS Lambda
    Type: AWS::IAM::Role
    Properties:
      RoleName: !Sub "CodeStar-${ProjectId}-Execution${Stage}"
      AssumeRolePolicyDocument:
        Statement:
          - Effect: Allow
            Principal:
              Service: [lambda.amazonaws.com]
            Action: sts:AssumeRole
      Path: /
      ManagedPolicyArns:
        - !Sub "arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
      PermissionsBoundary: !Sub "arn:${AWS::Partition}:iam::${AWS::AccountId}:policy/CodeStar_${ProjectId}_PermissionsBoundary"

Outputs:
  ApiURL:
    Description: "API URL"
    Value: !Sub "https://${MyApi}.execute-api.${AWS::Region}.amazonaws.com/${Stage}/"
EN

回答 2

Stack Overflow用户

发布于 2020-07-30 20:20:54

是啊。我发现这种行为也很烦人。让我们添加另一个类型为AWS::ApiGateway::Deployment的资源,并使用RestApiId属性连接到您的Api。现在,当您使用sam deploy时,它将使用提供的StageName部署您的api。

代码语言:javascript
运行
复制
Resources
...
  ApiGatewayDeployment:
    Type: AWS::ApiGateway::Deployment
    DependsOn:
    - HelloWorld
    Properties:
      RestApiId:
        Ref: MyApi
      StageName: !Sub "${Stage}"
票数 1
EN

Stack Overflow用户

发布于 2021-02-18 16:46:01

我也遇到了同样的问题:部署已经完成,但没有更新stage。问题是cloudformation不考虑具有相同名称的部署,所以如果您修改部署名称,它将按预期重新部署。

为了解决这个问题,我们使用了这个插件https://github.com/paprins/serverless-apigateway-deployment-timestamp,它基本上是在部署的名称上添加了一个时间戳。例如,此部署

代码语言:javascript
运行
复制
MyDeployment:
    Type: AWS::ApiGateway::Deployment

完成的部署如下所示

代码语言:javascript
运行
复制
APIProxyDeployment19878797197:
    Type: AWS::ApiGateway::Deployment

另一种选择是预处理描述符,修改部署名称,但对我来说,插件是更简单的解决方案。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63166411

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档