A simple command line interface for html-lambda-runner.
While Amazon's Lambda functions aren't hard to make, making lot's of them is a time consuming task, especially if you're only making lots of single page serving functions. html-lambda-cli provides a convenient and clean interface to rapidly build and deploy new HTML generating Lambda functions using our html-lambda-runner module. With a bit of setup and a few lines of code, you'll have a Lambda up and running in no time.
To Install
npm install -g html-lambda-cli
Usage
init
- Create the files needed for a new Lambda project in the current directory.
build
- Build the project source and bundle it into a zip file in the dist folder.
serve
- Preview the current Lambda by running a local server on :3000
. Changes will be automatically built and get parameters will be passed.
deploy
- Runs build
and then uploads the result to Amazon.
Make a Lambda
Most of the work you'll need to do for each function is merely setup on Amazon's end. While it looks like a lot, it's not a hard procedure. This assumes you've setup your local AWS credentials, if you haven't yet, skip to the section below.
- Create a new folder for your project
- Run
html-lambda init
to create the needed files. - Head over to the AWS console and open up Lambda.
- Click 'Create a Lambda function'.
- Click 'Skip' when asked to use a template.
- Give your function a name, description and leave the 'Runtime' set as
Node.js (4.x)
. - In the 'Edit code inline' area, put something simple for now. Something like
console.log('hello')
will do. - Leave 'Handler' as is and select
lambda_basic_execution
as the Lambda role. - Configure anything else if needed, then click 'Next' and 'Create'.
- Click on the 'API endpoints tab' and the 'Add API endpoint'.
- Select 'API Gateway' as the type and configure other properties as desired, setting 'Resource name' to
/
, 'Method' toGET
'Security' toOpen
. - Head over to 'API Gateway' and pull up the API under your Lambda's name.
- Under 'Method Request' > 'URL Query String Parameters' add
accessKey
. - Under 'Integration Request' add a new 'Content-Type' set to
application/json
. The template below only passes theaccessKey
. You can add other query parameters here as well. Click 'Save' when done.
{
"accessKey ": "$input.params('accessKey')"
}
- Under 'Method Response' add a new item under 'Response Headers for 200' with a value of
Content-Type
. - Under 'Integration Response' > 'Header Mappings', set
Content-Type
to'text/html'
- Add a new 'Content-Type' under 'Body Mapping Templates' with the value
text/html
and a template of$input.path('$')
. - Finally, click the 'Actions' button in the top left and select 'Deploy API'.
- Choose the development stage you selected earlier and click 'Deploy'.
- On the main services tab, select IAM and then in the left sidebar, choose 'Encryption Keys'.
- Create a new key and add yourself as a key administrator.
- Add
lambda_basic_execution
for IAM roles that can decrypt and encrypt with the key. Complete key creation. - View the newly created key and copy the ARN.
- Paste the ARN into the
lambdaKmsArn
ofaws-config.json
. Fill in the other Lambda fields as well. - In
config.json
set a secret key (used for simple authentication via GET over HTTPS). - Finally, run
html-lambda deploy
. - Visit your API endpoint with the proper
accessKey
attached, and your Lambda should present your page!
AWS Credentials
Create the file ~/.aws/credentials
. The file should look like:
[default]
aws_access_key_id = <your key>
aws_secret_access_key = <your access key>