- AWS CLI already configured with at least PowerUser permission
- NodeJS 8.10+ installed
- Docker installed
In this example we use npm but you can use yarn if you prefer to manage NodeJS dependencies:
cd src
npm install
cd ../Invoking function locally through local API Gateway
sam local start-apiIf the previous command ran successfully you should now be able to hit the following local endpoint to invoke your function http://localhost:3000/hello
AWS Lambda NodeJS runtime requires a flat folder with all dependencies including the application. SAM will use CodeUri property to know where to look up for both application and dependencies:
...
FirstFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/
...Firstly, we need a S3 bucket where we can upload our Lambda functions packaged as ZIP before we deploy anything - If you don't have a S3 bucket to store code artifacts then this is a good time to create one:
aws s3 mb s3://BUCKET_NAMENext, run the following command to package our Lambda function to S3:
sam package \
--template-file template.yaml \
--output-template-file packaged.yaml \
--s3-bucket REPLACE_THIS_WITH_YOUR_S3_BUCKET_NAMENext, the following command will create a Cloudformation Stack and deploy your SAM resources.
sam deploy \
--template-file packaged.yaml \
--stack-name usage-data-collector-app \
--capabilities CAPABILITY_IAMSee Serverless Application Model (SAM) HOWTO Guide for more details in how to get started.
After deployment is complete you can run the following command to retrieve the API Gateway Endpoint URL:
aws cloudformation describe-stacks \
--stack-name usage-data-collector-app \
--query 'Stacks[].Outputs'We use mocha for testing our code and it is already added in package.json under scripts, so that we can simply run the following command to run our tests:
cd src
npm run testAWS CLI commands to package, deploy and describe outputs defined within the cloudformation stack:
sam package \
--template-file template.yaml \
--output-template-file packaged.yaml \
--s3-bucket REPLACE_THIS_WITH_YOUR_S3_BUCKET_NAME
sam deploy \
--template-file packaged.yaml \
--stack-name usage-data-collector-app \
--capabilities CAPABILITY_IAM \
--parameter-overrides MyParameterSample=MySampleValue
aws cloudformation describe-stacks \
--stack-name usage-data-collector-app --query 'Stacks[].Outputs'NOTE: Alternatively this could be part of package.json scripts section.