├── .gitignore ├── Gopkg.lock ├── Gopkg.toml ├── LICENSE ├── README.md ├── function ├── Makefile └── function.go └── main.tf /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.dll 4 | *.so 5 | *.dylib 6 | 7 | # Test binary, build with `go test -c` 8 | *.test 9 | 10 | # Output of the go coverage tool, specifically when used with LiteIDE 11 | *.out 12 | 13 | # Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 14 | .glide/ 15 | 16 | .DS_Store 17 | .terraform 18 | 19 | # Ignore binary and zip file that we build 20 | function/main 21 | *.zip 22 | 23 | *.tfstate 24 | *.backup 25 | 26 | vendor/ -------------------------------------------------------------------------------- /Gopkg.lock: -------------------------------------------------------------------------------- 1 | # This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. 2 | 3 | 4 | [[projects]] 5 | name = "github.com/aws/aws-lambda-go" 6 | packages = [ 7 | "lambda", 8 | "lambda/messages", 9 | "lambdacontext" 10 | ] 11 | revision = "fafa7e49388b8991caf99308e80655ba91816b72" 12 | version = "v1.1.0" 13 | 14 | [solve-meta] 15 | analyzer-name = "dep" 16 | analyzer-version = 1 17 | inputs-digest = "99590d904e6f24342d045e124d7ecef7717674285cea60ceaa0e1884f614f083" 18 | solver-name = "gps-cdcl" 19 | solver-version = 1 20 | -------------------------------------------------------------------------------- /Gopkg.toml: -------------------------------------------------------------------------------- 1 | # Gopkg.toml example 2 | # 3 | # Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md 4 | # for detailed Gopkg.toml documentation. 5 | # 6 | # required = ["github.com/user/thing/cmd/thing"] 7 | # ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] 8 | # 9 | # [[constraint]] 10 | # name = "github.com/user/project" 11 | # version = "1.0.0" 12 | # 13 | # [[constraint]] 14 | # name = "github.com/user/project2" 15 | # branch = "dev" 16 | # source = "github.com/myfork/project2" 17 | # 18 | # [[override]] 19 | # name = "github.com/x/y" 20 | # version = "2.4.0" 21 | # 22 | # [prune] 23 | # non-go = false 24 | # go-tests = true 25 | # unused-packages = true 26 | 27 | 28 | [[constraint]] 29 | name = "github.com/aws/aws-lambda-go" 30 | version = "1.1.0" 31 | 32 | [prune] 33 | go-tests = true 34 | unused-packages = true 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Frank Cash 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # go-lambda-ping 2 | 3 | A simple program that will build the infrastructure to ping a website. 4 | 5 | ## Gathering Dependencies 6 | 7 | `go get github.com/aws/aws-lambda-go/lambda` 8 | 9 | ### Configuration 10 | 11 | The Go program exists inside of `function/`. `main.tf` is the Terraform configuration for the Lambda. 12 | 13 | ## Configuring the Site 14 | 15 | Change `url := "http://example.com/"` to `url := ""` in `function/function.go`. 16 | 17 | ## Building the Zip 18 | 19 | In the `function` directory run `make release` to generate the binary and then zip it. 20 | 21 | ## Deploying the Lambda 22 | 23 | 1. Run `terraform init` to initialize the terraform repository. 24 | 25 | 2. Then run `terraform plan` to create the execution plan. 26 | 27 | 3. Finally, `terraform apply` to apply the changes (run the execution plan). 28 | 29 | 30 | ## Trigger the Lambda 31 | ``` 32 | aws lambda invoke \ 33 | --invocation-type RequestResponse \ 34 | --function-name demo_lambda \ 35 | --region us-east-1 \ 36 | --log-type Tail \ 37 | --payload '{"key1":"value1", "key2":"value2", "key3":"value3"}' \ 38 | outputfile.txt 39 | ``` 40 | -------------------------------------------------------------------------------- /function/Makefile: -------------------------------------------------------------------------------- 1 | compile: 2 | GOOS=linux go build -o main 3 | 4 | compress: 5 | zip function.zip main 6 | 7 | release: compile \ 8 | compress -------------------------------------------------------------------------------- /function/function.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net/http" 6 | 7 | "github.com/aws/aws-lambda-go/lambda" 8 | ) 9 | 10 | func ping() (string, error) { 11 | url := "http://example.com/" 12 | resp, err := http.Get(url) 13 | return fmt.Sprintf("%s returned %s", url, resp.Status), err 14 | } 15 | 16 | func main() { 17 | // Make the handler available for Remote Procedure Call by AWS Lambda 18 | lambda.Start(ping) 19 | } 20 | -------------------------------------------------------------------------------- /main.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | region = "us-east-1" 3 | } 4 | 5 | resource "aws_iam_role" "lambda_exec_role" { 6 | name = "lambda_exec_role" 7 | 8 | assume_role_policy = <