diff --git a/README.md b/README.md index bc4f2d070..3b89a6035 100644 --- a/README.md +++ b/README.md @@ -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 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: @@ -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). diff --git a/conf/iam/policies/lambda_trust.json b/conf/iam/policies/lambda_trust.json new file mode 100644 index 000000000..abb969b9a --- /dev/null +++ b/conf/iam/policies/lambda_trust.json @@ -0,0 +1,13 @@ +{ + "Version": "2012-10-17", + "Statement": [ + { + "Sid": "", + "Effect": "Allow", + "Principal": { + "Service": "lambda.amazonaws.com" + }, + "Action": "sts:AssumeRole" + } + ] +} diff --git a/conf/lambda/functions/echo/config.json b/conf/lambda/functions/echo/config.json new file mode 100644 index 000000000..0775a5aa6 --- /dev/null +++ b/conf/lambda/functions/echo/config.json @@ -0,0 +1,7 @@ +{ + "Runtime": "nodejs", + "Description": "A simple echo function", + "Timeout": 5, + "MemorySize": 128, + "Publish": true +} diff --git a/sspa b/sspa index 7d92ae080..d0461138c 100755 --- a/sspa +++ b/sspa @@ -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() { @@ -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 } @@ -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" \ @@ -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 #" @@ -203,26 +249,17 @@ function help() { echo "Usage: sspa [arguments...]" echo echo "Where is one of:" - echo " server - Run a local development server" - echo " create_bucket - Create a web-accessible bucket in S3" - echo " deploy_bucket - Deploy the application to S3 bucket" - echo " create_pool - Create a new Cognito identity pool" - echo " create_table - 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 - 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 " + echo " deploy_bucket " + echo " create_pool " + echo " create_table " + echo " test" + echo " build_bundle" + echo " deploy_bundle" + echo " create_service " + #echo " create_gateway - 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"} @@ -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 @@ -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"