Skip to content

Latest commit

 

History

History
112 lines (85 loc) · 1.68 KB

readme.md

File metadata and controls

112 lines (85 loc) · 1.68 KB

AWS Lambda Base Golang

Check Go

go 
#use brew
brew install go
brew upgrade go

go to Download

Create Path

mkdir ~/go
echo "export GOPATH=\$HOME/go" >> ~/.bash_profile
source ~/.bash_profile

Create Project

mkdir ~/go/helloworld
cd ~/go/helloworld

Touch File

touch helloworld.go

Install Lambda

go get github.com/aws/aws-lambda-go/lambda

HelloWorld

package main

import (
	"fmt"
	"github.com/aws/aws-lambda-go/lambda"
	)

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

func Hello()  {
	fmt.Println("Hello lambda")
}

Go Build

go build helloworld.go

Zip

zip helloworld.zip helloworld

Write lambda-trust-policy.json

{
	"Version": "2012-10-17",
	"Statement": {
  		"Effect": "Allow",
  		"Principal": {
    		"Service": "lambda.amazonaws.com"
  		},
  		"Action": "sts:AssumeRole"
	}
}

AWS Prepare

aws configure
aws ecr get-login --no-include-email --region "region_name"

aws iam create-role --role-name lambda-basic-execution--assume-role-policy-document file://lambda-trust-policy.json

aws iam attach-role-policy --role-name lambda-basic-execution --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

aws iam get-role --role-name lambda-basic-execution

#or

aws lambda create-function \
--function-name helloworld_go \
--zip-file fileb://helloworld.zip \
--handler helloworld \
--runtime go1.x \
--role "arn:aws:iam::<YOUR_ACCOUNT_ID>:role/lambda-basic-execution"

Invoke

aws lambda invoke \
--function-name helloworld_go \
--invocation-type "RequestResponse" \
response.txt