I want to create an API without using a server. Confirm that the request is thrown and returned from the EC2 instance to the REST API created by API Gateway.
Let's create a REST API using Amazon API Gateway and Lambda.
[1. Create a Lambda function](# 1-lambda% E9% 96% A2% E6% 95% B0% E3% 82% 92% E4% BD% 9C% E6% 88% 90% E3% 81% 99 % E3% 82% 8B) [2. Create REST API with API Gateway](# 2-api-gateway% E3% 81% A7rest-api% E3% 82% 92% E4% BD% 9C% E6% 88% 90% E3% 81% 99% E3% 82% 8B) [3. Deploy REST API](# 3-rest-api% E3% 82% 92% E3% 83% 87% E3% 83% 97% E3% 83% AD% E3% 82% A4% E3% 81 % 99% E3% 82% 8B) [4. Create a second Lambda function](# 4-2% E7% 95% AA% E7% 9B% AE% E3% 81% AElambda% E9% 96% A2% E6% 95% B0% E3% 82% 92% E4% BD% 9C% E6% 88% 90% E3% 81% 99% E3% 82% 8B) [5. Add resources, methods and parameters to REST API with API Gateway](# 5-api-gateway% E3% 81% A7rest-api% E3% 81% AB% E3% 83% AA% E3% 82% BD% E3% 83% BC% E3% 82% B9% E3% 83% A1% E3% 82% BD% E3% 83% 83% E3% 83% 89% E3% 83% 91% E3% 83% A9% E3% 83% A1% E3% 83% BC% E3% 82% BF% E3% 82% 92% E8% BF% BD% E5% 8A% A0% E3% 81% 99% E3% 82% 8B)
Reference: https://docs.aws.amazon.com/ja_jp/apigateway/latest/developerguide/apigateway-getting-started-with-rest-apis.html
When you create the API Gateway, you need to specify the Lambda function, so create the Lambda function first.
Select "Function"-> "Create Function" from the Lambda console screen. Select "Create from scratch" on the function creation screen.
Function name: my-function Runtime: Python 3.7 And press "Create Function".
If the message "The function my-function was created successfully" appears, the function creation is complete.
By the way, the created function returns "Hello from Lambda!" By default.
Click "Create API" from the console screen of API Gateway. Since this is a verification, select "Build" in "REST API Private".
A pop-up will appear, so select OK.
Set as shown in the image below. After completing the selection and input, click "Create API".
When completed, only "/" will be displayed. This "/" is the root level resource and corresponds to the URL of the API base path.
Next, add a method. Select Actions> Create Method.
Select "GET" and press the check mark.
Then, the GET setup screen will appear, so enter the necessary information and "Save".
The set contents are displayed. Please refer to Official for details of each item.
Creating the REST API is now complete.
To actually use it, you need to deploy the REST API you created.
Select Actions> Deploy API for the REST API you created.
A pop-up will appear, so select "New Stage" and Set the stage name to "dev" and press "Deploy".
Then, the stage editor screen is displayed and you can actually send a request to the API with the URL of the URL call.
Let's actually make a request from an EC2 instance.
$ curl -X GET 'URL of the call'
"Hello from Lambda!"
It is OK if the response "Hello from Lambda!" Returned by the set Lambda is returned.
Next, we will also perform a pattern with parameters in the GET request. In the previous API, Lambda now returns a fixed value of'Hello from Lambda!'. So, this time, I will create an API with Lambda that puts the parameters of the received GET request in the response.
Create a Lambda function in the same way as before. Function name: my-function2 Runtime: Python 3.7 Run Role: Select the role you just created in Use Existing Role.
When the creation is complete, rewrite the function code.
Put the received parameters in the response.
lambda_function.py
import json
def lambda_handler(event, context):
myParam = event['myParam']
return {
'statusCode': 200,
'body': json.dumps(myParam)
}
Lambda settings are complete.
We will create resources with API Gateway.
"Action"-> "Create Resource"
Resource name: my-resource → "Create resource"
Next, we will create the method. With my-resource selected, "Action"-> "Create Method"
Select "GET" and confirm with the check button.
The my-resource GET setup screen will appear, so select the "my-function2" created earlier for the Lambda function and save it.
A pop-up for adding authority will appear, so click "OK".
Select "Integration Request" on the method execution screen.
Since there is a "mapping template" setting at the bottom, Request body passthrough: If no template is defined (recommended) Content-Type:application/json Is specified.
Enter the template and save it.
template
{
"myParam": "$input.params('myParam')"
}
Now, you can get the item "myParam" of the request parameter by event ['myParam'] on the Lambda function side.
lambda_function.py
import json
def lambda_handler(event, context):
myParam = event['myParam'] #← here
return {
'statusCode': 200,
'body': json.dumps(myParam)
}
This completes the settings on the API side, so let's deploy and execute it as before.
Try to request by adding "my-resource? MyParam = Hello% 20from% 20API% 20Gateway!" To the parameter.
$ curl -X GET 'https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/my-resource?myParam=Hello%20from%20API%20Gateway!'
{"statusCode": 200, "body": "\"Hello from API Gateway!\""}
I was able to confirm that the "Hello from API Gateway!" Assigned to the request parameter responds as a body.
This time, we implemented it with the minimum necessary settings, so it seems that more research will be required when introducing it, but I think that we have grasped the whole picture of API Gateway + Lambda as serverless.
Recommended Posts