I just did FizzBuzz with AWS Lambda

Backgroud I have more chances to hear about serverless applications using AWS Lambda, so I tried making it.

Preparetion

Development (lambda only)

import json

def lambda_handler(event, context):
    
    request = "[inner_test]"
    num = 30

    doc = {
        "message":'Hello from Lambda!',
        "request":process(num)
    }
    
    # TODO implement
    return {
        'statusCode': 200,
        'body': json.dumps(doc)
    }


def process(src):
    
    if src % 15 == 0:
        return "FizzBuzz"
    elif src % 5 == 0:
        return "Buzz"
    elif src % 3 == 0:
        return "Fizz"
    else :
        return src

After writing, press "Deploy"-> "Test".

Then ... The execution result is output in the log.

Development (with API Gateway) The previous configuration was only lambda, but here I will make a request from the outside using API Gateway.

First, select API Gateway from the trigger. Security is your choice.

Select "Stage"-> "POST" to get the URL. The structure of the URL itself is https: // {restapi_id} .execute-api. {region} .amazonaws.com/ {stage_name} /

So, get the value requested by lambda and FizzBuzz. Speaking in the codebase, it parses event ["body "] with json and gets the input value.

import json

def lambda_handler(event, context):
    
    request = "[inner_test]"
    num = 30
    
    #Supports API Gateway
    #Get the value of the request here
    if "body" in event.keys():
        request = json.loads(event["body"])
        num = request["num"]
        
    doc = {
        "message":'Hello from Lambda!',
        "request":process(num)
    }
    
    # TODO implement
    return {
        'statusCode': 200,
        'body': json.dumps(doc)
    }


def process(src):
    
    if src % 15 == 0:
        return "FizzBuzz"
    elif src % 5 == 0:
        return "Buzz"
    elif src % 3 == 0:
        return "Fizz"
    else :
        return src

Try hitting the API using Postman to see if the fizzbuzz value is actually returned. If you change the value of num, either Fizz``Buzz``FizzBuzz or a number will be returned.

It's done.

Future I thought I should use the minimum VPS and drop the necessary packages without saying serverless, but I could do it in no time. If you link with S3 after lambda processing, it seems that you can leave the data.

Reference Call REST API on Amazon API Gateway

Recommended Posts

I just did FizzBuzz with AWS Lambda
I just built a virtual environment with AWS lambda layer
I tried connecting AWS Lambda with other services
I want to AWS Lambda with Python on Mac!
[AWS] Create API with API Gateway + Lambda
Using Lambda with AWS Amplify with Go
Notify HipChat with AWS Lambda (Python)
[AWS] Using ini files with Lambda [Python]
I want to play with aws with python
What I did with a Python array
[AWS] Link Lambda and S3 with boto3
I just did pip install pyrebase ... (UnicodeDecodeError)
[AWS] Do SSI-like things with S3 / Lambda
Python + Selenium + Headless Chromium with aws lambda
I tried to delete bad tweets regularly with AWS Lambda + Twitter API
I wrote a Slack bot that notifies delay information with AWS Lambda
FizzBuzz with Python3
[AWS SAM] Create API with DynamoDB + Lambda + API Gateway
Regular serverless scraping with AWS lambda + scrapy Part 1.8
Serverless scraping using selenium with [AWS Lambda] -Part 1-
LINE BOT with Python + AWS Lambda + API Gateway
What I did when I got stuck in the time limit with lambda python
Serverless application with AWS SAM! (APIGATEWAY + Lambda (Python))
[AWS] Try tracing API Gateway + Lambda with X-Ray
Infrastructure construction automation with CloudFromation + troposphere + AWS Lambda
I wanted to operate google spread sheet with AWS lambda, so I tried it [Part 2]
[AWS] I made a reminder BOT with LINE WORKS
Dynamic HTML pages made with AWS Lambda and Python
I tried Amazon Comprehend sentiment analysis with AWS CLI.
I tried running TensorFlow in AWS Lambda environment: Preparation
I tried to get an AMI using AWS Lambda
Deploy Python3 function with Serverless Framework on AWS Lambda
Create a Layer for AWS Lambda Python with Docker
Manage your Amazon CloudWatch loggroup retention with AWS Lambda
Make ordinary tweets fleet-like with AWS Lambda and Python
[Introduction to AWS] I tried playing with voice-text conversion ♪
I want to bind a local variable with lambda
[AWS] Play with Step Functions (SAM + Lambda) Part.2 (Parameter)
Make Lambda Layers with Lambda
I made an IFTTT button that unlocks the entrance 2 lock sesame with 1 button (via AWS Lambda)
I played with wordcloud!
I tried AWS CDK!
AWS CDK with Python
I tried AWS Iot
I touched AWS Chalice
I wrote a script to create a Twitter Bot development environment quickly with AWS Lambda + Python 2.7
Try AWS Lambda Destinations
Fizzbuzz with exception handling
[AWS] What to do when you want to pip with Lambda
[AWS] Try adding Python library to Layer with SAM + Lambda (Python)
Output CloudWatch Logs to S3 with AWS Lambda (Pythyon ver)
Challenge problem 5 with Python: lambda ... I decided to copy without
What I did to welcome the Python2 EOL with confidence
Try automating Start / Stop for EC2 instances with AWS Lambda
I tried to sort a random FizzBuzz column with bubble sort.
Create API with Python, lambda, API Gateway quickly using AWS SAM
Site monitoring and alert notification with AWS Lambda + Python + Slack
[AWS] I made a reminder BOT with LINE WORKS (implementation)