11 Steps to Create an HTTPS Endpoint on AWS to trigger Lambda Function
======================================================================
Use AWS Lambda function via HTTPS Endpoint to create and update a DynamoDB Table
======================================================================
======================================================================
======================================================================
======================================================================
Step 1: Creating Lambda Function:
Syntax through AWS CLI :
aws lambda create-function \
--region REGION_NAME \
--function-name YOUR_LAMBDA_FUNCTION_NAME \
--zip-file fileb://NAME_OF_YOUR_ZIPPED_LAMBDA_FUNCTION_CODE.zip \
--role arn:aws:iam::****:role/lambda-gateway-execution-role \
--handler LambdaFunctionOverHttps.handler \
--runtime python3.6 \
--profile default
==================================================================
Step 2: Invoking the created Lambda Function:
Syntax through AWS CLI :
aws lambda invoke \
--invocation-type Event \
--function-name LambdaFunctionOverHttps \
--region ap-southeast-1 \
--payload file://YOUR_PAYLOAD_FILE.json \
--profile default
PATH_TO_YOUR_OUTPUT_FILE/outputfile.txt
Returns:
{
"StatusCode": 202
}
==================================================================
Step 3: Creating Rest API Gateway:
Syntax through AWS CLI :
aws apigateway create-rest-api \
--name DynamoDBOperations \
--region REGION_NAME \
--profile default
Returns:
{
"name": "DynamoDBOperations",
"id": "9****",
"createdDate": 1504516528
}
==================================================================
So, the API ID for this REST API Gateway is: 9****
==================================================================
Step 4: Get the ID of Root Resource:
Syntax through AWS CLI :
aws apigateway get-resources \
--rest-api-id 9***
Returns:
{
"items": [
{
"path": "/",
"id": "3***"
}
]
}
==================================================================
Step 5: Create a Resource (DynamoDBManager) in the API:
Syntax through AWS CLI :
aws apigateway create-resource \
--rest-api-id 9*** \
--parent-id 3*** \
--path-part DynamoDBManager
Returns:
{
"path": "/DynamoDBManager",
"pathPart": "DynamoDBManager",
"id": "0****",
"parentId": "3***"
}
=================================================================
=================================================================
So, Uptil now, what we have received from the Gateway Creation process are variables:
API ID: "9***",
ROOT ID: "3***"
RESOURCE ID: "0****"
=================================================================
=================================================================
Step 5: CREATE POST METHOD ON THE RESOURCE:
Syntax through AWS CLI :
aws apigateway put-method \
--rest-api-id 9**** \
--resource-id 0**** \
--http-method POST \
--authorization-type NONE
Returns:
{
"apiKeyRequired": false,
"httpMethod": "POST",
"authorizationType": "NONE"
}
=================================================================
Step 6: SET LAMBDA FUNCTION AS DESTINATION FOR POST METHOD:
Syntax through AWS CLI :
aws apigateway put-integration \
--rest-api-id 9*** \
--resource-id 0*** \
--http-method POST \
--type AWS \
--integration-http-method POST \
--uri arn:aws:apigateway:REGION_NAME:lambda:path/2015-03-31/functions/arn:aws:lambda:REGION_NAME:461024261025:function:FUNCTION_NAME/invocations
Returns:
{
"httpMethod": "POST",
"passthroughBehavior": "WHEN_NO_MATCH",
"cacheKeyParameters": [],
"type": "AWS",
"uri": ".......",
"cacheNamespace": "....."
}
======================================================================
Step 7: SET THE POST METHOD RESPONSE TO JSON :
Syntax through AWS CLI :
aws apigateway put-method-response \
--rest-api-id 9*** \
--resource-id 0*** \
--http-method POST \
--status-code 200 \
--response-models "{\"application/json\": \"Empty\"}"
Returns:
{
"responseModels": {
"application/json": "Empty"
},
"statusCode": "200"
}
======================================================================
Step 8: SET THE POST METHOD INTEGRATION RESPONSE TO JSON:
Syntax through AWS CLI :
aws apigateway put-integration-response \
--rest-api-id 9**** \
--resource-id 0**** \
--http-method POST \
--status-code 200 \
--response-templates "{\"application/json\": \"\"}"
Returns:
{
"statusCode": "200",
"responseTemplates": {
"application/json": null
}
}
======================================================================
Step 9: DEPLOY YOUR API:
Syntax through AWS CLI :
aws apigateway create-deployment \
--rest-api-id 9*** \
--stage-name prod
Returns:
{
"id": "k***",
"createdDate": 1504518230
}
======================================================================
======================================================================
So, Uptil now, what we have received from the Gateway Creation process are variables:
API ID: "9***",
ROOT ID: "3***"
RESOURCE ID: "0***"
DEPLOYMENT ID: "k***"
======================================================================
======================================================================
Step 10: GRANT PERMISSION TO ALLOW AMAZON API GATEWAY TO INVOKE THE LAMBDA FUNCTION:
Syntax through AWS CLI :
aws lambda add-permission \
--function-name FUNCTION_NAME \
--statement-id apigateway-test-2 \
--action lambda:InvokeFunction \
--principal apigateway.amazonaws.com \
--source-arn "arn:aws:execute-api:ap-southeast-1:461024261025:YOUR_API_ID/*/POST/DynamoDBManager"
Returns:
{
"Statement": "{\"Sid\":\"apigateway-test-2\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"apigateway.amazonaws.com\"},\"Action\":\"lambda:InvokeFunction\",\"Resource\":\"arn:aws:lambda:REGION_NAME:....:function:FUNCTION_NAME\",\"Condition\":{\"ArnLike\":{\"AWS:SourceArn\":\"arn:aws:execute-api:ap-southeast-1:461024261025:YOUR_API_ID/*/POST/DynamoDBManager\"}}}"
}
======================================================================
Step 11: TEST API BY SENDING HTTPS REQUEST TO INSERT AN ENTRY IN DYNAMODB TABLE NAMED 'LambdaTable':
Syntax through AWS CLI :
aws apigateway test-invoke-method \
--rest-api-id 9*** \
--resource-id 0*** \
--http-method POST \
--path-with-query-string "" \
--body "{\"operation\":\"create\",\"tableName\":\"LambdaTable\",\"payload\":{\"Item\":{\"Id\":\"1\",\"name\":\"Bob\"}}}"
OUR PAYLOAD CONSISTS OF:
{
"operation": "create",
"tableName": "LambdaTable",
"payload": {
"Item": {
"Id": "1",
"name": "Bob"
}
}
}
Returns:
.........
.........
.......Mon Sep 04 09:49:22 UTC 2017 : Successfully completed execution\nMon Sep 04 09:49:22 UTC 2017 : Method completed with status: 200\n",
"latency": 937,
"headers": {
"X-Amzn-Trace-Id": "sampled=0;root=1-59ad21a1-b874b0eb86c658ea7176887e",
"Content-Type": "application/json"
}
}
======================================================================
======================================================================
======================================================================
======================================================================
======================================================================
TEST CONNECTION FOR AN ECHO COMMENT:
Syntax through AWS CLI :
aws apigateway test-invoke-method \
--rest-api-id 9*** \
--resource-id 0**** \
--http-method POST \
--path-with-query-string "" \
--body "{\"operation\":\"echo\",\"payload\":{\"somekey1\":\"somevalue1\",\"somekey2\":\"somevalue2\"}}"
OUR PAYLOAD CONSISTS OF:
{
"operation": "echo",
"payload": {
"somekey1": "somevalue1",
"somekey2": "somevalue2"
}
}
Returns:
{
"status": 200,
"body": "{\"somekey1\": \"somevalue1\", \"somekey2\": \"somevalue2\"}",
"log": "Execution log for request test-request
.....
.....
}
======================================================================
TO READ ITEMS FROM DYNAMODB TABLE USING OUR HASH KEY:ID VIA HTTPS ENDPOINT:
Syntax through AWS CLI :
aws apigateway test-invoke-method \
--rest-api-id 9*** \
--resource-id 0*** \
--http-method POST \
--path-with-query-string "" \
--body "{\"operation\":\"read\",\"tableName\":\"LambdaTable\",\"payload\":{\"Key\":{\"Id\":\"1\"}}}"
OUR PAYLOAD CONSISTS OF:
{
"operation": "read",
"tableName": "LambdaTable",
"payload": {
"Key": {
"Id": "1"
}
}
}
Returns:
{
"status": 200,
"body": "{\"Item\": {\"Id\": \"1\", \"name\": \"Bob\"}, \"ResponseMetadata\": {\"RequestId\":
..
..
}
======================================================================
Use AWS Lambda function via HTTPS Endpoint to create and update a DynamoDB Table
======================================================================
======================================================================
======================================================================
======================================================================
Step 1: Creating Lambda Function:
Syntax through AWS CLI :
aws lambda create-function \
--region REGION_NAME \
--function-name YOUR_LAMBDA_FUNCTION_NAME \
--zip-file fileb://NAME_OF_YOUR_ZIPPED_LAMBDA_FUNCTION_CODE.zip \
--role arn:aws:iam::****:role/lambda-gateway-execution-role \
--handler LambdaFunctionOverHttps.handler \
--runtime python3.6 \
--profile default
==================================================================
Step 2: Invoking the created Lambda Function:
Syntax through AWS CLI :
aws lambda invoke \
--invocation-type Event \
--function-name LambdaFunctionOverHttps \
--region ap-southeast-1 \
--payload file://YOUR_PAYLOAD_FILE.json \
--profile default
PATH_TO_YOUR_OUTPUT_FILE/outputfile.txt
Returns:
{
"StatusCode": 202
}
==================================================================
Step 3: Creating Rest API Gateway:
Syntax through AWS CLI :
aws apigateway create-rest-api \
--name DynamoDBOperations \
--region REGION_NAME \
--profile default
Returns:
{
"name": "DynamoDBOperations",
"id": "9****",
"createdDate": 1504516528
}
==================================================================
So, the API ID for this REST API Gateway is: 9****
==================================================================
Step 4: Get the ID of Root Resource:
Syntax through AWS CLI :
aws apigateway get-resources \
--rest-api-id 9***
Returns:
{
"items": [
{
"path": "/",
"id": "3***"
}
]
}
==================================================================
Step 5: Create a Resource (DynamoDBManager) in the API:
Syntax through AWS CLI :
aws apigateway create-resource \
--rest-api-id 9*** \
--parent-id 3*** \
--path-part DynamoDBManager
Returns:
{
"path": "/DynamoDBManager",
"pathPart": "DynamoDBManager",
"id": "0****",
"parentId": "3***"
}
=================================================================
=================================================================
So, Uptil now, what we have received from the Gateway Creation process are variables:
API ID: "9***",
ROOT ID: "3***"
RESOURCE ID: "0****"
=================================================================
=================================================================
Step 5: CREATE POST METHOD ON THE RESOURCE:
Syntax through AWS CLI :
aws apigateway put-method \
--rest-api-id 9**** \
--resource-id 0**** \
--http-method POST \
--authorization-type NONE
Returns:
{
"apiKeyRequired": false,
"httpMethod": "POST",
"authorizationType": "NONE"
}
=================================================================
Step 6: SET LAMBDA FUNCTION AS DESTINATION FOR POST METHOD:
Syntax through AWS CLI :
aws apigateway put-integration \
--rest-api-id 9*** \
--resource-id 0*** \
--http-method POST \
--type AWS \
--integration-http-method POST \
--uri arn:aws:apigateway:REGION_NAME:lambda:path/2015-03-31/functions/arn:aws:lambda:REGION_NAME:461024261025:function:FUNCTION_NAME/invocations
Returns:
{
"httpMethod": "POST",
"passthroughBehavior": "WHEN_NO_MATCH",
"cacheKeyParameters": [],
"type": "AWS",
"uri": ".......",
"cacheNamespace": "....."
}
======================================================================
Step 7: SET THE POST METHOD RESPONSE TO JSON :
Syntax through AWS CLI :
aws apigateway put-method-response \
--rest-api-id 9*** \
--resource-id 0*** \
--http-method POST \
--status-code 200 \
--response-models "{\"application/json\": \"Empty\"}"
Returns:
{
"responseModels": {
"application/json": "Empty"
},
"statusCode": "200"
}
======================================================================
Step 8: SET THE POST METHOD INTEGRATION RESPONSE TO JSON:
Syntax through AWS CLI :
aws apigateway put-integration-response \
--rest-api-id 9**** \
--resource-id 0**** \
--http-method POST \
--status-code 200 \
--response-templates "{\"application/json\": \"\"}"
Returns:
{
"statusCode": "200",
"responseTemplates": {
"application/json": null
}
}
======================================================================
Step 9: DEPLOY YOUR API:
Syntax through AWS CLI :
aws apigateway create-deployment \
--rest-api-id 9*** \
--stage-name prod
Returns:
{
"id": "k***",
"createdDate": 1504518230
}
======================================================================
======================================================================
So, Uptil now, what we have received from the Gateway Creation process are variables:
API ID: "9***",
ROOT ID: "3***"
RESOURCE ID: "0***"
DEPLOYMENT ID: "k***"
======================================================================
======================================================================
Step 10: GRANT PERMISSION TO ALLOW AMAZON API GATEWAY TO INVOKE THE LAMBDA FUNCTION:
aws lambda add-permission \
--function-name FUNCTION_NAME \
--statement-id apigateway-test-2 \
--action lambda:InvokeFunction \
--principal apigateway.amazonaws.com \
--source-arn "arn:aws:execute-api:ap-southeast-1:461024261025:YOUR_API_ID/*/POST/DynamoDBManager"
Returns:
{
"Statement": "{\"Sid\":\"apigateway-test-2\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"apigateway.amazonaws.com\"},\"Action\":\"lambda:InvokeFunction\",\"Resource\":\"arn:aws:lambda:REGION_NAME:....:function:FUNCTION_NAME\",\"Condition\":{\"ArnLike\":{\"AWS:SourceArn\":\"arn:aws:execute-api:ap-southeast-1:461024261025:YOUR_API_ID/*/POST/DynamoDBManager\"}}}"
}
======================================================================
Step 11: TEST API BY SENDING HTTPS REQUEST TO INSERT AN ENTRY IN DYNAMODB TABLE NAMED 'LambdaTable':
aws apigateway test-invoke-method \
--rest-api-id 9*** \
--resource-id 0*** \
--http-method POST \
--path-with-query-string "" \
--body "{\"operation\":\"create\",\"tableName\":\"LambdaTable\",\"payload\":{\"Item\":{\"Id\":\"1\",\"name\":\"Bob\"}}}"
OUR PAYLOAD CONSISTS OF:
{
"operation": "create",
"tableName": "LambdaTable",
"payload": {
"Item": {
"Id": "1",
"name": "Bob"
}
}
}
Returns:
.........
.........
.......Mon Sep 04 09:49:22 UTC 2017 : Successfully completed execution\nMon Sep 04 09:49:22 UTC 2017 : Method completed with status: 200\n",
"latency": 937,
"headers": {
"X-Amzn-Trace-Id": "sampled=0;root=1-59ad21a1-b874b0eb86c658ea7176887e",
"Content-Type": "application/json"
}
}
======================================================================
======================================================================
HERE "Id" is the HASH Key for the LambdaTable
============================================================================================================================================
======================================================================
YOUR AWS HTTPS CONNECTION IS SUCCESSFULLY CREATED
============================================================================================================================================
TEST CONNECTION FOR AN ECHO COMMENT:
Syntax through AWS CLI :
aws apigateway test-invoke-method \
--rest-api-id 9*** \
--resource-id 0**** \
--http-method POST \
--path-with-query-string "" \
--body "{\"operation\":\"echo\",\"payload\":{\"somekey1\":\"somevalue1\",\"somekey2\":\"somevalue2\"}}"
OUR PAYLOAD CONSISTS OF:
{
"operation": "echo",
"payload": {
"somekey1": "somevalue1",
"somekey2": "somevalue2"
}
}
Returns:
{
"status": 200,
"body": "{\"somekey1\": \"somevalue1\", \"somekey2\": \"somevalue2\"}",
"log": "Execution log for request test-request
.....
.....
}
======================================================================
TO READ ITEMS FROM DYNAMODB TABLE USING OUR HASH KEY:ID VIA HTTPS ENDPOINT:
Syntax through AWS CLI :
aws apigateway test-invoke-method \
--rest-api-id 9*** \
--resource-id 0*** \
--http-method POST \
--path-with-query-string "" \
--body "{\"operation\":\"read\",\"tableName\":\"LambdaTable\",\"payload\":{\"Key\":{\"Id\":\"1\"}}}"
OUR PAYLOAD CONSISTS OF:
{
"operation": "read",
"tableName": "LambdaTable",
"payload": {
"Key": {
"Id": "1"
}
}
}
Returns:
{
"status": 200,
"body": "{\"Item\": {\"Id\": \"1\", \"name\": \"Bob\"}, \"ResponseMetadata\": {\"RequestId\":
..
..
}
======================================================================
======================================================================
AND THIS IS HOW WE HAVE CREATED AND TESTED AN HTTPS API GATEWAY FOR AWS
======================================================================
======================================================================
It’s really interesting content and nice post AWS online Training Hyderabad
ReplyDelete