{"id":10091,"date":"2023-03-22T13:16:54","date_gmt":"2023-03-22T05:16:54","guid":{"rendered":"http:\/\/123.57.164.21\/?p=10091"},"modified":"2023-03-22T13:16:54","modified_gmt":"2023-03-22T05:16:54","slug":"%e3%80%90aws%e3%80%91%e5%a6%82%e4%bd%95%e6%9e%84%e5%bb%ba%e6%97%a0%e6%9c%8d%e5%8a%a1%e5%99%a8-api","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=10091","title":{"rendered":"\u3010AWS\u3011\u5982\u4f55\u6784\u5efa\u65e0\u670d\u52a1\u5668 API"},"content":{"rendered":"\n<p><strong>\u6545\u4e8b\u80cc\u666f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u8001\u677f\uff1a\u4eca\u5929\u7684\u4f1a\u8bae\u5c31\u5230\u8fd9\u91cc\u4e86\uff0c\u5c0f\u767d\uff0c\u8fd9\u4e2a API \u4f60\u6765\u5b9e\u73b0\u4e00\u4e0b\u5427\u3002\u4e0a\u7ebf\u521d\u671f\u7684\u6d41\u91cf\u6bd4\u8f83\u4f4e\uff0c\u4f60\u9009\u4e00\u4e2a\u65e2\u7701\u94b1\u53c8\u5bb9\u6613\u6269\u5bb9\u7684\u65b9\u6848\u3002<br>\u5c0f\u767d\uff1a\u597d\u7684\u8001\u677f\u2026\u2026<\/p>\n\n\n\n<p>( \u56de\u5230\u5de5\u4f4d\u540e\u2026\u2026 )<\/p>\n\n\n\n<p>\u5c0f\u767d\uff1a\u6269\u5bb9\u4e0d\u662f\u5927\u95ee\u9898\uff0c\u4f46\u662f\u8fd8\u6709\u6bd4\u53ea\u7528\u4e00\u53f0\u670d\u52a1\u5668\u66f4\u7701\u94b1\u7684\u65b9\u6848\u5417\uff1f<br>\u5927\u795e\uff1a\u6bd4\u4e00\u53f0\u670d\u52a1\u5668\u66f4\u7701\u94b1\u7684\u65b9\u6848\uff0c\u5f53\u7136\u662f\u96f6\u53f0\u670d\u52a1\u5668\u4e86\u3002<br>\u5c0f\u767d\uff1a\u96f6\u53f0\u670d\u52a1\u5668\uff1f\u8fd8\u6709\u8fd9\u79cd\u6280\u672f\uff1f\uff01<br>\u5927\u795e\uff1a\u96be\u9053\u4f60\u4e0d\u77e5\u9053 AWS \u7684\u65e0\u670d\u52a1\u5668\u67b6\u6784\u5417\uff1f<br>\u5c0f\u767d\uff1a\u4f46\u6c42\u8d50\u6559\uff01<\/p>\n\n\n\n<p>( \u5927\u795e\u5f00\u542f\u6559\u5b66\u6a21\u5f0f )<\/p>\n\n\n\n<p><strong>\u6709\u670d\u52a1\u5668\u67b6\u6784<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u4f20\u7edf\u7684\u6709\u670d\u52a1\u5668 API \u67b6\u6784\u662f\u7531<strong>\u8d1f\u8f7d\u5747\u8861<\/strong>\u548c <strong>WEB \u5e94\u7528\u7a0b\u5e8f<\/strong>\u6240\u7ec4\u6210\u7684\u3002\u5ba2\u6237\u7aef\u5c06\u8bf7\u6c42\u53d1\u9001\u5230\u8d1f\u8f7d\u5747\u8861\uff0c\u8d1f\u8f7d\u5747\u8861\u518d\u8f6c\u53d1\u7ed9 WEB \u5e94\u7528\u7a0b\u5e8f\u3002WEB \u5e94\u7528\u7a0b\u5e8f\u751f\u6210\u54cd\u5e94\u7ed3\u679c\u518d\u901a\u8fc7\u8d1f\u8f7d\u5747\u8861\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/03\/image-217-1024x475.png\" alt=\"\" class=\"wp-image-10092\" width=\"519\" height=\"240\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-217-1024x475.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-217-300x139.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-217-768x356.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-217-830x385.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-217-230x107.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-217-350x162.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-217-480x223.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-217.png 1194w\" sizes=\"(max-width: 519px) 100vw, 519px\" \/><\/figure><\/div>\n\n\n\n<p><strong>\u65e0\u670d\u52a1\u5668\u67b6\u6784<\/strong><\/p>\n\n\n\n<p>\u65e0\u670d\u52a1\u5668\u67b6\u6784\u548c\u4f20\u7edf\u7684\u6709\u670d\u52a1\u5668\u67b6\u6784\u5e76\u6ca1\u6709\u672c\u8d28\u7684\u533a\u522b\uff0c\u53ea\u4e0d\u8fc7\u662f\u7528 AWS \u63d0\u4f9b\u7684\u6258\u7ba1\u670d\u52a1\u4f5c\u4e3a<strong>\u8d1f\u8f7d\u5747\u8861<\/strong>\u548c <strong>WEB \u5e94\u7528\u7a0b\u5e8f<\/strong>\u3002<br>\u5177\u4f53\u6765\u8bf4\u662f\u4f7f\u7528\u6258\u7ba1\u7684 <a href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/docs.aws.amazon.com\/apigateway\/latest\/developerguide\/welcome.html\" target=\"_blank\" rel=\"noreferrer noopener\">API Gateway<\/a> \u4f5c\u4e3a\u8d1f\u8f7d\u5747\u8861\uff0c\u4f7f\u7528 <a href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/welcome.html\" target=\"_blank\" rel=\"noreferrer noopener\">Lambda<\/a> \u4f5c\u4e3a WEB \u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n\n\n\n<p>\u4e0d\u540c\u4e4b\u5904\u5728\u4e8e Lambda \u662f<strong>\u6309\u9700\u542f\u52a8<\/strong>\u7684\u3002\u5f53 API Gateway \u63a5\u6536\u5230\u8bf7\u6c42\u65f6\u4f1a\u521b\u5efa Lambda \u5b9e\u4f8b\u6765\u5904\u7406\u8bf7\u6c42\uff1b\u5982\u679c API Gateway \u5728\u4e00\u6bb5\u65f6\u95f4\u5185\u6ca1\u6709\u6536\u5230\u8bf7\u6c42\uff0c\u90a3\u4e48 Lambda \u5b9e\u4f8b\u5c31\u4f1a\u88ab\u81ea\u52a8\u91ca\u653e\u3002\u91ca\u653e\u540e\u505c\u6b62\u8ba1\u8d39\uff0c\u8fbe\u5230<strong>\u964d\u4f4e\u6210\u672c<\/strong>\u7684\u76ee\u7684\u3002<\/p>\n\n\n\n<p>\u6269\u5bb9\u4e5f\u975e\u5e38\u7b80\u5355\u3002\u5f53 API Gateway \u6536\u5230\u7684\u8bf7\u6c42\u91cf\u8d85\u51fa Lambda \u5b9e\u4f8b\u7684\u5904\u7406\u80fd\u529b\u65f6\uff0c\u66f4\u591a\u7684 Lambda \u5b9e\u4f8b\u5c31\u4f1a\u88ab\u81ea\u52a8\u521b\u5efa\uff0c\u5b9e\u73b0<strong>\u81ea\u52a8\u6269\u5bb9<\/strong>\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/03\/image-218-1024x440.png\" alt=\"\" class=\"wp-image-10093\" width=\"484\" height=\"207\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-218-1024x440.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-218-300x129.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-218-768x330.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-218-830x357.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-218-230x99.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-218-350x150.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-218-480x206.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-218.png 1196w\" sizes=\"(max-width: 484px) 100vw, 484px\" \/><\/figure><\/div>\n\n\n\n<p>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u65e0\u670d\u52a1\u5668 Hello World API \u53ea\u9700 5 \u6b65\u3002<\/p>\n\n\n\n<p><strong>\u6b65\u9aa4 1\uff1a\u5b89\u88c5 AWS SAM CLI<\/strong><\/p>\n\n\n\n<p>AWS Serverless Application Model ( <a rel=\"noreferrer noopener\" href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/docs.aws.amazon.com\/serverless-application-model\/latest\/developerguide\/what-is-sam.html\" target=\"_blank\">AWS SAM<\/a> ) \u662f\u4e00\u4e2a\u5f00\u6e90\u6846\u67b6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u6846\u67b6\u5728 AWS \u4e0a\u6784\u5efa\u65e0\u670d\u52a1\u5668\u5e94\u7528\u3002<br>\u6839\u636e\u4f60\u4f7f\u7528\u7684\u64cd\u4f5c\u7cfb\u7edf\uff0c\u4f9d\u7167\u8fd9\u7bc7 <a rel=\"noreferrer noopener\" href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/docs.aws.amazon.com\/zh_cn\/serverless-application-model\/latest\/developerguide\/serverless-sam-cli-install.html\" target=\"_blank\">\u6587\u6863<\/a> \u4e2d\u7684\u6b65\u9aa4\u5b89\u88c5 AWS SAM CLI\u3002\u5982\u679c\u4f60\u60f3\u5728\u672c\u5730\u6d4b\u8bd5\u4f60\u7684 API\uff0c\u4f60\u8fd8\u9700\u8981\u6839\u636e\u6587\u6863\u4e2d\u7684\u6307\u793a\u5b89\u88c5 Docker\u3002<\/p>\n\n\n\n<p><strong>\u6b65\u9aa4 2\uff1a\u521b\u5efa\u793a\u4f8b\u5de5\u7a0b<\/strong><\/p>\n\n\n\n<p>\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u5de5\u7a0b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sam init<\/pre>\n\n\n\n<p>\u5982\u679c\u4f60\u6839\u636e\u63d0\u793a\u9009\u62e9\u4e86 Python \u9879\u76ee\uff0c\u4f60\u5c06\u4f1a\u770b\u5230\u5982\u4e0b\u8f93\u51fa\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">-----------------------\nGenerating application:\n-----------------------\nName: sam-app\nRuntime: python3.7\nDependency Manager: pip\nApplication Template: hello-world\nOutput Directory: .\n\nNext steps can be found in the README file at .\/sam-app\/README.md<\/pre>\n\n\n\n<p>\u81f3\u6b64\uff0c\u4e00\u4e2a\u793a\u4f8b\u5de5\u7a0b\u5c31\u521b\u5efa\u5b8c\u6bd5\u4e86\uff0c\u76ee\u5f55\u7ed3\u6784\u5982\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sam-app\/\n  \u251c\u2500\u2500 README.md\n  \u251c\u2500\u2500 events\/\n  \u2502   \u2514\u2500\u2500 event.json\n  \u251c\u2500\u2500 hello_world\/\n  \u2502   \u251c\u2500\u2500 __init__.py\n  \u2502   \u251c\u2500\u2500 app.py            #Contains your AWS Lambda handler logic.\n  \u2502   \u2514\u2500\u2500 requirements.txt  #Contains any Python dependencies the application requires, used for sam build\n  \u251c\u2500\u2500 template.yaml         #Contains the AWS SAM template defining your application's AWS resources.\n  \u2514\u2500\u2500 tests\/\n      \u2514\u2500\u2500 unit\/\n          \u251c\u2500\u2500 __init__.py\n          \u2514\u2500\u2500 test_handler.py<\/pre>\n\n\n\n<p>\u6709\u4e09\u4e2a\u6587\u4ef6\u6bd4\u8f83\u91cd\u8981\uff1a<\/p>\n\n\n\n<ul><li>template.yaml: \u5b9a\u4e49 AWS \u8d44\u6e90\u7684\u6587\u4ef6\uff0c\u4f8b\u5982 API Gateway \u548c Lambda\u3002<\/li><li>hello_world\/app.py: Lambda \u7684\u5904\u7406\u51fd\u6570\u3002<\/li><li>hello_world\/requirements.txt: \u58f0\u660e Python \u7684\u4f9d\u8d56\u9879\u3002<\/li><\/ul>\n\n\n\n<p><strong>\u6b65\u9aa4 3\uff1a\u6784\u5efa\u4f60\u7684 API<\/strong><\/p>\n\n\n\n<p>\u8fdb\u5165\u5230\u5de5\u7a0b\u6240\u5728\u76ee\u5f55\uff0c\u7136\u540e\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sam build<\/pre>\n\n\n\n<p>\u4f60\u4f1a\u770b\u5230\u5982\u4e0b\u8f93\u51fa\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Build Succeeded\n\nBuilt Artifacts : .aws-sam\/build\nBuilt Template : .aws-sam\/build\/template.yaml\n\nCommands you can use next\n=========================\n[*] Invoke Function: sam local invoke\n[*] Deploy: sam deploy --guided<\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u628a\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u7684\u6240\u6709\u4f9d\u8d56\u9879\u4ee5\u53ca\u5e94\u7528\u7a0b\u5e8f\u672c\u8eab\u7684\u4ee3\u7801\u590d\u5236\u5230 <code>.aws-sam\/build<\/code> \u76ee\u5f55\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.aws-sam\/\n  \u2514\u2500\u2500 build\/\n      \u251c\u2500\u2500 HelloWorldFunction\/\n      \u2514\u2500\u2500 template.yaml<\/pre>\n\n\n\n<p><strong>\u6b65\u9aa4 4\uff1a\u90e8\u7f72\u5230 AWS<\/strong><\/p>\n\n\n\n<p>\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u628a\u7a0b\u5e8f\u90e8\u7f72\u5230 AWS \u4e91\u7aef\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sam deploy --guided<\/pre>\n\n\n\n<p>\u6839\u636e\u63d0\u793a\u8f93\u5165\u76f8\u5e94\u4fe1\u606f\uff0c\u76f4\u63a5\u8f93\u5165\u56de\u8f66\u5c31\u662f\u9075\u5faa\u9ed8\u8ba4\u8bbe\u7f6e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u4f60\u53ef\u80fd\u4f1a\u88ab\u95ee\u5230 HelloWorldFunction may not have authorization defined, Is this okay? [y\/N]\uff0c\u8fd9\u662f AWS SAM \u5728\u544a\u8bc9\u4f60\uff0c\u4f60\u7684 API \u6ca1\u6709\u8eab\u4efd\u9a8c\u8bc1\uff0c\u90e8\u7f72\u540e\u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u8c03\u7528\u3002 \u5728\u8fd9\u91cc\u8f93\u5165 y \u5c31\u53ef\u4ee5\uff0c\u6211\u4eec\u5148\u4e0d\u7ba1\u8eab\u4efd\u9a8c\u8bc1\u7684\u4e8b\u60c5\u3002<\/pre>\n\n\n\n<p>\u4e4b\u540e\u4f60\u4f1a\u5f97\u5230\u5982\u4e0b\u8f93\u51fa\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Deploying with following values\n===============================\nStack name : sam-app\nRegion : us-east-1\nConfirm changeset : False\nDeployment s3 bucket : sam-bucket\nCapabilities : [\"CAPABILITY_IAM\"]\nParameter overrides : {}\n\nInitiating deployment\n=====================\n\nWaiting for changeset to be created..\n\nCloudFormation stack changeset\n---------------------------------------------------------------------------------------------------------------------------------------------------\nOperation                                         LogicalResourceId                                 ResourceType\n---------------------------------------------------------------------------------------------------------------------------------------------------\n+ Add                                             HelloWorldFunctionHelloWorldPermissionProd        AWS:: Lambda:: Permission\n+ Add                                             ServerlessRestApiDeployment47fc2d5f9d             AWS:: ApiGateway:: Deployment\n+ Add                                             ServerlessRestApiProdStage                        AWS:: ApiGateway:: Stage\n+ Add                                             ServerlessRestApi                                 AWS:: ApiGateway:: RestApi\n-   Modify                                          HelloWorldFunctionRole                            AWS:: IAM:: Role\n-   Modify                                          HelloWorldFunction                                AWS:: Lambda:: Function\n---------------------------------------------------------------------------------------------------------------------------------------------------\n\n2019-11-21 14:33:24 - Waiting for stack create\/update to complete\n\nCloudFormation events from changeset\n-------------------------------------------------------------------------------------------------------------------------------------------------\nResourceStatus                       ResourceType                         LogicalResourceId                    ResourceStatusReason\n-------------------------------------------------------------------------------------------------------------------------------------------------\nUPDATE_IN_PROGRESS                   AWS:: IAM:: Role                       HelloWorldFunctionRole               -\nUPDATE_COMPLETE                      AWS:: IAM:: Role                       HelloWorldFunctionRole               -\nUPDATE_IN_PROGRESS                   AWS:: Lambda:: Function                HelloWorldFunction                   -\nUPDATE_COMPLETE                      AWS:: Lambda:: Function                HelloWorldFunction                   -\nCREATE_IN_PROGRESS                   AWS:: ApiGateway:: RestApi             ServerlessRestApi                    -\nCREATE_COMPLETE                      AWS:: ApiGateway:: RestApi             ServerlessRestApi                    -\nCREATE_IN_PROGRESS                   AWS:: ApiGateway:: RestApi             ServerlessRestApi                    Resource creation Initiated\nCREATE_IN_PROGRESS                   AWS:: ApiGateway:: Deployment          ServerlessRestApiDeployment47fc2d5   Resource creation Initiated\n                                                                         f9d\nCREATE_IN_PROGRESS                   AWS:: Lambda:: Permission              HelloWorldFunctionHelloWorldPermis   Resource creation Initiated\n                                                                         sionProd\nCREATE_IN_PROGRESS                   AWS:: Lambda:: Permission              HelloWorldFunctionHelloWorldPermis   -\n                                                                         sionProd\nCREATE_IN_PROGRESS                   AWS:: ApiGateway:: Deployment          ServerlessRestApiDeployment47fc2d5   -\n                                                                         f9d\nCREATE_COMPLETE                      AWS:: ApiGateway:: Deployment          ServerlessRestApiDeployment47fc2d5   -\n                                                                         f9d\nCREATE_IN_PROGRESS                   AWS:: ApiGateway:: Stage               ServerlessRestApiProdStage           -\nCREATE_IN_PROGRESS                   AWS:: ApiGateway:: Stage               ServerlessRestApiProdStage           Resource creation Initiated\nCREATE_COMPLETE                      AWS:: ApiGateway:: Stage               ServerlessRestApiProdStage           -\nCREATE_COMPLETE                      AWS:: Lambda:: Permission              HelloWorldFunctionHelloWorldPermis   -\n                                                                         sionProd\nUPDATE_COMPLETE_CLEANUP_IN_PROGRES   AWS:: CloudFormation:: Stack           sam-app                              -\nS\nUPDATE_COMPLETE                      AWS:: CloudFormation:: Stack           sam-app                              -\n-------------------------------------------------------------------------------------------------------------------------------------------------\n\nStack sam-app outputs:\n---------------------------------------------------------------------------------------------------------------------------------------------------\nOutputKey-Description                                                     OutputValue\n---------------------------------------------------------------------------------------------------------------------------------------------------\nHelloWorldFunctionIamRole - Implicit IAM Role created for Hello World     arn: aws: iam::123456789012: role\/sam-app-\nfunction                                                                  HelloWorldFunctionRole-104VTJ0TST7M0\nHelloWorldApi - API Gateway endpoint URL for Prod stage for Hello World   https:\/\/0ks2zue0zh.execute-api.us-east-1.amazonaws.com\/Prod\/hello\/\nfunction\nHelloWorldFunction - Hello World Lambda Function ARN                      arn: aws: lambda: us-east-1:123456789012: function: sam-app-\n                                                                         HelloWorldFunction-1TY92MJX0BXU5\n---------------------------------------------------------------------------------------------------------------------------------------------------\n\nSuccessfully created\/updated stack - sam-app in us-east-1<\/pre>\n\n\n\n<p>\u81f3\u6b64\uff0c\u4f60\u7684 API \u5c31\u88ab\u90e8\u7f72\u5230 AWS \u4e91\u7aef\u4e86\u3002\u5177\u4f53\u6765\u8bf4 <code>sam deploy<\/code> \u547d\u4ee4\u521b\u5efa\u4e86\u4e00\u4e2a <a rel=\"noreferrer noopener\" href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/docs.aws.amazon.com\/AmazonS3\/latest\/userguide\/Welcome.html\" target=\"_blank\">S3<\/a> bucket\uff0c\u5e76\u4e14\u628a <code>sam build<\/code> \u547d\u4ee4\u4ea7\u751f\u7684\u6587\u4ef6\u4e0a\u4f20\u81f3\u8fd9\u4e2a S3 bucket\u3002\u6700\u540e\u518d\u7528 <a rel=\"noreferrer noopener\" href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/docs.aws.amazon.com\/AWSCloudFormation\/latest\/UserGuide\/Welcome.html\" target=\"_blank\">Cloud Formation<\/a> \u90e8\u7f72\u5e94\u7528\u7a0b\u5e8f\u3002<br><code>sam deploy<\/code> \u4f1a\u8f93\u51fa URL \u4f9b\u4f60\u6d4b\u8bd5\uff0c\u4f60\u53ef\u4ee5\u7528 <code>curl<\/code> \u5de5\u5177\u5411 API \u53d1\u9001\u8bf7\u6c42\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">curl https:\/\/&lt;restapiid>.execute-api.us-east-1.amazonaws.com\/Prod\/hello\/<\/pre>\n\n\n\n<p>\u5982\u679c\u4f60\u7684 API \u90e8\u7f72\u6210\u529f\u7684\u8bdd\uff0c\u4f60\u5e94\u8be5\u80fd\u770b\u5230\u5982\u4e0b\u54cd\u5e94\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{\"message\": \"hello world\"}<\/pre>\n\n\n\n<p><strong>\u6b65\u9aa4 5\uff1a ( \u53ef\u9009 ) \u5728\u672c\u5730\u6d4b\u8bd5\u4f60\u7684 API<\/strong><\/p>\n\n\n\n<p>\u5f53\u4f60\u5728\u5f00\u53d1\u7684\u65f6\u5019\uff0c\u4f60\u901a\u5e38\u5e0c\u671b\u5728\u672c\u5730\u5c31\u80fd\u6d4b\u8bd5\u4f60\u7684 API\u3002AWS SAM CLI \u63d0\u4f9b\u4e86 <code>sam local<\/code> \u547d\u4ee4\uff0c\u8be5\u547d\u4ee4\u57fa\u4e8e Docker \u6765\u6a21\u62df Lambda \u73af\u5883\u3002\u5177\u4f53\u6765\u8bf4\u6709\u4e24\u79cd\u5728\u672c\u5730\u6d4b\u8bd5\u7684\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<ul><li>\u5728\u672c\u5730\u90e8\u7f72\u4f60\u7684 API<\/li><li>\u76f4\u63a5\u8c03\u7528 Lambda \u51fd\u6570<\/li><\/ul>\n\n\n\n<p>\u4e0b\u9762\u5206\u522b\u5c55\u793a\u8fd9\u4e24\u79cd\u65b9\u5f0f\u3002<\/p>\n\n\n\n<p><strong>\u5728\u672c\u5730\u90e8\u7f72 API<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sam local start-api<\/pre>\n\n\n\n<p>\u4f60\u4f1a\u5f97\u5230\u5982\u4e0b\u8f93\u51fa\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">2019-07-12 15:27:58 Mounting HelloWorldFunction at http:\/\/127.0.0.1:3000\/hello [GET]\n2019-07-12 15:27:58 You can now browse to the above endpoints to invoke your functions. You do not need to restart\/reload SAM CLI while working on your functions, changes will be reflected instantly\/automatically. You only need to restart SAM CLI if you update your AWS SAM template\n2019-07-12 15:27:58  * Running on http:\/\/127.0.0.1:3000\/ ( Press CTRL+C to quit )\n\nFetching lambci\/lambda: python3.7 Docker container image......................................................................................................................................................................................\n2019-07-12 15:28:56 Mounting \/&lt;working-development-path>\/sam-app\/.aws-sam\/build\/HelloWorldFunction as \/var\/task: ro,delegated inside runtime container\nSTART RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72 Version: $LATEST\nEND RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72\nREPORT RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72  Duration: 4.42 ms       Billed Duration: 100 ms Memory Size: 128 MB     Max Memory Used: 22 MB\n2019-07-12 15:28:58 No Content-Type given. Defaulting to 'application\/json'.\n2019-07-12 15:28:58 127.0.0.1 - - [12\/Jul\/2019 15:28:58] \"GET \/hello HTTP\/1.1\" 200 -\n\n<\/pre>\n\n\n\n<p>Docker \u4f1a\u82b1\u4e00\u70b9\u65f6\u95f4\u52a0\u8f7d\u955c\u50cf\u3002\u52a0\u8f7d\u5b8c\u6bd5\u4e4b\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u7528 <code>curl<\/code> \u5411\u672c\u5730\u90e8\u7f72\u7684 API \u53d1\u9001\u8bf7\u6c42\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">curl http:\/\/127.0.0.1:3000\/hello<\/pre>\n\n\n\n<p>\u4f60\u4f1a\u5f97\u5230\u5982\u4e0b\u8f93\u51fa\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">2019-07-12 15:29:57 Invoking app.lambda_handler ( python3.7 )\n2019-07-12 15:29:57 Found credentials in shared credentials file: ~\/.aws\/credentials\n\nFetching lambci\/lambda: python3.7 Docker container image......\n2019-07-12 15:29:58 Mounting \/&lt;working-development-path>\/sam-app\/.aws-sam\/build\/HelloWorldFunction as \/var\/task: ro,delegated inside runtime container\nSTART RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72 Version: $LATEST\nEND RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72\nREPORT RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72  Duration: 7.92 ms       Billed Duration: 100 ms Memory Size: 128 MB     Max Memory Used: 22 MB\n{\"statusCode\":200,\"body\":\"{\\\"message\\\": \\\"hello world\\\"}\"}<\/pre>\n\n\n\n<p><code>start-api<\/code> \u8fd9\u4e2a\u547d\u4ee4\u4f1a\u5728\u672c\u5730\u521b\u5efa\u4e00\u4e2a Docker \u5bb9\u5668\u4ee5\u6267\u884c Lambda \u51fd\u6570\uff0c\u770b\u8d77\u6765\u5c31\u50cf\u5728\u4e91\u7aef\u6267\u884c\u4e00\u6837\u3002<\/p>\n\n\n\n<p><strong>\u76f4\u63a5\u8c03\u7528 Lambda \u51fd\u6570<\/strong><\/p>\n\n\n\n<p>\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u5c31\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528 Lambda \u51fd\u6570\uff0c\u800c\u4e0d\u7528\u901a\u8fc7 HTTP \u7ec8\u7ed3\u70b9\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sam local invoke \"HelloWorldFunction\" -e events\/event.json<\/pre>\n\n\n\n<p>\u4f60\u4f1a\u5f97\u5230\u5982\u4e0b\u8f93\u51fa\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">2019-07-01 14:08:42 Found credentials in shared credentials file: ~\/.aws\/credentials\n2019-07-01 14:08:42 Invoking app.lambda_handler ( python3.7 )\n\nFetching lambci\/lambda: python3.7 Docker container image...............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................\n2019-07-01 14:09:39 Mounting \/&lt;working-development-path>\/sam-app\/.aws-sam\/build\/HelloWorldFunction as \/var\/task: ro,delegated inside runtime container\nSTART RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72 Version: $LATEST\nEND RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72\nREPORT RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72    Duration: 3.51 ms    Billed Duration: 100 ms    Memory Size: 128 MB    Max Memory Used: 22 MB\n{\"statusCode\":200,\"body\":\"{\\\"message\\\": \\\"hello world\\\"}\"}<\/pre>\n\n\n\n<p><code>invoke<\/code> \u547d\u4ee4\u76f4\u63a5\u8c03\u7528 Lambda \u51fd\u6570\uff0c\u5e76\u4e14\u80fd\u7528\u4f60\u5728 <code>event.json<\/code> \u6587\u4ef6\u91cc\u63d0\u4f9b\u7684\u6570\u636e\u4f5c\u4e3a\u8f93\u5165\u3002<\/p>\n\n\n\n<p><strong>\u6e05\u7406\u8d44\u6e90<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u7531\u4e8e AWS SAM \u57fa\u4e8e Cloud Formation \u521b\u5efa\u8d44\u6e90\uff0c\u6240\u4ee5\u5982\u679c\u4f60\u4e0d\u518d\u9700\u8981\u8fd9\u7bc7\u6587\u7ae0\u91cc\u521b\u5efa\u7684 API\uff0c\u4f60\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u6e05\u7406\u6389\u8fd9\u4e9b\u8d44\u6e90\uff0c\u4ee5\u514d\u4ea7\u751f\u989d\u5916\u8d39\u7528\u3002<\/p>\n\n\n\n<ol><li>\u6253\u5f00 Cloud Formation \u7684\u7ba1\u7406\u9875\u9762\uff1a<a href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/console.aws.amazon.com\/cloudformation\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/console.aws.amazon.com\/cloudformation<\/a>\u3002<\/li><li>\u5728\u5de6\u4fa7\u7684\u9762\u677f\u9009\u62e9 <strong>Stacks<\/strong>\u3002<\/li><li>\u5728 Stack \u5217\u8868\u91cc\u9009\u62e9 <strong>sam-app<\/strong> ( \u6216\u8005\u4f60\u521b\u5efa\u5de5\u7a0b\u65f6\u81ea\u5df1\u586b\u7684\u540d\u5b57 ) \u3002<\/li><li>\u9009\u62e9 Delete\u3002<\/li><\/ol>\n\n\n\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5c0f\u767d\uff1a\u539f\u6765\u4f7f\u7528 AWS SAM \u6784\u5efa\u65e0\u670d\u52a1\u5668 API \u8fd9\u4e48\u7b80\u5355\uff0c\u53ef\u4ee5\u8ba9\u8001\u677f\u6ee1\u610f\u4e86\u3002<br>\u5927\u795e\uff1a\u662f\u7684\uff0c\u65e0\u670d\u52a1\u5668\u67b6\u6784\u7684\u7279\u70b9\u662f\u5728\u8d44\u8d39\u548c\u6027\u80fd\u4e0a\u5747\u6709\u6781\u5927\u7684\u5f39\u6027\u3002\u5f53\u4ea7\u54c1\u521d\u671f\u6d41\u91cf\u8f83\u4f4e\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u51cf\u5c11 Lambda \u5b9e\u4f8b\u7684\u6570\u91cf\u964d\u4f4e\u6210\u672c\uff0c\u751a\u81f3\u5728\u95f2\u65f6\u91ca\u653e\u6240\u6709\u7684\u5b9e\u4f8b\u964d\u5230 0 \u8d44\u8d39\u3002\u5f53\u6d41\u91cf\u589e\u591a\u65f6\u4f1a\u81ea\u52a8\u521b\u5efa\u66f4\u591a Lambda \u5b9e\u4f8b\uff0c\u65e0\u9700\u989d\u5916\u5de5\u4f5c\u5373\u53ef\u5b9e\u73b0\u6269\u5bb9\u3002\u65e0\u670d\u52a1\u5668\u67b6\u6784\u9002\u5408\u4ea7\u54c1\u4ece 0 \u5230 1\uff0c\u4ee5\u53ca\u4ece 1 \u5230 100 \u7684\u6574\u4e2a\u751f\u547d\u5468\u671f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6545\u4e8b\u80cc\u666f \u8001\u677f\uff1a\u4eca\u5929\u7684\u4f1a\u8bae\u5c31\u5230\u8fd9\u91cc\u4e86\uff0c\u5c0f\u767d\uff0c\u8fd9\u4e2a API \u4f60\u6765\u5b9e\u73b0\u4e00\u4e0b\u5427\u3002\u4e0a\u7ebf\u521d\u671f\u7684\u6d41\u91cf\u6bd4\u8f83\u4f4e\uff0c\u4f60\u9009\u4e00\u4e2a\u65e2\u7701\u94b1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,9],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/10091"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10091"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/10091\/revisions"}],"predecessor-version":[{"id":10094,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/10091\/revisions\/10094"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}