Skip to content

Commit

Permalink
Adding create_service action in sspa
Browse files Browse the repository at this point in the history
  • Loading branch information
benrady committed Dec 18, 2015
1 parent a53b7ef commit 019fcd4
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 25 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@ Go to http://localhost:9292/tests for the tests

It will reload automatically as you change files

## Change Log

### Beta 2 Changes

* Now using 'admin' profile instead of default profile
* Added create_service action to sspa
* Changed Lambda functions to pull code from single bundle stored in S3.
* deploy_bucket can now take a config path

## MIT License

Note: The license below applies only to the contents of this git repository, not the Pragmatic Bookshelf title "Serverless Single Page Apps", or any other related content.

Copyright (c) 2015 Ben Rady <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
Expand All @@ -19,4 +30,6 @@ The above copyright notice and this permission notice shall be included in all c

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

HeroImage.jpg is licensed from popularwoodworking.com under the Creative Commons Attribution License (CC BY 3.0 US).
## Creative Commons Attributions

* HeroImage.jpg is licensed from popularwoodworking.com under the Creative Commons Attribution License (CC BY 3.0 US).
13 changes: 13 additions & 0 deletions conf/iam/policies/lambda_trust.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
7 changes: 7 additions & 0 deletions conf/lambda/functions/echo/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"Runtime": "nodejs",
"Description": "A simple echo function",
"Timeout": 5,
"MemorySize": 128,
"Publish": true
}
87 changes: 63 additions & 24 deletions sspa
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

abspath="$(cd "${0%/*}" 2>/dev/null; echo "$PWD"/"${0##*/}")"
root_dir=`dirname $abspath`
app_name=learnjs
code_bucket=${app_name}/_code_bundles
profile=admin

function check_python() {
Expand Down Expand Up @@ -32,23 +34,37 @@ function check_node_deps() {
function dev_server() {
cd public
exec python -m SimpleHTTPServer 9292
popd
}

function create_s3_bucket() {
local bucket_name=$1
local bucket_uri="s3://${bucket_name}"
aws --profile admin s3 mb $bucket_uri
local bucket_path=conf/s3/${bucket_name}
# The s3 command doesn't output JSON :-/
if [[ ! -e ${bucket_path}/endpoint.txt ]]; then
aws --profile admin s3 mb $bucket_uri && mkdir -p ${bucket_path}
local region=$(aws --profile admin configure get region)
local endpoint="http://${1}.s3-website-${region}.amazonaws.com"
echo "$endpoint" > ${bucket_path}/endpoint.txt
echo "Website endpoint is: $endpoint"
fi
}

function webify_bucket() {
local bucket_name=$1
local bucket_uri="s3://${bucket_name}"
aws --profile admin s3 website \
--index-document index.html \
--error-document error.html \
$bucket_uri
local region=$(aws --profile admin configure get region)
echo "Website endpoint is: http://${1}.s3-website-${region}.amazonaws.com"
}

function deploy_s3_bucket() {
local bucket_uri="s3://$1"
local bucket_name=$1
if [[ -d ${1} ]]; then
bucket_name=$(basename $bucket_name)
fi
local bucket_uri="s3://${1}"
aws --profile admin s3 sync public/ $bucket_uri --acl public-read
}

Expand Down Expand Up @@ -113,6 +129,7 @@ function create_cognito_auth_role() {
if [[ ! -s ${identity_pool_dir}/role_info.json ]]; then
local role_name="${pool_name}_cognito_authenticated"
echo "Creaing role: $role_name"
# Might be able to use the create_iam_role function for this
aws --profile admin iam create-role \
--role-name "$role_name" \
--assume-role-policy-document "file://${identity_pool_dir}/assume_role_policy.json" \
Expand Down Expand Up @@ -195,6 +212,35 @@ function test_services() {
cd ..
}

function create_iam_role() {
local role_name="${app_name}_lambda_exec"
local role_dir=conf/iam/roles/${role_name}
local policy_document=$2
mkdir -p $role_dir
if [[ ! -e ${role_dir}/info.json ]]; then
aws --profile admin iam create-role \
--role-name "$role_name" \
--assume-role-policy-document "${policy_document}" \
> ${role_dir}/info.json
fi
}

function create_lambda_service() {
local service_dir=${1%/}
local function_name=$(basename $service_dir)
create_iam_role lambda_exec "file://conf/iam/policies/lambda_trust.json"
local role_arn=$(support/jsed.py conf/iam/roles/learnjs_lambda_exec/info.json 'Role.Arn')
if [[ ! -e ${service_dir}/info.json ]]; then
aws lambda create-function \
--function-name ${function_name} \
--role ${role_arn} \
--zip-file "fileb://services/archive.zip" \
--handler "index.${function_name}" \
--cli-input-json "file://${service_dir}/config.json" \
> ${service_dir}/info.json
fi
}

function help() {
echo "#################################################"
echo "# Serverless Single Page Apps - The Bash Helper #"
Expand All @@ -203,26 +249,17 @@ function help() {
echo "Usage: sspa <action> [arguments...]"
echo
echo "Where <action> is one of:"
echo " server - Run a local development server"
echo " create_bucket <bucket name> - Create a web-accessible bucket in S3"
echo " deploy_bucket <bucket name> - Deploy the application to S3 bucket"
echo " create_pool <config dir> - Create a new Cognito identity pool"
echo " create_table <config dir> <pool_name> - Create a new DynamoDB table"
echo " test - Run Lambda service automated tests"
echo " build_bundle - Build the Lambda service bundle"
echo " deploy_bundle - Deploy the Lambda service bundle"
echo " create_service <config_file> - Create a new Lambda service"
echo
echo "Examples:"
echo
echo "Deploy the web app to an S3 bucket:"
echo " $ sspa deploy_public learnjs.benrady.com"
echo
echo "Create the 'learnjs' identity pool"
echo " $ sspa create_pool conf/cognito/identity_pools/learnjs"
echo " server"
echo " create_bucket <bucket name>"
echo " deploy_bucket <bucket dir>"
echo " create_pool <config dir>"
echo " create_table <config dir> <pool name>"
echo " test"
echo " build_bundle"
echo " deploy_bundle"
echo " create_service <config file>"
#echo " create_gateway <config_file> <service> - Attach an API Gateway to a service"
echo
echo "Create a 'learnjs' table accessible by users in the 'learnjs' identity pool"
echo " $ sspa create_pool conf/cognito/identity_pools/learnjs learnjs"
}

action=${1:-"help"}
Expand Down Expand Up @@ -253,6 +290,7 @@ case "$action" in
create_bucket)
if [[ $# -eq 2 ]]; then
create_s3_bucket ${2}
webify_bucket ${2}
else
echo "Please specify a bucket name"
exit 1
Expand Down Expand Up @@ -288,6 +326,7 @@ case "$action" in

create_service)
if [[ $# -eq 2 ]]; then
build_bundle
create_lambda_service ${2}
else
echo "Please specify a Lambda config file"
Expand Down

0 comments on commit 019fcd4

Please sign in to comment.