Setting up the API Gateway and integrating it with a Lambda proxy

Now that we know the Lambda function works with some API Gateway test data, and returns a header and body with a statusCode of 200, we just need to add the API Gateway that will invoke the Lambda function, as shown in the following diagram:

Perform the following steps:

  1. Sign in to the AWS Management Console and open the API Gateway console at https://console.aws.amazon.com/apigateway/.
  2. Choose Get Started or, in the Amazon API Gateway navigation pane, choose APIs and choose Create API.
  3. On the Create page, perform the following steps:
    1. In Choose Protocol, select REST
    2. In Create new API, select New API
    3. Under Settings, type metrics for API name
    4. Choose Regional for Endpoint Type
    5. Choose Create API
  4. Choose Create Resource from the Actions drop-down menu.
  5. In the New Child Resource window, perform the following steps:
    1. In Resource Name, type visits
    2. In Resource Path, type visits
    3. Select Enable API Gateway CORS
    4. Choose Create Resource.
  6.  Select the /visits resource and choose Create Resource from the Actions drop-down menu.
  7. In the New Child Resource window, perform the following steps:
    1. In Resource Name, type {resourceId}
    2. In Resource Path, type {resourceId}, replacing the default -resourceId- value
    3. Check Enable API Gateway CORS
    4. Choose Create Resource
  8. Select the /Vists/{resourceId} resource and choose Create Method from the Actions drop-down menu.
  9. Choose GET in the dropdown and then the checkmark to its right.
  10. Choose the GET resource method in the /visits/{resourceId} - GET - Setup window:
    1. In Integration type, choose Lambda Function
    2. Check Use Lambda Proxy integration
    3. In Lambda Region, select your region from the dropdown
    1. In Lambda Function, type lambda-dynamo-data-api
    2. Check Use Default Timeout
    3. Choose Save
  1. Choose OK in the Add Permission to Lambda Function. This will allow API Gateway to invoke the Lambda function.

You should now have an API Gateway GET - Method Execution that looks like the following screenshot:

Finally do a quick test to make sure it works before we deploy it by performing the following steps:

  1. Choose /visits/{resourceId} - GET in the Resources menu on the left
  2. Choose Test
  3. Type 324 under Path {resourceId}
  4. Choose Test

You should see the status 200, latency, logs, and JSON response body, as shown in the following code:

[ 
{
"EventCount": 3,
"EventDay": 20171001,
"EventId": "324"
},
{
"EventCount": 5,
"EventDay": 20171002,
"EventId": "324"
}
]

If you don't get a 2XX status code, then look at the logs, which will help you diagnose the issue. It will probably be linked to security IAM roles.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset