I wrote a REST API using AWS API Gateway and Lambda, so make a note of it. Is it possible to avoid setting up a server on EC2 by using API Gateway and Lambda in combination?
First, sign in to the AWS Management Console and then select AWS Lambda. If you've never created a Lambda function, select the Get Started Now button.
Press the Get Started Now button to see a sample list of Lambda functions. This time we will create a new one, so select the "Skip" button at the bottom right.
If you select the "Skip" button, the screen for creating a Lambda function will appear, so we will describe the details.
First, specify the function name and language. Specify "hello_world" for the function name and "Python 2.7" for the language.
Next is the actual code. Paste the code below into your inline editor. Simple code that just returns {'message':'Hello World!'}.
import json
def lambda_handler(event, context):
return {'message': 'Hello World!'}
Next, set the role. You can use an existing role, but this time we will create a new one. Select the Role dropdown and select "Basic execution role".
If you select "Basic execution role", the following screen will open. Here, the IAM role is "Create a new IAM role" and the role name is "lambda_basic_execution". When you're done, select the Allow button to create the role.
Click the "Allow" button to return to the original screen. Since we will not set the memory or timeout time this time, just select the "Next" button.
When you select the "Next" button, a confirmation screen will be displayed. Select the Create Function button to create your Lambda Function.
This completes the creation of the Lambda function. Select the "Test" button and check if the "Execution Result" is as follows.
{
"message": "Hello World!"
}
After creating the Lambda function, go back to the management console home screen and select API Gateway. If you have never created an API, select the Get Started Now button.
The screen for entering the API name will be displayed. Enter "Hello API" for the API name, and then select the "Create API" button.
Then create the resource. Select the "Create Resource" button. Then enter Hello for the Resource Name and select the Create Resource button.
After creating the resource, assign methods to the created resource. Make sure the "/ hello" you created is selected, then select the "Create Method" button. Then a dropdown will appear under "/ hello", so select "GET".
After that, if you select the check button (☑) to the right of the method name, you will be able to select Lambda Function or HTTP Proxy as the Integration Type, so select Lambda Function. For Lambda Region, select the region where you created the Lambda function, and for Lambda Function, enter the name of the Lambda function you created earlier, "hello_world." After setting everything, select the "Save" button.
When you see "Add Permission to Lambda Function", select "OK". This completes the creation.
Try to test the API you created by selecting the "Test" button. If you look at the Response Body, you'll see that the Lambda function is running.
{
"message": "Hello World!"
}
Select the "Deploy API" button on the left side of the screen.
When you select the "Deploy API" button, the following screen will appear. Enter New Stage as the Deployment Stage and beta as the Stage name and select the Deploy button.
The Invoke URL is displayed.
Please add "/ hello" after beta in the URL before accessing. If the following is displayed, it is successful. Thank you for your hard work.
{"message": "Hello World!"}
Recommended Posts