Giter Site home page Giter Site logo

sbstjn / go-lambda-example Goto Github PK

View Code? Open in Web Editor NEW
51.0 2.0 16.0 61 KB

Example AWS Lambda function in Go and SAM

Home Page: https://sbstjn.com/golang-lambda-with-aws-sam-serverless-application-model.html

Makefile 24.04% Go 75.96%
golang lambda sam serverless cloudformation apigateway

go-lambda-example's Introduction

Lambda Go Example

Dependecies

$ > go get github.com/aws/aws-lambda-go/events
$ > go get github.com/aws/aws-lambda-go/lambda
$ > go get github.com/stretchr/testify/assert

Build

# Build binary
$ > make build

# Test Go Code
$ > make test

Deploy

Create .env

AWS_ACCOUNT_ID=1234567890
AWS_BUCKET_NAME=your-bucket-name-for-cloudformation-package-data
AWS_STACK_NAME=your-cloudformation-stack-name
AWS_REGION=us-west-1

Install AWS CLI

$ > brew install awscli

Command

# Create S3 Bucket
$ > make configure

# Upload data to S3 Bucket
$ > make package

# Deploy CloudFormation Stack
$ > make deploy

Usage

$ > make outputs

[
  {
    "OutputKey": "URL",
    "OutputValue": "https://random-id.execute-api.us-west-1.amazonaws.com/Prod",
    "Description": "URL for HTTPS Endpoint"
  }
]

$ > curl https://random-id.execute-api.us-west-1.amazonaws.com/Stage/people

{"data":[{"id":"d1","name":"Anton","age":31},{"id":"c2","name":"Frank","age":28},{"id":"b1","name":"Horst","age":42}]}

$ > curl https://random-id.execute-api.us-west-1.amazonaws.com/Stage/person/b1

{"data":{"id":"b1","name":"Horst","age":42}}

go-lambda-example's People

Contributors

sbstjn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

go-lambda-example's Issues

Why is AWS_ACCOUNT_ID needed?

I saw, that AWS_ACCOUNT_ID (declared in .env) is never used.

For what could it been used?
Or: For what did you used it?

Btw. nice example! It helped me a lot to get in touch with golang and aws lambda.

Suggestion: Upload only the dist directory

In your example (which is very nice btw.) you upload the whole repository to the S3 bucket.
I know - it is only an example. But is it really important to upload the binarys AND the source code, only to use only the binary?
Could this been for debug reasons in a productive environment?

I found with a few changes you can upload only the dist directory.
You need to delete every ocurence dist/ in the template.yml and do this to the Makefile:

package: build
		cp template.yml dist/template.yml
		cd dist && \
		aws cloudformation package \
			--template-file template.yml \
			--s3-bucket $(AWS_BUCKET_NAME) \
			--region $(AWS_REGION) \
			--output-template-file package.yml

deploy:
		cd dist && \
		aws cloudformation deploy \
			--template-file package.yml \
			--region $(AWS_REGION) \
			--capabilities CAPABILITY_IAM \
			--stack-name $(AWS_STACK_NAME)

After this, only the binarys and the template.yml is uploaded.
It was not possible to exclude the template.yml. But I think it is needed to build the cloud formation stack, so the template.yml must been uploaded, because it describes the stack.

But I found also, that the S3 bucket could be emptied, and the lambda function still works.
Could it be, that the cloud formation is build, with the handler-binarys integrated in it?
Does it not have to fetch the builds from the S3 bucket, every time an instance is build because of a lambda call?

Greetings

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.