Git Product home page Git Product logo

api-gateway-with-lambda's Introduction

API Gateway with Lambda, AWS Service and Mock Integrations

API Gateway with Lambda

Step 1: Create an SNS Topic

  • Create a Topic named “API-messages” and select the type as Standard.

Fig:1

Fig:1

  • Now create a Subscription, Protocol shall be Email and give the e-mail ID. Confirmation mail will be sent to your mail ID.

Step 2: Create a Lambda function

  • Create a Lambda function with the info:

Fig:2

Fig:2

  • In the function go to code source and enter the code given below:
def lambda_handler(event, context):   
    return {
        'statusCode': 200,
        'headers': {},
        'body': event['requestContext']['identity']['sourceIp'],
        'isBase64Encoded': False
        }

Step 3: API Gateway:

  • Select the REST API public:

Fig 3

Fig 3

Fig 4

Fig 4

  • Create a Resource and a method:

Fig 5

Fig 5

Fig 6 Create a method with GET and select ‘Mock’ and save it.

Fig 6 Create a method with GET and select ‘Mock’ and save it.

Fig 6 Select the ‘Integration Response’.

Fig 6 Select the ‘Integration Response’.

Fig 7 In the Integration Response, Select the Mapping Templates and add ‘application/json’. Type the desired message in the template.

Fig 7 In the Integration Response, Select the Mapping Templates and add ‘application/json’. Type the desired message in the template.

  • Create another Resource named ‘Lambda

Fig 8

Fig 8

Fig 9

Fig 9

AWS Service Integration

Step 1: Create an IAM role:

Fig 10

Fig 10

Fig 11 Type API Gateway and select the API gateway.

Fig 11 Type API Gateway and select the API gateway.

  • Select the default permissions shown in the list.
  • Give the role name as “api-gw-sns-role” and create the role.

Fig 12 In order to give additional permissions, select the role and click on in-line policy.

Fig 12 In order to give additional permissions, select the role and click on in-line policy.

  • Select JSON and enter the following code:
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "sns:Publish",
            "Resource": "*"
        }
    ]
}
  • Give the policy a name and select Create Policy

Step 2: Create Resources in the API gateway

  • Create a resource named ‘sns’ in the API gateway that we created earlier. Change the type to POST . Enter all the details and select the appropriate options shown in the image below.

Fig 13

Fig 13

Fig 14 The Execution role has the ARN (Amazon Resource Name) of the IAM role that we created.

Fig 14 The Execution role has the ARN (Amazon Resource Name) of the IAM role that we created.

  • In the Method Request option, add query strings. One is ‘Message’, other is ‘TopicArn’.

Fig 15

Fig 15

  • In the integration request, create another query string. In the ‘Mapped from’ option, enter ‘method.request.querystring.Message’. This will give the info about the how the data is mapped between the method request and the integration request. So the same for ‘TopicArn’ - ‘method.request.querystring.TopicArn’

Fig 16

Fig 16

  • Now deploy the API. Go the Actions dropdown and deploy the API. After there will be an invoke URL that will be generated.

Fig 17

Fig 17

Step 3: Test the API with all the resources that we created.

  • With the invoke URL we can access the resources that we want by adding the name of the resources at the end of the URL. For ex: add ‘/mock’ at the end of the URL. Below are the results.

Fig 18

Fig 18

Fig 19 Lambda function is now invoked which will display the IP of the requester in my case its my IP.

Fig 19 Lambda function is now invoked which will display the IP of the requester in my case its my IP.

  • We can test the sns API directly from the API console:

Fig 20 Click on the Test button to test this API.

Fig 20 Click on the Test button to test this API.

  • On in the Query strings, give the enter the parameters like - “TopicArn=arn:aws:sns:us-east-1:634211996823:API-messages&Message=I+have+mastered+Devops”. Here the TopiArn is the ARN of the SNS topic that we created and the Message is the message that we wish to send. Messages cant have spaces so instead ‘+’ is used.

Fig 21

Fig 21

Below message is the Notification received via the SNS to mail ID.

Fig 22

Fig 22

Clearing the AWS

  • All the services that were created can be deleted and its straight forward.
  • There will be some roles created by the lambda function automatically which should be tracked and deleted.

Untitled

  • Clear the Cloud Watch logs as well.

api-gateway-with-lambda's People

Contributors

karthi770 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.