├── .gitignore ├── LICENSE ├── README.md ├── SECURITY.md ├── event.json ├── handler.js ├── httpQueryString.json ├── package.json └── serverless.yml /.gitignore: -------------------------------------------------------------------------------- 1 | # Serverless 2 | .serverless 3 | .env 4 | functions 5 | tmp 6 | .coveralls.yml 7 | 8 | # Logs 9 | *.log 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 18 | .grunt 19 | 20 | # node-waf configuration 21 | .lock-wscript 22 | 23 | # Compiled binary addons (http://nodejs.org/api/addons.html) 24 | build/Release 25 | 26 | # Dependency directory 27 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 28 | node_modules 29 | 30 | # IDE 31 | **/.idea 32 | 33 | # OS 34 | .DS_Store 35 | .tmp 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Microsoft Corporation. All rights reserved. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Serverless Boilerplate - Azure Functions 2 | 3 | This is a quick boilerplate for getting started with the serverless-azure-functions plugin for the serverless framework. 4 | 5 | ## Getting started 6 | 7 | ### 1. Get a Serverless Service with Azure as the Provider 8 | 9 | 1. Recommend using Node v6.5.0 10 | 1. Install the serverless tooling - `npm i -g serverless` 11 | 1. Create boilerplate (change `my-app` to whatever you'd prefer): `serverless install --url https://github.com/Azure/boilerplate-azurefunctions --name my-app` 12 | 1. `cd my-app` 13 | 2. `npm install` 14 | 15 | ### 2. Set up credentials 16 | 17 | We'll set up an Azure Subscription and our service principal. You can learn more in the [credentials doc]( https://www.serverless.com/framework/docs/providers/azure/guide/credentials). 18 | 19 | 1. Set up an Azure Subscription 20 | 21 | Sign up for a free account @ [https://azure.com](https://azure.microsoft.com/en-us/services/functions/). 22 | 23 | Azure comes with a [free trial](https://azure.microsoft.com/en-us/free/) that includes $200 of free credit. 24 | 25 | 26 | 2. . Get the Azure CLI 27 | 28 | ``` 29 | npm i -g azure-cli 30 | ``` 31 | 32 | 3. Login to Azure 33 | 34 | ``` 35 | azure login 36 | ``` 37 | 38 | This will give you a code and prompt you to visit [aka.ms/devicelogin](https://aka.ms/devicelogin). Provide the code and then login with your Azure identity (this may happen automatically if you're already logged in). You'll then be able to access your account via the CLI. 39 | 40 | 4. Get your subcription and tenant id 41 | 42 | ``` 43 | azure account show 44 | ``` 45 | 46 | Save the subcription and tenant id for later 47 | 48 | 5. Create a service principal for a given `` and `` and add contributor role. 49 | 50 | ``` 51 | azure ad sp create -n -p 52 | ``` 53 | 54 | This should return an object which has the `servicePrincipalNames` property on it and an ObjectId. Save the Object Id and one of the names in the array and the password you provided for later. If you need to look up your service principal later, you can use `azure ad sp -c ` where `` is the name provided originally. Note that the `` you provided is not the name you'll provide later, it is a name in the `servicePrincipalNames` array. 55 | 56 | Then grant the SP contributor access with the ObjectId 57 | 58 | ```bash 59 | azure role assignment create --objectId -o Contributor 60 | ``` 61 | 62 | 6. Set up environment variables 63 | 64 | You need to set up environment variables for your subscription id, tenant id, service principal name, and password. 65 | 66 | ```bash 67 | # bash 68 | export azureSubId='' 69 | export azureServicePrincipalTenantId='' 70 | export azureServicePrincipalClientId='' 71 | export azureServicePrincipalPassword='' 72 | ``` 73 | 74 | ```powershell 75 | # PowerShell 76 | $env:azureSubId='' 77 | $env:azureServicePrincipalTenantId='' 78 | $env:azureServicePrincipalClientId='' 79 | $env:azureServicePrincipalPassword='' 80 | ``` 81 | 82 | 83 | ### 3. Update the config in `serverless.yml` 84 | 85 | Open up your `serverless.yml` file and update the following information: 86 | 87 | ```yml 88 | service: my-azure-functions-app # Name of the Azure function App you want to create 89 | ``` 90 | 91 | ### 4. Deploy, test, and remove your service 92 | 93 | 1. **Deploy a Service:** 94 | 95 | Use this when you have made changes to your Functions or you simply want to deploy all changes within your Service at the same time. 96 | ```bash 97 | serverless deploy 98 | ``` 99 | 100 | 2. **Deploy the Function:** 101 | 102 | Use this to quickly upload and overwrite your Azure function, allowing you to develop faster. 103 | ```bash 104 | serverless deploy function -f httpjs 105 | ``` 106 | 107 | 3. **Invoke the Function:** 108 | 109 | Invokes an Azure Function on Azure 110 | ```bash 111 | serverless invoke --path httpQueryString.json -f httpjs 112 | ``` 113 | 114 | 4. **Stream the Function Logs:** 115 | 116 | Open up a separate tab in your console and stream all logs for a specific Function using this command. 117 | ```bash 118 | serverless logs -f httpjs -t 119 | ``` 120 | 121 | 5. **Remove the Service: (optional)** 122 | 123 | Removes all Functions and Resources from your Azure subscription. 124 | ```bash 125 | serverless remove 126 | ``` 127 | 128 | ### Contributing 129 | 130 | Please create issues in this repo for any problems or questions you find. Before sending a PR for any major changes, please create an issue to discuss. 131 | 132 | Please follow the [Microsoft Code of Conduct](https://opensource.microsoft.com/codeofconduct/) when working with everyone in this project. 133 | 134 | - **Be friendly and patient**: Remember you might not be communicating in someone else's primary spoken or programming language, and others may not have your level of understanding. 135 | - **Be welcoming**: Our communities welcome and support people of all backgrounds and identities. This includes, but is not limited to members of any race, ethnicity, culture, national origin, color, immigration status, social and economic class, educational level, sex, sexual orientation, gender identity and expression, age, size, family status, political belief, religion, and mental and physical ability. 136 | - **Be respectful**: We are a world-wide community of professionals, and we conduct ourselves professionally. Disagreement is no excuse for poor behavior and poor manners. Disrespectful and unacceptable behavior includes, but is not limited to: 137 | - Violent threats or language. 138 | - Discriminatory or derogatory jokes and language. 139 | - Posting sexually explicit or violent material. 140 | - Posting, or threatening to post, people's personally identifying information ("doxing"). 141 | - Insults, especially those using discriminatory terms or slurs. 142 | - Behavior that could be perceived as sexual attention. 143 | - Advocating for or encouraging any of the above behaviors. 144 | - **Understand disagreements**: Disagreements, both social and technical, are useful learning opportunities. Seek to understand the other viewpoints and resolve differences constructively. 145 | This code is not exhaustive or complete. It serves to capture our common understanding of a productive, collaborative environment. We expect the code to be followed in spirit as much as in the letter. 146 | 147 | ## License 148 | 149 | [MIT](LICENSE) 150 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## Security 4 | 5 | Microsoft takes the security of our software products and services seriously, which includes all source code repositories managed through our GitHub organizations, which include [Microsoft](https://github.com/microsoft), [Azure](https://github.com/Azure), [DotNet](https://github.com/dotnet), [AspNet](https://github.com/aspnet), [Xamarin](https://github.com/xamarin), and [our GitHub organizations](https://opensource.microsoft.com/). 6 | 7 | If you believe you have found a security vulnerability in any Microsoft-owned repository that meets [Microsoft's definition of a security vulnerability](https://aka.ms/opensource/security/definition), please report it to us as described below. 8 | 9 | ## Reporting Security Issues 10 | 11 | **Please do not report security vulnerabilities through public GitHub issues.** 12 | 13 | Instead, please report them to the Microsoft Security Response Center (MSRC) at [https://msrc.microsoft.com/create-report](https://aka.ms/opensource/security/create-report). 14 | 15 | If you prefer to submit without logging in, send email to [secure@microsoft.com](mailto:secure@microsoft.com). If possible, encrypt your message with our PGP key; please download it from the [Microsoft Security Response Center PGP Key page](https://aka.ms/opensource/security/pgpkey). 16 | 17 | You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Additional information can be found at [microsoft.com/msrc](https://aka.ms/opensource/security/msrc). 18 | 19 | Please include the requested information listed below (as much as you can provide) to help us better understand the nature and scope of the possible issue: 20 | 21 | * Type of issue (e.g. buffer overflow, SQL injection, cross-site scripting, etc.) 22 | * Full paths of source file(s) related to the manifestation of the issue 23 | * The location of the affected source code (tag/branch/commit or direct URL) 24 | * Any special configuration required to reproduce the issue 25 | * Step-by-step instructions to reproduce the issue 26 | * Proof-of-concept or exploit code (if possible) 27 | * Impact of the issue, including how an attacker might exploit the issue 28 | 29 | This information will help us triage your report more quickly. 30 | 31 | If you are reporting for a bug bounty, more complete reports can contribute to a higher bounty award. Please visit our [Microsoft Bug Bounty Program](https://aka.ms/opensource/security/bounty) page for more details about our active programs. 32 | 33 | ## Preferred Languages 34 | 35 | We prefer all communications to be in English. 36 | 37 | ## Policy 38 | 39 | Microsoft follows the principle of [Coordinated Vulnerability Disclosure](https://aka.ms/opensource/security/cvd). 40 | 41 | 42 | -------------------------------------------------------------------------------- /event.json: -------------------------------------------------------------------------------- 1 | { 2 | "input": "Hello from Serverless" 3 | } -------------------------------------------------------------------------------- /handler.js: -------------------------------------------------------------------------------- 1 | module.exports.hello = function (context, req) { 2 | context.log('JavaScript HTTP trigger function processed a request.'); 3 | 4 | if (req.query.name || (req.body && req.body.name)) { 5 | res = { 6 | // status: 200, /* Defaults to 200 */ 7 | body: "Hello " + (req.query.name || req.body.name) 8 | }; 9 | } 10 | else { 11 | res = { 12 | status: 400, 13 | body: "Please pass a name on the query string or in the request body" 14 | }; 15 | } 16 | context.done(null, res); 17 | }; 18 | 19 | module.exports.helloQueue = function (context, queueItem) { 20 | context.log('JavaScript queue trigger function processed work item', queueItem); 21 | context.bindings.blobOut = { 22 | "hello": "world" 23 | } 24 | context.done(); 25 | }; -------------------------------------------------------------------------------- /httpQueryString.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "pragna" 3 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "boilerplate-azurefunctions", 3 | "version": "0.1.0", 4 | "description": "A boilerplate to get started using the serverless-azure-functions plugin for serverless framework", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "serverless.com", 10 | "license": "MIT", 11 | "devDependencies": { 12 | "serverless-azure-functions": "*" 13 | } 14 | } -------------------------------------------------------------------------------- /serverless.yml: -------------------------------------------------------------------------------- 1 | service: azure-functions-app1234 2 | 3 | provider: 4 | name: azure 5 | location: West US 6 | #armTemplate: 7 | #file: YourARMTemplate.json 8 | #parameters: 9 | #VariableNameToUserInArmTemplate: Value 10 | 11 | plugins: 12 | - serverless-azure-functions 13 | 14 | package: 15 | exclude: 16 | - node_modules/** 17 | - .gitignore 18 | - package.json 19 | - .git/** 20 | 21 | functions: 22 | httpjs: 23 | handler: handler.hello 24 | events: 25 | - http: true 26 | x-azure-settings: 27 | authLevel : anonymous 28 | 29 | queuejs: 30 | handler: handler.helloQueue 31 | events: 32 | - queue: SampleQueue 33 | x-azure-settings: 34 | name: queueItem 35 | connection : AzureWebJobsStorage 36 | - blob: 37 | x-azure-settings: 38 | name: blobOut 39 | direction: out 40 | connection : AzureWebJobsStorage 41 | 42 | --------------------------------------------------------------------------------