Skip to content

Commit 9cd7f4d

Browse files
committed
basic implemenation working
1 parent 6e354ad commit 9cd7f4d

File tree

4 files changed

+66
-67
lines changed

4 files changed

+66
-67
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ build/
1919
.env*
2020
env.js
2121
package-lock.json
22+
test

README.md

+53-11
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,29 @@ $ npm install -g @serverless/components
2020

2121
### 2. Create
2222

23-
Just create a `serverless.yml` file
2423

25-
```shell
26-
$ touch serverless.yml
27-
$ touch .env # your development AWS api keys
28-
$ touch .env.prod # your production AWS api keys
24+
```console
25+
$ mkdir my-function && cd my-function
26+
```
27+
28+
the directory should look something like this:
29+
30+
31+
```
32+
|- code
33+
|- handler.js
34+
|- package.json # optional
35+
|- serverless.yml
36+
|- .env # your development AWS api keys
37+
|- .env.prod # your production AWS api keys
38+
```
39+
40+
```js
41+
// handler.js
42+
module.exports.hello = async (event, context, cb) => {
43+
return { hello: 'world' }
44+
}
45+
2946
```
3047

3148
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.
@@ -35,6 +52,7 @@ AWS_ACCESS_KEY_ID=XXX
3552
AWS_SECRET_ACCESS_KEY=XXX
3653
```
3754

55+
3856
### 3. Configure
3957

4058
```yml
@@ -44,17 +62,41 @@ myFunction:
4462
component: '@serverless/function'
4563
inputs:
4664
name: my-function
47-
code: ./index.handler
48-
path: /invoke
49-
method: get
50-
permissions:
51-
- dynamodb?read
65+
description: My Serverless Function
66+
memory: 128
67+
timeout: 20
68+
code: ./code
69+
handler: handler.hello
70+
runtime: nodejs8.10
71+
env:
72+
TABLE_NAME: my-table
73+
region: us-east-1
5274
```
5375
5476
### 4. Deploy
5577
5678
```shell
57-
$ components
79+
function (master)$ components
80+
81+
myFunction › outputs:
82+
name: 'serverless-function'
83+
description: 'AWS Lambda Component'
84+
memory: 512
85+
timeout: 10
86+
code: './code'
87+
bucket: undefined
88+
shims: []
89+
handler: 'index.hello'
90+
runtime: 'nodejs8.10'
91+
env:
92+
role:
93+
arn: 'arn:aws:iam::552750238299:role/serverless-function'
94+
arn: 'arn:aws:lambda:us-east-1:552750238299:function:serverless-function'
95+
96+
97+
21s › dev › serverless-function › done
98+
99+
function (master)$
58100
```
59101

60102
 

package.json

+1-6
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,8 @@
99
"author": "Serverless, Inc.",
1010
"license": "Apache",
1111
"dependencies": {
12-
"@serverless/aws-api-gateway": "^0.1.0",
13-
"@serverless/aws-iam-role": "^0.1.0",
1412
"@serverless/aws-lambda": "^0.1.0",
15-
"@serverless/components": "^0.1.0",
16-
"aws-sdk": "^2.442.0",
17-
"ramda": "^0.26.1",
18-
"shortid": "^2.2.14"
13+
"@serverless/components": "^0.1.0"
1914
},
2015
"devDependencies": {
2116
"babel-eslint": "9.0.0",

serverless.js

+11-50
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
const path = require('path')
2-
const shortid = require('shortid')
3-
const aws = require('aws-sdk')
41
const { Component } = require('@serverless/components')
5-
const { mergeDeepRight } = require('ramda')
6-
const { deployAwsLambda, removeAwsLambda } = require('./lib')
72

83
/**
94
* Function Component
@@ -18,43 +13,15 @@ class Function extends Component {
1813
*/
1914

2015
async default(inputs = {}) {
21-
this.cli.status('Running')
16+
this.cli.status('Deploying')
2217

23-
// Defaults
24-
const defaults = {}
25-
defaults.code = `./code/index.handler`
26-
defaults.description = `A function deployed with Serverless Components`
27-
defaults.env = {}
28-
defaults.cors = true
18+
const lambda = await this.load('@serverless/aws-lambda')
2919

30-
if (this.state.inputs && this.state.inputs.name) {
31-
defaults.name = this.state.inputs.name
32-
} else {
33-
defaults.name = `function-${shortid.generate().replace(/[_-]/g, '')}`
34-
}
20+
const lambdaOutputs = await lambda(inputs)
3521

36-
// Merge inputs w/ defaults
37-
inputs = mergeDeepRight(defaults, inputs)
22+
this.cli.outputs(lambdaOutputs)
3823

39-
// Save inputs to state
40-
this.state.inputs = inputs
41-
await this.save()
42-
43-
// Declare Outputs
44-
let outputs = {}
45-
outputs.name = inputs.name
46-
47-
// Deploy
48-
if (!inputs.compute || !inputs.compute.type || inputs.compute.type === 'aws-lambda') {
49-
const fnOutputs = await deployAwsLambda(this, inputs)
50-
outputs = mergeDeepRight(outputs, fnOutputs)
51-
} else {
52-
throw new Error(`Sorry compute.type ${inputs.compute.type} is not yet supported.`)
53-
}
54-
55-
this.cli.outputs(outputs)
56-
57-
return outputs
24+
return lambdaOutputs
5825
}
5926

6027
/**
@@ -64,21 +31,15 @@ class Function extends Component {
6431
*/
6532

6633
async remove(inputs = {}) {
67-
// Status
6834
this.cli.status('Removing')
6935

70-
// Remove
71-
let outputs
72-
if (!inputs.compute || !inputs.compute.type || inputs.compute.type === 'aws-lambda') {
73-
outputs = await removeAwsLambda(this)
74-
} else {
75-
throw new Error(`Sorry compute.type ${inputs.compute.type} is not yet supported.`)
76-
}
36+
const lambda = await this.load('@serverless/aws-lambda')
37+
38+
const lambdaOutputs = await lambda.remove(inputs)
39+
40+
this.cli.outputs(lambdaOutputs)
7741

78-
// Clear state
79-
this.state = {}
80-
await this.save()
81-
return {}
42+
return lambdaOutputs
8243
}
8344
}
8445

0 commit comments

Comments
 (0)