API gateway integration to get method and post method to retrieve/insert data in DynamoDB using Lambda
Integration 1
Get method integration in
API Gateway to retrieve information from the DynamoDB table
a.
Create an IAM role
Create an assumable IAM role.
Following trust policy attached to it
{
"Version":
"2012-10-17",
"Statement": [
{
"Sid":
"",
"Effect":
"Allow",
"Principal": {
"Service":
"apigateway.amazonaws.com"
},
"Action":
"sts:AssumeRole"
}
]
}
The role must have invokefunction permission
{
"Version":
"2012-10-17",
"Statement": [
{
"Effect":
"Allow",
"Action":
"lambda:InvokeFunction",
"Resource":
"*"
}
]
}
Additionally, add the DynamoDBFullAccess policy as the role will be
attached to the
Lambda functions as well to access the DynamoDB
b.
Create DynamoDB table
c.
Create lambda functions
lambda_dynamodb_getEmployee
-------------------------------------------------------------------------------------------
import json
import boto3
# Get the service resource.
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('employee')
def lambda_handler(event, context):
empid=event['a']
deptid=event['b']
response = table.get_item( Key={
'empid':empid,
'deptid':deptid
})
return {
'statusCode': 200,
'body': response['Item']
}
-------------------------------------------------------------------------------------------
d.
Create API gateway
For Integration type, choose Amazon Service.
For Amazon Region, choose the region (e.g., ap-south-1) where you created the Lambda function.
For Amazon Service, choose Lambda.
Leave Amazon Subdomain blank, because our Lambda function is not hosted on any Amazon subdomain.
For HTTP method, choose POST and choose the checkmark icon to save your choice.
Lambda requires that the POST request be used to invoke any Lambda function.
This example shows that the HTTP method in a frontend method request can be different from the integration request in the backend.
Choose Use path override for Action Type. This option allows us to specify the ARN of the Invoke action to execute our Calc function.
Enter
/2015-03-31/functions/arn:aws:lambda:ap-south-1:598245567716:function:lambda_dynamodb_getEmployee/invocations in Path override,
where region is the region where you created your Lambda function and account-id is the account number for the Amazon account.
For Execution role, enter the role ARN for the dynamoDBAPIGatewayRole IAM role you created earlier.
Leave Content Handling set to Passthrough, because this method will not deal with any binary data.
Leave Use default timeout checked.
Choose Save.
Choose Method Request.
Now you will set up query parameters for the GET method on /employees so it can receive input on behalf of the backend Lambda function.
Choose the pencil icon next to Request Validator and choose Validate query string parameters and headers from the drop-down menu.
This setting will cause an error message to return to state the required parameters are missing if the client does not specify them.
You will not get charged for the call to the backend.
Choose the checkmark icon to save your changes.
Expand the URL Query String Parameters section.
Choose Add query string.
For Name, type empid.
Choose the checkmark icon to save the parameter.
Repeat the previous steps to create parameters named deptid and operator.
Check the Required option for each parameter to ensure that they are validated.
Choose Method Execution and then choose Integration Request to set up the mapping template to translate the client-supplied query strings to the integration request payload as required by the employee function.
Expand the Mapping Templates section.
Choose When no template matches the request Content-Type header for Request body passthrough.
Under Content-Type, choose Add mapping template.
Type application/json and choose the checkmark icon to open the template editor.
Choose Yes, secure this integration to proceed.
Copy the following mapping script into the mapping template editor:
{
"a": "$input.params('empid')",
"b": "$input.params('deptid')"
}
e. Deploy the method
f. Try to run
Integration 2: Create a POST method
to insert DynamoDB table via Lambda call
a.
Use same IAM role
b.
Same DynamoDB table
c.
Create lambda Function called lambda_dynamodb_putEmployee
------------------------------------------------------------------------------------------------
import json
import boto3
# Get the service resource.
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('employee')
def lambda_handler(event, context):
# TODO implement
table.put_item(Item=event)
return {
'statusCode': 200,
'message': 'a new employee inserted successfully!'
}
---------------------------------------------------------------------------------------------
d.
Create API gateway POST method on same employees
resource
e.
Test it
Comments
Post a Comment