我有一个简单的项目。基本设置就像这样......
project
|- etc
| |- sam.yaml
| \- swagger.yaml
|- src
| |- client
| | \- Client files (VUE frontend)
| \- backend
| |- index.js
| \- package.json
\- gulpfile.js
我运行gulp,最后得到一个build
文件夹。它看起来像这样......
build
|- client
| |- index.html
| \- More static client files
|- backend
| |- index.js
| |- package.json
| \_ node_modules
| \- Lots of stuff
\- backend.zip
构建效果很好。我希望在本地运行它,而不是每次进行更改时将ZIP上传到lambda,将客户端目录上传到S3。
为此,我正在运行aws-sam-local。是的,我已安装并运行docker
。从项目文件夹我运行...
sam local start-api -t etc/sam.yaml -s ../build/client/
现在我可以访问我的浏览器并访问http://localhost:3000/查看内容。正如预期的那样,我的静态文件(index.html)加载得很好。但是当我尝试访问API路由http://localhost:3000/test时,我得到了404。
我的sam.yaml
---
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: AWS SAM template with API defined in an external Swagger file along with Lambda integrations and CORS configurations
Resources:
ApiGatewayApi:
Type: AWS::Serverless::Api
Properties:
DefinitionUri: ../etc/swagger.yaml
StageName: Prod
Variables:
LambdaFunctionName: !Ref LambdaFunction
LambdaFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ../build/backend/
Handler: index.handler
Runtime: nodejs6.10
Events:
ProxyApiRoot:
Type: Api
Properties:
RestApiId: !Ref ApiGatewayApi
Path: /
Method: ANY
ProxyApiGreedy:
Type: Api
Properties:
RestApiId: !Ref ApiGatewayApi
Path: /{proxy+}
Method: ANY
Outputs:
ApiUrl:
Description: URL of your API endpoint
Value: !Join
- ''
- - https://
- !Ref ApiGatewayApi
- '.execute-api.'
- !Ref 'AWS::Region'
- '.amazonaws.com/Prod'
我的swagger.yaml
---
swagger: 2.0
basePath: /prod
info:
title: HACC
schemes:
- https
paths:
/:
x-amazon-apigateway-any-method:
produces:
- application/json
responses:
200:
description: 200 response
schema:
$ref: "#/definitions/Empty"
x-amazon-apigateway-integration:
responses:
default:
statusCode: 200
uri: arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:<<accountId>>:function:${stageVariables.LambdaFunctionName}/invocations
passthroughBehavior: when_no_match
httpMethod: POST
type: aws_proxy
options:
consumes:
- application/json
produces:
- application/json
responses:
200:
description: 200 response
schema:
$ref: "#/definitions/Empty"
headers:
Access-Control-Allow-Origin:
type: string
Access-Control-Allow-Methods:
type: string
Access-Control-Allow-Headers:
type: string
x-amazon-apigateway-integration:
responses:
default:
statusCode: 200
responseParameters:
method.response.header.Access-Control-Allow-Methods: "'DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT'"
method.response.header.Access-Control-Allow-Headers: "'Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token'"
method.response.header.Access-Control-Allow-Origin: "'*'"
passthroughBehavior: when_no_match
requestTemplates:
application/json: "{\"statusCode\": 200}"
type: mock
/{proxy+}:
x-amazon-apigateway-any-method:
x-amazon-apigateway-auth:
type: aws_iam
produces:
- application/json
parameters:
- name: proxy
in: path
required: true
type: string
responses: {}
x-amazon-apigateway-integration:
uri: arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:<<accountId>>:function:${stageVariables.LambdaFunctionName}/invocations
httpMethod: POST
type: aws_proxy
options:
consumes:
- application/json
produces:
- application/json
responses:
200:
description: 200 response
schema:
$ref: "#/definitions/Empty"
headers:
Access-Control-Allow-Origin:
type: string
Access-Control-Allow-Methods:
type: string
Access-Control-Allow-Headers:
type: string
x-amazon-apigateway-integration:
responses:
default:
statusCode: 200
responseParameters:
method.response.header.Access-Control-Allow-Methods: "'DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT'"
method.response.header.Access-Control-Allow-Headers: "'Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token'"
method.response.header.Access-Control-Allow-Origin: "'*'"
passthroughBehavior: when_no_match
requestTemplates:
application/json: "{\"statusCode\": 200}"
type: mock
definitions:
Empty:
type: object
title: Empty Schema
如何让aws-sam-local运行我的lambda API?