Last active
January 13, 2019 16:58
-
-
Save oxlb/616a4de2d90cb2afd0e62a16a35da54d to your computer and use it in GitHub Desktop.
Serverless framework env conf example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| const connection = { | |
| client: 'mysql', | |
| connection: { | |
| host : process.env.HOST, | |
| user : process.env.USER, | |
| password : process.env.PASSWORD, | |
| database : process.env.DATABASE | |
| }, | |
| pool: { min: 1, max: 10 }, | |
| debug: true | |
| }; | |
| const knex = require('knex')(connection); | |
| const TABLE_TODO = 'todo'; | |
| //## Create TODO Table | |
| module.exports.createTodoTable = async (event, context) => { | |
| if(await knex.schema.hasTable(TABLE_TODO)){ | |
| return response(403, 'Table '+ TABLE_TODO+ ' already exist'); | |
| } | |
| const table = await knex.schema.createTable(TABLE_TODO, function(table) { | |
| table.increments('id').primary(); | |
| table.string('text'); | |
| table.dateTime("created_at").defaultTo(knex.fn.now()); | |
| table.dateTime("updated_at"); | |
| table.dateTime("deleted_at"); | |
| }); | |
| return response(200,table); | |
| }; | |
| //## Insert value into TODO table | |
| module.exports.addToTodoTable = async (event, context) => { | |
| const body = (process.env.ENV === 'local') ? event.body : JSON.parse(event.body); | |
| if(!await knex.schema.hasTable(TABLE_TODO)){ | |
| return response(403, 'Table '+ TABLE_TODO+ ' not exist'); | |
| } | |
| const table = await knex(TABLE_TODO).insert([body]) | |
| return response(200,table); | |
| }; | |
| function response(statusCode, body) { | |
| return { | |
| statusCode: statusCode, | |
| body: JSON.stringify(body), | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #### Serverless framework environment configuration example | |
| dev: | |
| ENV: 'dev' | |
| HOST: 'your host here' | |
| USER: 'your user here' | |
| PASSWORD: 'your password here' | |
| DATABASE: 'your database here' | |
| prod: | |
| ENV: 'prod' | |
| HOST: 'your host here' | |
| USER: 'your user here' | |
| PASSWORD: 'your password here' | |
| DATABASE: 'your database here' | |
| local: | |
| ENV: 'local' | |
| HOST: 'your host here' | |
| USER: 'your user here' | |
| PASSWORD: 'your password here' | |
| DATABASE: 'your database here' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| service: sls-msql | |
| provider: | |
| name: aws | |
| runtime: nodejs8.10 | |
| region: us-east-2 | |
| stage: dev | |
| environment: | |
| ENV: ${file(./serverless.env.yml):${opt:stage, self:provider.stage}.ENV} | |
| HOST: ${file(./serverless.env.yml):${opt:stage, self:provider.stage}.HOST} | |
| USER: ${file(./serverless.env.yml):${opt:stage, self:provider.stage}.USER} | |
| PASSWORD: ${file(./serverless.env.yml):${opt:stage, self:provider.stage}.PASSWORD} | |
| DATABASE: ${file(./serverless.env.yml):${opt:stage, self:provider.stage}.DATABASE} | |
| vpc: | |
| securityGroupIds: | |
| - securityGroupId1 | |
| subnetIds: | |
| - subnetId1 | |
| - subnetId2 | |
| functions: | |
| createTodoTable: | |
| handler: handler.createTodoTable | |
| ## serverless invoke local -f addToTodoTable --data '{ "queryStringParameters": {"text":"abcd"}}' | |
| addToTodoTable: | |
| handler: handler.addToTodoTable | |
| events: | |
| - http: | |
| path: todo | |
| method: post |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment