Giter Site home page Giter Site logo

go-lambda's Introduction

go-lambda

Demo Project for Go using AWS Lambda

https://www.softkraft.co/aws-lambda-in-golang/

How to Deploy AWS Lambda on AWS

  1. Create Lambda function:
    1. In Console, search for Lambda.
    2. In Lambda page, choose Create function.
    3. In next page:
      1. Function name: helloLambda, for example.
      2. Runtime: your language, for example: Go.
      3. Execution role: Create a new role from aws policy templates
      4. Role Name: helloLambda-executor - and choose Simple microservice permission
  2. Creat API:
    1. In Console search for API Gateway
    2. In section of REST Api, click Build button.
    3. In the next page, choose:
      1. Protocol: REST
      2. Create new API: New API
      3. API name: helloLambdaAPI
    4. After click Create API button, on the next screen:
      1. Click select box Action and select Create API option.
      2. Select:
        1. Integration type: Lambda Function
        2. Use Lambda Proxy integration: check
        3. Lambda Function: helloLambda
        4. Use Default Timeout: check

With [[Go]]

Demo project: https://github.com/ducminhgd/go-lambda

Simple steps:

  1. Make sure that you install the dependencies go get -v all.
  2. Build project GOOS=linux go build -o build/main cmd/main.go.
  3. Zip binary to upload to AWS zip -jrm build/main.zip build/main
  4. If you want to handle request and response in your code, then please make sure that the field Use Lambda Proxy integration in Integration Request checked.

Sample code

package main

import (
	"context"
	"fmt"

	"github.com/aws/aws-lambda-go/events"
	"github.com/aws/aws-lambda-go/lambda"
)

func HandleRequest(ctx context.Context, req events.APIGatewayProxyRequest) (*events.APIGatewayProxyResponse, error) {
	name, found := req.QueryStringParameters["name"]
	if !found {
		name = "Anonymous"
	}
	resp := events.APIGatewayProxyResponse{Headers: map[string]string{"Content-Type": "text/plain"}}
	resp.StatusCode = 200
	resp.Body = string(fmt.Sprintf("Hello, %s! Welcome to AWS Lambda", name))
	return &resp, nil
}

func main() {
	lambda.Start(HandleRequest)
}

go-lambda's People

Contributors

ducminhgd avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.