roaris / news-ticker

毎朝ニュースを送信するLINE Bot
0 stars 0 forks source link

ステージング環境を作る #8

Open roaris opened 2 years ago

roaris commented 2 years ago

やること

公開後、バグが発生したり、機能追加をしたい時のためにステージング環境を用意したい template.yamlを書き換えて、別のLambda関数、DynamoDBとしてデプロイできないだろうか

roaris commented 2 years ago
roaris commented 2 years ago

How to deploy test/staging environments? · Issue #814 · aws/aws-sam-cliに書いてある方法でtemplate.yamlを書いたが動かず

roaris commented 2 years ago

stack_nameを切り替えてデプロイする方法しか出来なかった sam deployでうっかり本番環境にデプロイしてしまいそう

roaris commented 2 years ago

AWS SAMにおける環境管理のベストプラクティス | 株式会社CyberOwlの方に書いてる方でできそう

roaris commented 2 years ago

謎エラーが多すぎる... CloudFormationの管理画面からスタック(スタックはAPIとLambdaをまとめたものかな)を削除して、もう一度デプロイすると上手くいく ◆ AWS SAMを使ってLambdaにデプロイする – Hack Note

roaris commented 2 years ago
Api:
    Type: AWS::Serverless::Api
    Properties:
      StageName: dev
EchoFunction:
  Type: AWS::Serverless::Function
  Properties:
    CodeUri: echo/
    Handler: echo
    Runtime: go1.x
    Architectures:
      - x86_64
    Events:
      CatchAll:
        Type: Api
        Properties:
          Path: /echo
          Method: POST
          RestApiId: !Ref Api
    Policies:
      - AmazonDynamoDBFullAccess

StageNameはドキュメントには、

API Gateway が invoke Uniform Resource Identifier (URI) の最初のパスセグメントとして使用するステージの名前です。 

と書いている devにすると、エンドポイントは/dev/echoになるはずだが、なぜか/Stage/echoになる このStageNameは一体なんなんだ...

roaris commented 2 years ago

色々と試したが全てエラーで上手くいかなかった

Parameters:
  Env:
    Type: String
    AllowedValues:
      - Prod
      - Stage
    Default: Stage

Resources:
  EchoApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: !Ref Env
  EchoFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: echo/
      Handler: echo
      Runtime: go1.x
      Architectures:
        - x86_64
      Events:
        CatchAll:
          Type: Api
          Properties:
            Path: /echo
            Method: POST
            RestApiId: !Ref EchoApi
      Policies:
        - AmazonDynamoDBFullAccess
      FunctionName: !Sub EchoFunction${Env}

EnvをProdにしてデプロイした後に、Stageにしてデプロイすると、

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ResourceStatus                                    ResourceType                                      LogicalResourceId                                 ResourceStatusReason                            
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
UPDATE_FAILED                                     AWS::Lambda::Function                             EchoFunction                                      Replacement type updates not supported on stack 
                                                                                                                                                      with disable-rollback.                          
UPDATE_IN_PROGRESS                                AWS::CloudFormation::Stack                        news-ticker                                       Replacement type updates not supported on stack 
                                                                                                                                                      with disable-rollback.                          
UPDATE_ROLLBACK_COMPLETE                          AWS::Lambda::Function                             EchoFunction                                      Rollback succeeded for the failed resources.    
UPDATE_FAILED                                     AWS::CloudFormation::Stack                        news-ticker                                       The following resource(s) failed to update:     
                                                                                                                                                      [EchoFunction].                                 
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
roaris commented 2 years ago

こんなのも出た スタックを削除してから実行してるんだけど...

CREATE_FAILED                                     AWS::ApiGateway::Stage                            EchoApiStage                                      Resource handler returned message: "Stage       
                                                                                                                                                      already exists (Service: ApiGateway, Status     
                                                                                                                                                      Code: 409, Request ID: 1f3029fc-74b8-42fc-81bc- 
                                                                                                                                                      af7573f1eebe, Extended Request ID: null)"       
                                                                                                                                                      (RequestToken:                                  
                                                                                                                                                      5e59955f-6c43-09a8-cb30-27e0b59e0499,           
                                                                                                                                                      HandlerErrorCode: AlreadyExists)