[Python] I wrote a REST API using AWS API Gateway and Lambda.

Introduction

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?

procedure

  1. Creating a Lambda function
  2. Creating an API with API Gateway
  3. Deploy the created API

Creating a Lambda function

Go to Lambda screen

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. get-started.JPG

Creating a Lambda function

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. skip.JPG

If you select the "Skip" button, the screen for creating a Lambda function will appear, so we will describe the details. configure.JPG

Function name and language input

First, specify the function name and language. Specify "hello_world" for the function name and "Python 2.7" for the language. func_name.JPG

Enter code

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!'}

Role selection

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". select_role.jpg

Creating a 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. create_role.JPG

final confirmation

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. review1.JPG

Test 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!"
}

Creating an API with API Gateway

Go to API Gateway screen

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. get-started-api.JPG

Enter API name

The screen for entering the API name will be displayed. Enter "Hello API" for the API name, and then select the "Create API" button. new-api.JPG

Create resource

Then create the resource. Select the "Create Resource" button. Then enter Hello for the Resource Name and select the Create Resource button. create_resource.JPG

Create method

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". create_method.jpg

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. save_api.JPG

When you see "Add Permission to Lambda Function", select "OK". This completes the creation.

Test your API

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!"
}

API deployment

Select the "Deploy API" button on the left side of the screen. deploy_api.JPG

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. deploy.JPG

The Invoke URL is displayed. url.JPG

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

[Python] I wrote a REST API using AWS API Gateway and Lambda.
Amazon API Gateway and AWS Lambda Python version
AWS Amplify + API Gateway + Lambda + Python returns a normal response
I made a Chatbot using LINE Messaging API and Python
I made a Chatbot using LINE Messaging API and Python (2) ~ Server ~
I wrote a class in Python3 and Java
LINE BOT with Python + AWS Lambda + API Gateway
I wrote AWS Lambda, and I was a little addicted to the default value of Python arguments
AWS CDK-Lambda + API Gateway (Python)
Try implementing a Cisco Spark bot with AWS Lambda + Amazon API Gateway (Python)
Quickly take a query string with API Gateway-> Lambda (Python)
Summary if using AWS Lambda (Python)
I made a Line-bot using Python!
[AWS] Create API with API Gateway + Lambda
I wrote a Python script that exports all my posts using the Qiita API v2
I wrote a script to create a Twitter Bot development environment quickly with AWS Lambda + Python 2.7
I stopped my instance at a specific time using AWS Lambda
I tried ChatOps with Slack x API Gateway x Lambda (Python) x RDS
LINE BOT (Messaging API) development with API Gateway and Lambda (Python) [Part 2]
[AWS] Using ini files with Lambda [Python]
I tried using Twitter api and Line api
I tried using UnityCloudBuild API from Python
Easy REST API with API Gateway / Lambda / DynamoDB
[AWS] Create a Python Lambda environment with CodeStar and do Hello World
I tried to make "Sakurai-san" a LINE BOT with API Gateway + Lambda
Development and deployment of REST API in Python using Falcon Web Framework
I tried to create a sample to access Salesforce using Python and Bottle
I wrote FizzBuzz in python using a support vector machine (library LIVSVM).
I want to make a web application using React and Python flask
I wrote a Slack bot that notifies delay information with AWS Lambda
I tried using AWS Rekognition's Detect Labels API
Periodically run a python program on AWS Lambda
[AWS SAM] Create API with DynamoDB + Lambda + API Gateway
I tried web scraping using python and selenium
A memo that I wrote a quicksort in Python
I tried object detection using Python and OpenCV
Let's create a REST API using SpringBoot + MongoDB
I tried reading a CSV file using Python
[AWS] Try tracing API Gateway + Lambda with X-Ray
Create a Mac app using py2app and Python3! !!
Try using ChatWork API and Qiita API in Python
I made a Python Qiita API wrapper "qiipy"
Create a REST API using the model learned in Lobe and TensorFlow Serving.
A story about cross-compiling a python package for AWS Lambda and deploying it serverless
Develop, run, and deploy AWS Lambda remotely using lambda-uploader
Dynamic HTML pages made with AWS Lambda and Python
I tried to notify the update of "Become a novelist" using "IFTTT" and "Become a novelist API"
Try creating a compressed file using Python and zlib
I made a login / logout process using Python Bottle.
A python lambda expression ...
Get LEAD data using Marketo's REST API in Python
Check types_map when using mimetypes on AWS Lambda (Python)
Send and receive Gmail via the Gmail API using Python
Register a ticket with redmine API using python requests
Try running a Schedule to start and stop an instance on AWS Lambda (Python)
I tried to get an AMI using AWS Lambda
Implementing a generator using Python> link> yield and next ()> yield
I made a LINE BOT with Python and Heroku
I tried APN (remote notification) using Parse.com REST API
Create a Layer for AWS Lambda Python with Docker
I want to AWS Lambda with Python on Mac!