@serverless/aws-api-gateway
Advanced tools
Comparing version 1.0.1 to 1.1.0
{ | ||
"name": "@serverless/aws-api-gateway", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"main": "./serverless.js", | ||
@@ -5,0 +5,0 @@ "publishConfig": { |
@@ -29,3 +29,3 @@ # aws-api-gateway | ||
```shell | ||
$ npm install -g @serverless/components | ||
$ npm install -g serverless | ||
``` | ||
@@ -40,6 +40,11 @@ | ||
$ touch index.js # your lambda code | ||
$ touch .env # your development AWS api keys | ||
$ touch .env.prod # your production AWS api keys | ||
$ touch .env # your AWS api keys | ||
``` | ||
``` | ||
# .env | ||
AWS_ACCESS_KEY_ID=XXX | ||
AWS_SECRET_ACCESS_KEY=XXX | ||
``` | ||
the `index.js` file should look something like this: | ||
@@ -82,9 +87,2 @@ | ||
the `.env` files are not required if you have the aws keys set globally and you want to use a single stage, but they should look like this. | ||
``` | ||
AWS_ACCESS_KEY_ID=XXX | ||
AWS_SECRET_ACCESS_KEY=XXX | ||
``` | ||
Keep reading for info on how to set up the `serverless.yml` file. | ||
@@ -96,3 +94,3 @@ | ||
#### Creating REST APIs | ||
You can create new REST APIs by specifying the endpoints you'd like to create, and optionally passing a name and description for your new REST API. | ||
You can create new REST APIs by specifying the endpoints you'd like to create, and optionally passing a name and description for your new REST API. You may also choose between a lambda proxy or http proxy integration by using the function or proxyURI field respectively. The function field will override the proxyURI field. | ||
@@ -102,8 +100,5 @@ ```yml | ||
name: rest-api | ||
createUser: | ||
component: "@serverless/aws-lambda" | ||
inputs: | ||
name: ${name}-create-user | ||
code: ./code | ||
@@ -114,3 +109,2 @@ handler: index.createUser | ||
inputs: | ||
name: ${name}-get-users | ||
code: ./code | ||
@@ -121,3 +115,2 @@ handler: index.getUsers | ||
inputs: | ||
name: ${name}-auth | ||
code: ./code | ||
@@ -129,3 +122,2 @@ handler: index.auth | ||
inputs: | ||
name: ${name} | ||
description: Serverless REST API | ||
@@ -135,8 +127,12 @@ endpoints: | ||
method: POST | ||
function: ${comp:createUser.arn} | ||
authorizer: ${comp:auth.arn} | ||
function: ${createUser.arn} | ||
authorizer: ${auth.arn} | ||
- path: /users | ||
method: GET | ||
function: ${comp:getUsers.arn} | ||
authorizer: ${comp:auth.arn} | ||
function: ${getUsers.arn} | ||
authorizer: ${auth.arn} | ||
- path: /users | ||
method: PUT | ||
proxyURI: https://example.com/users | ||
authorizer: ${auth.arn} | ||
``` | ||
@@ -150,8 +146,5 @@ | ||
name: rest-api | ||
createUser: | ||
component: "@serverless/aws-lambda" | ||
inputs: | ||
name: ${name}-create-user | ||
code: ./code | ||
@@ -162,3 +155,2 @@ handler: index.createUser | ||
inputs: | ||
name: ${name}-get-users | ||
code: ./code | ||
@@ -174,6 +166,6 @@ handler: index.getUsers | ||
method: POST | ||
function: ${comp:createUser.arn} | ||
function: ${createUser.arn} | ||
- path: /users | ||
method: GET | ||
function: ${comp:getUsers.arn} | ||
function: ${getUsers.arn} | ||
``` | ||
@@ -184,26 +176,3 @@ | ||
```shell | ||
api (master)$ components | ||
myApig › outputs: | ||
id: 'e4asreichk' | ||
endpoints: [ { path: '/users', | ||
method: 'POST', | ||
function: | ||
'arn:aws:lambda:us-east-1:552750238291:function:rest-api-create-user', | ||
url: | ||
'https://e4asreichk.execute-api.us-east-1.amazonaws.com/dev/users', | ||
id: 'jkgqlqjnf2' }, | ||
{ path: '/users', | ||
method: 'GET', | ||
function: | ||
'arn:aws:lambda:us-east-1:552750238291:function:rest-api-get-users', | ||
url: | ||
'https://e4asreichk.execute-api.us-east-1.amazonaws.com/dev/users', | ||
id: 'h7zh3r' } ] | ||
38s › dev › rest-api › done | ||
api (master)$ | ||
$ serverless | ||
``` | ||
@@ -210,0 +179,0 @@ |
44
utils.js
@@ -249,6 +249,11 @@ const { utils } = require('@serverless/core') | ||
const createIntegration = async ({ apig, lambda, apiId, endpoint }) => { | ||
const functionName = endpoint.function.split(':')[6] | ||
const accountId = endpoint.function.split(':')[4] | ||
const region = endpoint.function.split(':')[3] // todo what if the lambda in another region? | ||
const isLambda = !!endpoint.function | ||
let functionName, accountId, region | ||
if (isLambda) { | ||
functionName = endpoint.function.split(':')[6] | ||
accountId = endpoint.function.split(':')[4] | ||
region = endpoint.function.split(':')[3] // todo what if the lambda in another region? | ||
} | ||
const integrationParams = { | ||
@@ -258,5 +263,7 @@ httpMethod: endpoint.method, | ||
restApiId: apiId, | ||
type: 'AWS_PROXY', | ||
type: isLambda ? 'AWS_PROXY' : 'HTTP_PROXY', | ||
integrationHttpMethod: 'POST', | ||
uri: `arn:aws:apigateway:${region}:lambda:path/2015-03-31/functions/${endpoint.function}/invocations` | ||
uri: (isLambda | ||
? `arn:aws:apigateway:${region}:lambda:path/2015-03-31/functions/${endpoint.function}/invocations` | ||
: endpoint.proxyURI) | ||
} | ||
@@ -277,15 +284,18 @@ | ||
const permissionsParams = { | ||
Action: 'lambda:InvokeFunction', | ||
FunctionName: functionName, | ||
Principal: 'apigateway.amazonaws.com', | ||
SourceArn: `arn:aws:execute-api:${region}:${accountId}:${apiId}/*/*`, | ||
StatementId: `${functionName}-${apiId}` | ||
} | ||
// Create lambda trigger for AWS_PROXY endpoints | ||
if (isLambda) { | ||
const permissionsParams = { | ||
Action: 'lambda:InvokeFunction', | ||
FunctionName: functionName, | ||
Principal: 'apigateway.amazonaws.com', | ||
SourceArn: `arn:aws:execute-api:${region}:${accountId}:${apiId}/*/*`, | ||
StatementId: `${functionName}-${apiId}` | ||
} | ||
try { | ||
await lambda.addPermission(permissionsParams).promise() | ||
} catch (e) { | ||
if (e.code !== 'ResourceConflictException') { | ||
throw Error(e) | ||
try { | ||
await lambda.addPermission(permissionsParams).promise() | ||
} catch (e) { | ||
if (e.code !== 'ResourceConflictException') { | ||
throw Error(e) | ||
} | ||
} | ||
@@ -292,0 +302,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
658
40565
172