AWS API gateway path rather than query string - amazon-web-services

I am creating an API using AWS API Gateway and am currently using query strings to pass values into my lambda functions. This results in the URLs being structured like so:
users/user?user_id='test123'
What I would like to do is uses a path to pas in values rather than query strings. For example:
users/user/id/test123
I have looked at mapping templates and understand they are used to convert the data into the format for the function, but I am not sure how I can use a path and then map it into the lambda function.
Any ideas?

You can define a resource with a variable component path like this:
/users/user/id/{userid}
Then you'd use a mapping template to pass the userid value to the Lambda function in the body of the request:
#set($inputRoot = $input.path('$'))
{
"userId" : "$input.params('userid')"
}
Note that Lambda functions only accept parameters in the body, not query string.

Related

Does anyone know what should i use for validator in AWS AppConfig?

I am currentluy working on the AppConfig but don't know whether I should use a JSON schema validator or a lambda function as a validator. What is diffrenet between JSON schema and Lambda function? What benefit do they have?
I created a JSON schema and it's easy, but the file is large. I wonder if a lambda function could help and what the lambda function would look like.
The AppConfig JSON Schema Validator is a syntactic validation that allows you to check values in the JSON data itself. You can set constraints in your JSON Schema to check a value is not null, a number is within a certain range, a string has certain characters, etc.
The AppConfig Lambda Validator is more of a functional validation. Since Lambda allows you to do execute logic, you can use a Lambda Validator to run a query, do service discovery, or confirm the state of your resources before the new configuration data/feature flag is pushed out/deployed. A Lambda Validator will let you check pretty much anything. You can use a Lambda Validator to confirm syntactically the contents of the data too, but usually JSON Schema is better for that.
N.B.: I work on the AppConfig Team at AWS

AWS Api Gateway Primitive Type in URL passed to Lambda

I have a setup where we use API gateway, a json mapping template in the integration request where we access URL parameters as such:
"input":{
"my_int":"$input.params('someurlparam')"
},
I then access this variable in my Python lambda. This all works but I am getting inconsistent results in my lambda, sometimes the param is detected as an int and sometimes as a string. Different applications/testers accessing the api are getting different results and I have had to implement isinstance(my_int, str) checks and conversions in my lambda (I want to avoid this). How can I ensure the variable is parsed as an int prior to hitting my lambda?

How to modify a query parameter in AWS API Gateway in the integration request for an HTTP Proxy

I have a client facing API that takes a query parameter Time. The format is 14:00:00. Originally it would pass through this query parameter to the back end endpoint. However the back end endpoint (that I do not control) is now expecting time in the format 0001-01-01T14:00:00.
Is it possible to to modify the value of the query param before passing it on in AWS API Gateway?
I know you can modify the request body with a mapping template, and in the template you can access the queryParameters, but can you change them so that it modifies the actual request made to the back end?
I saw this:
https://forums.aws.amazon.com/thread.jspa?messageID=696524&#696524
but the user said he gave up trying to modify
Potential workaround I can think of right now are pass the parameters to a lambda and have the lambda build and make the request with modified values, with the response as the return value for the lambda
For now one can overrides query string in Mapping Templates using velocity templates e.g:
$context.requestOverride.querystring.time="_your_transformed_data_"
There is docs
I think you basically answered your own question :)
There is no way to transform query or header request parameters. All transformations need to happen in the body mapping template.
Best workaround would be to forward request on to a Lambda function to massage the parameters into the expected shape

Consuming RSS feed with AWS Lambda and API Gateway

I'm a newbie rails programmer, and I have even less experience with all the AWS products. I'm trying to use lambda to subscribe to and consume an rss feed from youtube. I am able to send the subscription request just fine with HTTParty from my locally hosted rails app:
query = {'hub.mode':'subscribe', 'hub.verify':'sync', 'hub.topic': 'https://www.youtube.com/feeds/videos.xml?channel_id=CHANNELID', 'hub.callback':'API Endpoint for Lambda'}
subscribe = 'HTTParty.post(https://pubsubhubbub.appspot.com/subscribe, :query=>query)
and it will ping the lambda function with a get request. I know that I need to echo back a hub.challenge string, but I don't know how. The lambda event is empty, I didn't see anything useful in the context. I tried formatting the response in the API gateway but that didn't work either. So right now when I try to subscribe I get back a 'Challenge Mismatch' error.
I know this: https://pubsubhubbub.googlecode.come/git/pubsubhubbub-core-0.3.html#subscribing explains what I'm trying to do better than what I just did, and section 6.2.1 is where the breakdown is. How do I set up either the AWS Lambda function and/or the API Gateway to reflect back the 'hub.challenge' verification token string?
You need to use the parameter mapping functionality of API Gateway to map the parameters from the incoming query string to a parameter passed to your Lambda function. From the documentation link you provided, it looks like you'll at least need to map the hub.challenge query string parameter, but you may also need the other parameters (hub.mode, hub.topic, and hub.verify_token) depending on what validation logic (if any) that you're implementing.
The first step is to declare your query string parameters in the method request page. Once you have declared the parameters open the integration request page (where you specify which Lambda function API Gateway should call) and use the "+" icon to add a new template. In the template you will have to specify a content type (application/json), and then the body you want to send to Lambda. You can read both query string and header parameters using the params() function. In that input mapping field you are creating the event body that is posted to AWS Lambda. For example: { "challenge": "$input.params('hub.challenge')" }
Documentation for mapping query string parameters

How do I pass GET parameters to my AWS Lambda function when using an HTTP endpoint?

I followed the example from here to set up a sample AWS Lambda function: http://docs.aws.amazon.com/lambda/latest/dg/get-started-step4-optional.html.
Then, I created an HTTP GET endpoint via AWS API Gateway. I can access the endpoint but I don't know how to pass that int myCount as a parameter.
I tried ?myCount=3 as a GET parameter but that didn't work.
Any help?
You need to setup a mapping template in API Gateway. If you know the name of your parameters ahead of time your template could look like this:
{
"myCount": "$input.params('myCount')",
"myUserId": "$input.params('myUserId')"
}
Where each $input.params('...') will get evaluated and the value in your query string will be put in its place when the event is passed to Lambda.
This is pretty much a duplicate of passing query params for aws lambda function
In order to send HTTP parameters to a lambda function, they need to be sent in the request body via a mapping template.
See this blog post for a simple example of how to do this.
This is another way to do it:
Open API Gateway, select your endpoint and click on Resources
Select Method Request in the settings
Under the URL Query String Parameters, you can add query string.
As an example, in my Python lambda function, I can retrieve the query parameter just with the following:
def endpoint(event, context):
my_parameter = event["queryStringParameters"]