A small repo to test creating a Lambda function with the SAM CLI.
The lambda_handler.py
file contains the lambda function.
The template.yml
fle is the SAM template file used to create the lambda function.
You will need to update this file with the arn of your Lambda function role.
The requirements.txt
file is an empty Python requirements file that allows for local building and testing.
Before you can run this package you must have the associated IAM permissions and a S3 bucket to read and write to.
In the following commands replace <bucket>
with your S3 bucket name and a unique <stack-name>
.
You also need a Lambda role as created in this repo
and update the template.yml
file with this arn.
Executing the following command will build, deploy and remotely test the lambda function.
bash deploy.bsh <bucket> <stack-name>
If successful you should see Test successful
at end of script output
Please read the deploy.bsh file to see all of the individual commands.
Note that all of these operations occur in the package
folder so only these contents are deployed to AWS.
This command builds the CloudFormation package .aws-sam
to be tested locally and deployed.
cd package
sam build
First start a local Lambda endpoint with this command.
sam local start-lambda
Then in a new terminal invoke the local lambda with this command.
rm response.json
aws lambda invoke \
--function-name demo-sam-lambda \
--endpoint-url "http://127.0.0.1:3001" \
--payload '{"first_name": "Dan", "last_name": "the man"}' \
--cli-binary-format raw-in-base64-out \
--no-verify-ssl response.json
You should see a 200
status code and the response.json
file should contain {"message": "Hello Dan the man!"}
.
This command deploys the CloudFormation template in .aws-sam
, which creates the lambda function.
sam deploy --stack-name <stack-name> --s3-bucket <bucket> --capabilities CAPABILITY_IAM
rm response.json
aws lambda invoke \
--function-name demo-sam-lambda \
--payload '{"first_name": "Dangerous", "last_name": "Dan"}' \
--cli-binary-format raw-in-base64-out \
response.json
You should see a 200
status code and the response.json
file should contain {"message": "Hello Dangerous Dan!"}
.