Post on dev.to: https://dev.to/gfabrizi/porting-lambda-functions-to-aws-sam-3hl7
Original link to dev.to post for explaination on the js code: https://dev.to/gfabrizi/lambda-inception-architectural-pattern-f67
- Clone this repository.
- Install AWS cli tools (https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) and AWS SAM (https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html)
- Be sure to be logged in aws cli with a user that has at least the policies AmazonS3FullAccess, AWSCloudFormationFullAccess, AWSLambda_FullAccess, IAMFullAccess
- In a editor open
samconfig.toml
and modify theregion
in the[default.deploy.parameters]
block to suit your needs - Open up your terminal and enter the project's root directory. Launch:
sam build
sam deploy
(sayy
when it asks to deploy)sam list endpoints --output json
(take note of theCloudEndpoint
url, this is the endpoint you have to call)
Now that all the resources are deployed, you need to add api credential to the IAM user generated by SAM.
To do so go on IAM web console e click on the new lambda-inception-invoker
user. Click on the "security-credentials" and next on "Create access key".
In the "Access key best practices & alternatives" select "Other" and click "Next". Give a descriptive tag to this api key (optional) and click on "Create access key".
In the last screen of the wizard, click on "Download .csv file" to save the credentials.
Now you can call your api endpoint like this:
curl -X POST https://YOUR_LAMBDA_FUNCTION_ENDPOINT/crawl -H 'Content-Type: application/json' \
-d '{"url":"https://example.org/"}' \
--user "ACCESS_KEY:SECRET_ACCESS_KEY" \
--aws-sigv4 "aws:amz:YOUR_AWS_REGION:lambda"
(modify YOUR_LAMBDA_FUNCTION_ENDPOINT
, ACCESS_KEY
, SECRET_ACCESS_KEY
and YOUR_AWS_REGION
with your config)