Es scheint, dass sam aktualisiert wurde, um die Verwendung zu vereinfachen. Daher werde ich ein Memorandum über den Ablauf von der Umgebungskonstruktion bis zur Bereitstellung mit Pipenv aufbewahren.
$ pip install pipenv
$ pipenv --python 3.7
$ pipenv shell
Die während der Entwicklung benötigten Bibliotheken können durch Hinzufügen der Option "--dev" getrennt werden.
$ pipenv install aws-sam-cli --dev
Machen Sie für nachfolgende Arbeiten Projektname [sam-app]: den gleichen Namen wie das Verzeichnis, das Pipfile usw. enthält.
$ sam init
Which template source would you like to use?
        1 - AWS Quick Start Templates
        2 - Custom Template Location
Choice: 1
Which runtime would you like to use?
        1 - nodejs12.x
        2 - python3.8
        3 - ruby2.5
        4 - go1.x
        5 - java11
        6 - dotnetcore2.1
        7 - nodejs10.x
        8 - nodejs8.10
        9 - python3.7
        10 - python3.6
        11 - python2.7
        12 - java8
        13 - dotnetcore2.0
        14 - dotnetcore1.0
Runtime: 9
Project name [sam-app]:   aws-sam-1       
Allow SAM CLI to download AWS-provided quick start templates from Github [Y/n]: y
AWS quick start application templates:
        1 - Hello World Example
        2 - EventBridge Hello World
        3 - EventBridge App from scratch (100+ Event Schemas)
Template selection: 1
-----------------------
Generating application:
-----------------------
Name: sam-app
Runtime: python3.7
Dependency Manager: pip
Application Template: hello-world
Output Directory: .
Next steps can be found in the README file at ./aws-sam-1/README.md
Bei der Arbeit bis zu diesem Punkt ist die Verzeichnisstruktur wie folgt.
.
├── Pipfile
├── Pipfile.lock
├── README.md
└── aws-sam-1
    ├── README.md
    ├── events
    │   └── event.json
    ├── hello_world
    │   ├── __init__.py
    │   ├── app.py
    │   └── requirements.txt
    ├── template.yaml
    └── tests
        └── unit
            ├── __init__.py
            └── test_handler.py
