commit 86effe60a52c72ae7a260e3f075c5fe09c75e951 Author: Andrew Tomaka Date: Tue Aug 1 22:29:26 2017 -0400 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..094e431 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +**/build/** diff --git a/1-business-open/index.js b/1-business-open/index.js new file mode 100644 index 0000000..6230d15 --- /dev/null +++ b/1-business-open/index.js @@ -0,0 +1,19 @@ +exports.index = function(event, context, callback) { + if (isBusinessTime(new Date())) { + callback(null, { + statusCode: '200', + body: JSON.stringify({ message: "It's time for business" }), + headers: { 'Content-Type': 'application/json' } + }) + } else { + callback(null, { + statusCode: '200', + body: JSON.stringify({ message: "It's party time" }), + headers: { 'Content-Type': 'application/json' } + }) + } +} + +function isBusinessTime(now) { + return now.getDay() <= 4 && now.getHours() >= 9 && now.getHours() < 17 +} diff --git a/2-business-open_shell/Makefile b/2-business-open_shell/Makefile new file mode 100644 index 0000000..ec58c69 --- /dev/null +++ b/2-business-open_shell/Makefile @@ -0,0 +1,28 @@ +bucket = "examples.atomaka.com" # your bucket that exists in us-east-2 + +# DO NOT EDIT BELOW HERE +key = "business-hours.zip" +yarnbin = $(shell yarn bin) + +deploy: upload + AWS_DEFAULT_REGION=us-east-2 aws lambda update-function-code \ + --function-name business-hours \ + --s3-bucket $(bucket) \ + --s3-key $(key) + +upload: build + AWS_DEFAULT_REGION=us-east-2 aws s3 cp \ + build/business-hours.zip s3://$(bucket)/$(key) + +build: clean + mkdir build/ + cp index.js build/ + cd build/ \ + && zip -q -r $(key) . \ + && cd ../ + +clean: + rm -rf build/ + +setup: build + bash setup.sh diff --git a/2-business-open_shell/index.js b/2-business-open_shell/index.js new file mode 100644 index 0000000..6230d15 --- /dev/null +++ b/2-business-open_shell/index.js @@ -0,0 +1,19 @@ +exports.index = function(event, context, callback) { + if (isBusinessTime(new Date())) { + callback(null, { + statusCode: '200', + body: JSON.stringify({ message: "It's time for business" }), + headers: { 'Content-Type': 'application/json' } + }) + } else { + callback(null, { + statusCode: '200', + body: JSON.stringify({ message: "It's party time" }), + headers: { 'Content-Type': 'application/json' } + }) + } +} + +function isBusinessTime(now) { + return now.getDay() <= 4 && now.getHours() >= 9 && now.getHours() < 17 +} diff --git a/2-business-open_shell/resources/log-policy.json b/2-business-open_shell/resources/log-policy.json new file mode 100644 index 0000000..ed7141b --- /dev/null +++ b/2-business-open_shell/resources/log-policy.json @@ -0,0 +1,14 @@ +{ + "Version": "2012-10-17", + "Statement": [ + { + "Sid": "", + "Effect": "Allow", + "Principal": { + "Service": "apigateway.amazonaws.com" + }, + "Action": "sts:AssumeRole" + } + ] +} + diff --git a/2-business-open_shell/resources/role-policy.json b/2-business-open_shell/resources/role-policy.json new file mode 100644 index 0000000..8c90e24 --- /dev/null +++ b/2-business-open_shell/resources/role-policy.json @@ -0,0 +1,8 @@ +{ + "Version": "2012-10-17", + "Statement": { + "Effect": "Allow", + "Principal": {"Service": "lambda.amazonaws.com"}, + "Action": "sts:AssumeRole" + } +} diff --git a/2-business-open_shell/setup.sh b/2-business-open_shell/setup.sh new file mode 100644 index 0000000..b4b9729 --- /dev/null +++ b/2-business-open_shell/setup.sh @@ -0,0 +1,72 @@ +#!/bin/bash +set -x + +role=$(AWS_DEFAULT_REGION=us-east-2 aws iam create-role \ + --path /examples/ \ + --role-name business-hours-lambda \ + --assume-role-policy-document file://resources/role-policy.json \ + | jq '.Role.Arn' \ + | tr -d '"') +AWS_DEFAULT_REGION=us-east-2 aws iam create-role \ + --path /examples/ \ + --role-name log-business-hours-lambda \ + --assume-role-policy-document file://resources/log-policy.json + +echo sleeping 10 +sleep 10 + +lambda=$(AWS_DEFAULT_REGION=us-east-2 aws lambda create-function \ + --function-name business-hours \ + --runtime nodejs6.10 \ + --role $role \ + --handler index.index \ + --zip-file fileb://build/business-hours.zip \ + | jq '.FunctionArn' \ + | tr -d '"') + +echo sleeping 5 +sleep 5 + +api=$(aws apigateway create-rest-api \ + --name 'Shell Business Hours' \ + --region us-east-2 \ + | jq '.id' \ + | tr -d '"') + +echo sleeping 5 +sleep 5 + +resource=$(aws apigateway get-resources \ + --rest-api-id $api \ + --region us-east-2 \ + | jq '.items[0].id' \ + | tr -d '"') + +echo sleeping 5 +sleep 5 + +aws apigateway put-method \ + --rest-api-id $api \ + --resource-id $resource \ + --http-method GET \ + --authorization-type "NONE" \ + --region us-east-2 + +echo sleeping 5 +sleep 5 + +aws apigateway put-integration \ + --rest-api-id $api \ + --resource-id $resource \ + --http-method GET \ + --type AWS \ + --integration-http-method GET \ + --uri arn:aws:apigateway:us-east-2:lambda:path/2015-03-31/functions/$lambda/invocations + +echo sleeping 5 +sleep 5 + +api=l271anov3c +aws apigateway create-deployment \ + --rest-api-id $api \ + --stage-name prod diff --git a/3-business-open-serverless/.gitignore b/3-business-open-serverless/.gitignore new file mode 100644 index 0000000..2b48c8b --- /dev/null +++ b/3-business-open-serverless/.gitignore @@ -0,0 +1,6 @@ +# package directories +node_modules +jspm_packages + +# Serverless directories +.serverless \ No newline at end of file diff --git a/3-business-open-serverless/handler.js b/3-business-open-serverless/handler.js new file mode 100644 index 0000000..e08fa5a --- /dev/null +++ b/3-business-open-serverless/handler.js @@ -0,0 +1,20 @@ +exports.index = function(event, context, callback) { + if (isBusinessTime(new Date())) { + callback(null, { + statusCode: '200', + body: JSON.stringify({ message: "It's time for business" }), + headers: { 'Content-Type': 'application/json' } + }) + } else { + callback(null, { + statusCode: '200', + body: JSON.stringify({ message: "It's party time" }), + headers: { 'Content-Type': 'application/json' } + }) + } +} + +function isBusinessTime(now) { + return now.getDay() < 4 && now.getHours() >= 9 && now.getHours() < 17 +} + diff --git a/3-business-open-serverless/serverless.yml b/3-business-open-serverless/serverless.yml new file mode 100644 index 0000000..d873e77 --- /dev/null +++ b/3-business-open-serverless/serverless.yml @@ -0,0 +1,13 @@ +service: aws-nodejs # NOTE: update this with your service name + +provider: + name: aws + runtime: nodejs6.10 + +functions: + hello: + handler: handler.index + events: + - http: + path: / + method: get diff --git a/README.md b/README.md new file mode 100644 index 0000000..af78f20 --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +# Serverless Framework Examples + +Examples from talk given at the Lansing DevOps Meetup Group + +## 1-business-open + +DIY lambda function used in manually creating the API gateway stuff + +## 2-business-open-shell + +Attempt to create a shell maintained. May not be 100% complete. Shows that +creating these commands via CLI can be complex. Furthermore, this is not a +deterministic execution. + +## 3-business-open-serverless + +Uses the serverless framework to minimize our efforts and knowledge while +getting the exact same output.