API gateway proxy integration with lambda function in CDK - amazon-web-services

When I set CDK as following , and deploy them. the two content was generated in api gateway.
new LambdaRestApi(this,"api",{
handler:lambdaFunction
});
I totally beginner this kind of API manipulation and have questions.
① what is {proxy+} ?
② what is the difference between following two API ?
③ How can I see payload which will be passed to lambda function ?
If someone has opinion or materials please let me know.
Thanks

The purpose of the proxy+ is to enable the following URLs to work with your function:
https://44444.execute-api.gggg.amazonaws.com/test-invoke-stage/some/path1/path3
https://44444.execute-api.gggg.amazonaws.com/test-invoke-stage
https://44444.execute-api.gggg.amazonaws.com/test-invoke-stage/test/gggg
https://44444.execute-api.gggg.amazonaws.com/test-invoke-stage/test/5
without proxy+ only the following will work:
https://44444.execute-api.gggg.amazonaws.com/test-invoke-stage
So the proxy+ is able to accept everything past /test-invoke-stage as it matches every path starting with /test-invoke-stage.

Related

API Gateway not passing event to lambda from console?

I'm trying to call a basic lambda from my API Gateway console. The lambda has an input taken in by the event called filterBy.
I created a query string called filterBy however, when I try to invoke the lambda I get the error:
{errorMessage: {'statusCode': 500, 'body': '{"msg": "KeyError(\'filterBy\')"}'}}
Presumably because the piece of code in my lambda
event['filterBy'] is not finding a filterBy in the event. What do I need to do so that I can get the filterBy in the event from the API Gateway console? I understand this is probably quite simple but I surprisingly cannot find anything about this so any help would be appreciated.
Based on the integration type, approach can be vary.
1. Lambda custom integrations:
Looks like you are trying to use Lambda custom integrations. If that is the case you have to add a mapping template as below.
(under the Integration Request -> Mapping Templates --> Add mapping template)
{
"filterBy": "$input.params('filterBy')"
}
Please refer this article or this video for more info.
2. Lambda proxy integrations:
If you are using Lambada proxy integration (either as REST API or as HTTP APIs), then instead of event['filterBy'], you have to access the queryStringParameters first and then access the relevant query param.
event['queryStringParameters']['filterBy']
And another thing is: once you modify something in API GW, please make sure to deploy and wait some time before test. :)

AWS API Gateway concatenate query string parameter

I am trying to have an API Gateway that can publish to multiple SNS and choose which one it publishes to by sending the name of the topic as a header. Then I want to concatenate it to make the TopicArn in the integration request.
For example with a header 'topic' I want to put something like this in 'mapped from':
'arn:aws:sns:xx-xxxx-1:1234567:{method.request.header.topic}'
But that doesn't work. Maybe I should use the mapping but don't know how to do it.
I have tried lots of things but am just stuck. Would love to find a good code example somewhere but any help at all would be appreciated

Set the HTTP Endpoint URL value for REST API from AWS CDK

I am trying to have a serviceHost stage variable to be set for each request from API GATEWAY, exactly like picture attached below.
According to doc https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-set-stage-variables-aws-console.html we can have something like this from console, but since my app is totally on CDK so just wanted to figure out a way to have it configured through CDK itself.
Couldn't find that in https://docs.aws.amazon.com/cdk/api/latest/docs/#aws-cdk_aws-apigateway.IntegrationOptions.html or anywhere.
Is it possible to attain through CDK somehow.
You can set the stage variables when declaring a stage. As per the documentation:
import aws_cdk.aws_apigateway
my_stage = aws_cdk.aws_apigateway.Stage(
self,
"my_stage",
variables = {"serviceHost": "my_value"}
)

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 to setup AWS API gateway resource to take matrix parameters?

I have been trying to setup a resource using AWS API Gateway but I can't seem to find a way to either set or access matrix parameters.
I want to be able to set up a resource similar to the following -
GET /image;height=750;width=1000;format=png
Is it possible ?
You need to configure the setup to use Query Parameters.
You do this in the Method Request area of a method configuration from within the console:
https://console.aws.amazon.com/apigateway/home?region=<region-id>#/restapis/<api-id>/resources/<resource-id>/methods/<method-type>
You can also do this use the AWS API Gateway HTTP API putMethod endpoint, or the AWS#APIGateway#putMethod call in any of their SDKs.
API Gateway currently does not support matrix parameters. As a workaround, you could use query parameters as already mentioned and parse them in your backend.
Best,
Jurgen
I realize that this is a very old question. Leaving my response in case someone has a similar challenge
There are multiple ways to set this up. It all comes down to the context in which the API is intended to be used.
While query parameters will solve the problem, they are not the best suited for representing a resource. They fit well with scenarios that involve filtering. If this API is intended to be used as a source for <img /> tags on the UI, this pattern GET .../images/{widthxheight}/{imageName}.{extension} can be used.
Ex: GET .../images/200x400/sponge-bob.png
However, if the intent is for this API to be used for the purpose of looking up. the below definition can be used -
POST .../image-results
Content-Type: application/json
{
"name":"sponge bob",
"height": 400,
"width": 200,
"format": "png"
}