Da es jedoch besser ist, flach zu sein, bringen Sie "aws-sam-1 /" und darunter auf die nächsthöhere Ebene.
$ mv ./aws-sam-1/* ./
$ rm -r ./aws-sam-1
Die Verzeichnishierarchie nach der Ausführung sieht folgendermaßen aus. Ich verwalte nicht oft mehrere Vorlagen in einem Repository, daher mag ich diese, weil sie sauberer ist.
.
├── Pipfile
├── Pipfile.lock
├── README.md
├── events
│   └── event.json
├── hello_world
│   ├── __init__.py
│   ├── app.py
│   └── requirements.txt
├── template.yaml
└── tests
    └── unit
        ├── __init__.py
        └── test_handler.py
$ sam build
Building resource 'HelloWorldFunction'
Running PythonPipBuilder:ResolveDependencies
Running PythonPipBuilder:CopySource
Build Succeeded
Built Artifacts  : .aws-sam/build
Built Template   : .aws-sam/build/template.yaml
Commands you can use next
=========================
[*] Invoke Function: sam local invoke
[*] Deploy: sam deploy --guided
Wenn Sie es ausführen, können Sie unter .aws-sam / verschiedene Dinge tun.
Ich werde nicht im Detail erklären, aber es sind das Ausführungsskript, template.yaml, die Bibliothek usw., die tatsächlich bereitgestellt werden.
Nun, es ist endlich bereitgestellt, aber ich denke, das Update hat es hier am einfachsten gemacht.
Bisher war es ziemlich mühsam, den Quellcode und die abhängigen Dateien jedes Mal mit "sam package" zu aktualisieren und den Stapelnamen zum Zeitpunkt der Bereitstellung anzugeben. Nach dem Update können Sie sie nun mit der Option "--guided" in einer Datei namens "samconfig.toml" zusammenstellen. Auf diese Weise können die zweite und die nachfolgenden Bereitstellungen nur mit dem Befehl "sam deploy" ausgeführt werden.
Lassen Sie uns nun nach dem Update bereitstellen. Wenn Sie separate Einstellungen für "~ / .aws / credentials" haben, fügen Sie die Option "--profile" hinzu.
$ sam deploy --guided
Configuring SAM deploy
======================
        Looking for samconfig.toml :  Not found
        Setting default arguments for 'sam deploy'
        =========================================
        Stack Name [sam-app]: aws-sam-1-stack  
        AWS Region [us-east-1]: ap-northeast-1              
        #Shows you resources changes to be deployed and require a 'Y' to initiate deploy
        Confirm changes before deploy [y/N]: y
        #SAM needs permission to be able to create roles to connect to the resources in your template
        Allow SAM CLI IAM role creation [Y/n]: 
        Save arguments to samconfig.toml [Y/n]: 
        Looking for resources needed for deployment: Not found.
        Creating the required resources...
        Successfully created!
                Managed S3 bucket: aws-sam-cli-managed-default-samclisourcebucket-1np75dr62k62n
                A different default S3 bucket can be set in samconfig.toml
        Saved arguments to config file
        Running 'sam deploy' for future deployments will use the parameters saved above.
        The above parameters can be changed by modifying samconfig.toml
        Learn more about samconfig.toml syntax at 
        https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html
        Deploying with following values
        ===============================
        Stack name                 : aws-sam-1-stack
        Region                     : ap-northeast-1
        Confirm changeset          : True
        Deployment s3 bucket       : aws-sam-cli-managed-default-samclisourcebucket-1np75dr62k62n
        Capabilities               : ["CAPABILITY_IAM"]
        Parameter overrides        : {}
Initiating deployment
=====================
Uploading to aws-sam-1-stack/7f10f61137bc6dbef4059bf50229b958  532293 / 532293.0  (100.00%)
Uploading to aws-sam-1-stack/0797e53a6d5052f23f1972e20e6fd8dd.template  1102 / 1102.0  (100.00%)
Waiting for changeset to be created..
CloudFormation stack changeset
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Operation                                                           LogicalResourceId                                                   ResourceType                                                      
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ Add                                                               HelloWorldFunctionHelloWorldPermissionProd                          AWS::Lambda::Permission                                           
+ Add                                                               HelloWorldFunctionRole                                              AWS::IAM::Role                                                    
+ Add                                                               HelloWorldFunction                                                  AWS::Lambda::Function                                             
+ Add                                                               ServerlessRestApiDeployment47fc2d5f9d                               AWS::ApiGateway::Deployment                                       
+ Add                                                               ServerlessRestApiProdStage                                          AWS::ApiGateway::Stage                                            
+ Add                                                               ServerlessRestApi                                                   AWS::ApiGateway::RestApi                                          
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Changeset created successfully. arn:aws:cloudformation:ap-northeast-1:xxxxxxxxxxxx:changeSet/samcli-deploy1576913429/1265b6b9-05ac-4a26-9f75-d81099274672
Previewing CloudFormation changeset before deployment
======================================================
Deploy this changeset? [y/N]: y
2019-12-21 16:30:55 - Waiting for stack create/update to complete
CloudFormation events from changeset
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ResourceStatus                                     ResourceType                                       LogicalResourceId                                  ResourceStatusReason                             
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE_IN_PROGRESS                                 AWS::IAM::Role                                     HelloWorldFunctionRole                             Resource creation Initiated                      
CREATE_IN_PROGRESS                                 AWS::IAM::Role                                     HelloWorldFunctionRole                             -                                                
CREATE_COMPLETE                                    AWS::IAM::Role                                     HelloWorldFunctionRole                             -                                                
CREATE_IN_PROGRESS                                 AWS::Lambda::Function                              HelloWorldFunction                                 -                                                
CREATE_IN_PROGRESS                                 AWS::Lambda::Function                              HelloWorldFunction                                 Resource creation Initiated                      
CREATE_COMPLETE                                    AWS::Lambda::Function                              HelloWorldFunction                                 -                                                
CREATE_IN_PROGRESS                                 AWS::ApiGateway::RestApi                           ServerlessRestApi                                  -                                                
CREATE_IN_PROGRESS                                 AWS::ApiGateway::RestApi                           ServerlessRestApi                                  Resource creation Initiated                      
CREATE_COMPLETE                                    AWS::ApiGateway::RestApi                           ServerlessRestApi                                  -                                                
CREATE_IN_PROGRESS                                 AWS::Lambda::Permission                            HelloWorldFunctionHelloWorldPermissionProd         -                                                
CREATE_IN_PROGRESS                                 AWS::Lambda::Permission                            HelloWorldFunctionHelloWorldPermissionProd         Resource creation Initiated                      
CREATE_IN_PROGRESS                                 AWS::ApiGateway::Deployment                        ServerlessRestApiDeployment47fc2d5f9d              -                                                
CREATE_COMPLETE                                    AWS::ApiGateway::Deployment                        ServerlessRestApiDeployment47fc2d5f9d              -                                                
CREATE_IN_PROGRESS                                 AWS::ApiGateway::Deployment                        ServerlessRestApiDeployment47fc2d5f9d              Resource creation Initiated                      
CREATE_IN_PROGRESS                                 AWS::ApiGateway::Stage                             ServerlessRestApiProdStage                         -                                                
CREATE_IN_PROGRESS                                 AWS::ApiGateway::Stage                             ServerlessRestApiProdStage                         Resource creation Initiated                      
CREATE_COMPLETE                                    AWS::ApiGateway::Stage                             ServerlessRestApiProdStage                         -                                                
CREATE_COMPLETE                                    AWS::Lambda::Permission                            HelloWorldFunctionHelloWorldPermissionProd         -                                                
CREATE_COMPLETE                                    AWS::CloudFormation::Stack                         aws-sam-1-stack                                    -                                                
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Stack aws-sam-1-stack outputs:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
OutputKey-Description                                                                                 OutputValue                                                                                         
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
HelloWorldFunctionIamRole - Implicit IAM Role created for Hello World function                        arn:aws:iam::xxxxxxxxxxxx:role/aws-sam-1-stack-HelloWorldFunctionRole-PB5A5G9O9MBP                  
HelloWorldApi - API Gateway endpoint URL for Prod stage for Hello World function                      https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/Prod/hello/                             
HelloWorldFunction - Hello World Lambda Function ARN                                                  arn:aws:lambda:ap-northeast-1:xxxxxxxxxxxx:function:aws-sam-1-stack-                                
                                                                                                      HelloWorldFunction-1QOQ29MG7346Z                                                                    
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Successfully created/updated stack - aws-sam-1-stack in ap-northeast-1
Die Bereitstellung ist jetzt abgeschlossen.
HelloWorldApi - API Gateway endpoint URL for Prod stage for Hello World function https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/Prod/hello/
Der Zugriffspunkt auf API Gateway wird im Protokoll angezeigt. Greifen wir also darauf zu.
curl https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/Prod/hello/
{"message": "hello world"}
Wenn es zurückkommt, ist es ein Erfolg.
Diesmal habe ich die Tutorial-App so verwendet, wie sie ist. Beim nächsten Mal möchte ich eine API erstellen, indem ich DynamoDB kombiniere.
Repository: https://gitlab.com/tarte9572/aws-sam-1
Recommended Posts