私は簡単なプロジェクトを持っています。基本設定はこんな感じ…
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 をラムダにアップロードし、クライアント ディレクトリを 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を取得してラムダ API を実行するにはどうすればよいですか?