1
0
Fork 0

Initial commit

This commit is contained in:
Andrew Tomaka 2017-08-01 22:29:26 -04:00
commit 86effe60a5
No known key found for this signature in database
GPG key ID: C78D1A81582BAC86
11 changed files with 218 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
**/build/**

19
1-business-open/index.js Normal file
View file

@ -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
}

View file

@ -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

View file

@ -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
}

View file

@ -0,0 +1,14 @@
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "apigateway.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}

View file

@ -0,0 +1,8 @@
{
"Version": "2012-10-17",
"Statement": {
"Effect": "Allow",
"Principal": {"Service": "lambda.amazonaws.com"},
"Action": "sts:AssumeRole"
}
}

View file

@ -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

6
3-business-open-serverless/.gitignore vendored Normal file
View file

@ -0,0 +1,6 @@
# package directories
node_modules
jspm_packages
# Serverless directories
.serverless

View file

@ -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
}

View file

@ -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

18
README.md Normal file
View file

@ -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.