AWS API Gateway - How to create a post method and test it - amazon-web-services

My scenario is when my Lambda function's Api endpoint is hit, with a post variable, i want my Lambda to invoke a database connection and get some results.
So the first step is to create an API Gateway endpoint and I have problem creating the POST method with parameters
Here is what i did till now:
Created a API Endpoint for my Lambda Function.
When i go into the POST Method in Lambda's API endpoint section, i am taken to this screen
When i Test the function from the AWS Console,
and response is as follows in the console
How do i create parameters for the api here? and how do i test it? I have my lambda function ready, that takes in a String as input and returns a String as response.
Can someone guide me for the next steps..

You can check the Create and Test a POST Method
you do not mention the parameters in the lambda definition, the parameter will be taken from the request directly (so in future if you change your lambda code to take new parameters, you dont need to change the configuration
To test :
In the Method Execution pane, in the Client box, and then choose TEST.
Expand Request Body, and type the following:
{
"name": "User" <or any other parameter that you expect>
}
Choose Test. If successful, Response Body will display the reply from
your lambda execution
For example here an example post api I had
so again for POST API you dont define the parameters
If you were doing GET API you would define the query parameters

Great step by step explanation of Serverless API on AWS: Serverless API on AWS in 10 minutes

The answer by Frederic is very good, I'll just leave another example in the docs -> http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html
Further down on that page is an example for Param Mapping which will proxy any incoming parameters to Lambda. You still need to explicitly state any incoming parameters that you expect clients to send in the Method Request.

Related

Pass Data to Step Function From HTTP API Gateway

I have a Step Function integration with one of my routes in API Gateway. My API is an HTTP API. It has the following settings:
This is successfully invoking my StepFunction but I am unable to access any of my body parameters. This route is being invoked from a front-end form which submits data. I would like to use this data for further processing in my step functions.
When I see my step function execution, the input is just {}, is there something I need to change?
In the advanced settings set a value for "Input - optional" of "$request.body"

How to make API calls to AWS Lambda using API Reference

I want to handle (create, delete, invoke, list down, update) AWS Lambda functions using REST API calls. I don't have intentions to use AWS console or AWS CLI. Some content in the documentation is unclear to me. I want to know the following things.
Base path or Host path for the API calls
Example request for invoking a lambda function
Base path for API calls will be in this format:
https://lambda.us-west-2.amazonaws.com
So the full path will be
https://lambda.us-west-2.amazonaws.com/2015-03-31/functions/FunctionName/invocations?Qualifier=Qualifier
where the definition for FunctionName and Qualifier can be
found here.
You need to sign the HTTP request using Signature Version 4 with
valid AWS IAM credentials before sending it. The signing process can be found here. Example code for the signing process using Python is given here. Then follow instructions in here to make the invocation request.

SurveyMonkey - Create Webhook with AWS API Gateway as Subscription Url

I am trying to Integrate SurveyMonkey with my backend.
This is the flow:
(1) We create a webhook(https://developer.surveymonkey.com/api/v3/#webhooks) using POST method from PHP, with Subscription url as an AWS API Gateway.
(2) When user completes a survey, SurveyMonkey invokes the subscription Url (API Gateway), which in turn calls a Lambda function to update the database.
The problem is when i try to give a API Gateway url as the subscription Url, SurveyMonkey returns a Bad Request response. If i use a general .php page, the webhook is getting created successfully.
In the conversation with SurveyMonkey Support Team, they say, the API Gateway should return a 200 response for HEAD request. I am not sure how to set this up in AWS.
Can anyone who has implemented / integrated, API Gateway with SurveyMonkey please guide me on how to solve this issue?
Here is the conversation with SurveyMonkey Support Team:
Thanks for getting back in touch. I just spoke with my colleagues on
the development team and they noted that there shouldn't be an issue
in using a lamda function for your subscription_URL. As long as it
accepts HEAD and POST requests and returns a 200. I hope this helps
you out; of course, please let me know if I can assist further.
Thanks for getting back in touch. I wanted to confirm that the lambda
function needs to return a 200 to a HEAD request in order for us to
enable the webhook properly. This isn't done after the webhook is
cerated, but as the webhook is created. It still seems like the
lambda function isn't responding, or responding correctly, as it is
created, so it will continue to fail. I hope this helps clarify;
please let me know if I can assist further.
One way or another, you need to get the API Gateway to respond to HEAD requests. There are a lot of ways to do this. You could add a HEAD method to the resource of your URL. You can do that in the API Gateway console under the resource section when you're editing you api. You can send the HEAD requests to a lambda function and have that function return a 200 status code for appropriate HEAD requests. You can set up a mock return in the api integration request, but you'll need to set up the correct integration details. You can also just set ANY lambda proxy integration and deal with everything on the lambda side.
Once you have something setup you should be able to test with curl:
curl -I http://yourUrl.example.com

AWS API Gateway Get Method How to Pass API Key?

I have a working AWS API Gateway GET method. I want to secure it using an API key, so I've created an API key and usage plan, etc.
So previously when I wanted to call the GET method, I would just type a URL with the appropriate parameters into the browser and send it. However, now that we've introduced the API key into the mix, I'm not sure how to call it.
I'm aware of the command line GET and curl tools. Which of these 3 tools (browser, GET, CURL) can accomplish this task and what syntax do I need to use to make the request to the GET method passing the API key?
Mark B is right, I'm just copying because it's the right answer.
You must pass an HTTP header named x-api-key with the API Key as the value. One tool is cURL, another is Postman.

how to pass the variable from api gateway to the url of another service running on EC2

I am new to AWS. I am asked to use the services that is written in java and already exist in java EC2 to serve the UI. I need to use API gateway to call the services so the UI will first call the API gate way and then API gate way will call my service. Also I know that in Select Integration Request if I choose http proxy and add the url of my services on Ec2 I can achieve this. However the problem is that I have a service like this:
http://domainname/article/{id}
As you can see the id is a variable and the above service should be called via API Gateway so API gate way should be able to pass the id to the service url. Lets say the following is the url of API gateway:
https://my-api-id.execute-api.region-id.amazonaws.com/stage-name/article?id=1
How can I handle the above scenario? is it possible to do that?
While you can use a Lambda function for this, you can also do it directly from API Gateway using basic request mapping.
Define your API in API Gateway with a resource having path /article
Add a GET method.
In the Method Execution pane, choose Method Request.
Expand "URL Query String Parameters"
Hit "Add query string"
Type id as the name and hit the checkbox on the right to save
Go back to the Method Execution pane, choose Integration Request.
Edit your endpoint URL to add the path parameter, if you have not already done so.
The URL should be: http://domainname/article/{id}
Choose the arrow next to "URL Path Parameters" to expand that section.
Click "Add path".
Enter id as the name
Under "Mapped from" enter: method.request.querystring.id
Click the check box on the right to save.
Go back to Method Execution and click Test.
Add an id value under query strings and click test.
There are some similar examples in the documentation here
I would suggest using Lambda to re-assemble the request. API Gateway will invoke the Lambda function that would receive the request
http://domainname/article/1
and will perform an HTTP request to your service
https://my-api-id.execute-api.region-id.amazonaws.com/stage-name/article?id=1