- Install sam-cli - Details
- Install docker (required only to test functions locally)
sam build -t sam/templates/template.yaml
A lambda can be invoked locally using various event types. These events can be stored as json in a file. More details on generating events for local testing - Details
sam local invoke <lambda function resource name from template.yaml> -e sam/events/<filename>.json
example
sam local invoke SqsLambda -e sam/events/sqs.json
sam local start-api
It will start a localhost server on port 3000. To use different port use below command
sam local start-api -p 8000
We can use SAM to deploy the lambda functions and create resources automatically - Details
Example
sam deploy --config-file sam/samconfig.toml --config-env dev
We need below roles with required permission in order to run SAM template.
- S3 Access
- Cloudformation Access
- iam:PassRole Sample IAM Policy
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Statement1",
"Effect": "Allow",
"Action": [
"iam:PassRole"
],
"Resource": [
"arn:aws:iam::1234567890:role/cloudformation-role"
]
}
]
}
Add required permissions according to the resources added in the SAM template file.
Lambda can be invoked in multiple ways. Following are few
- SQS
- function Url
- API Gateway
- AWS SKD eg. boto3
Invoke lambda without any external event trigger using boto3 - Details
Example code
import json
import boto3
# Create a Boto3 Lambda client
lambda_client = boto3.client('lambda')
# payload is optional
payload = {
"key": "value"
}
# Invoke the Lambda function
# InvocationType="Event" -> just invoke
# InvocationType="RequestResponse" -> invoke and get function response
response = lambda_client.invoke(
FunctionName="function_url_lambda",
InvocationType="Event",
Payload=json.dumps(payload)
)
print(response)
# if InvocationType is RequestResponse
response_data = response['Payload'].read()
print(json.loads(response_data))