forked from pulumi/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created example for AWS Lambda in Go (pulumi#553)
- Loading branch information
Tasia Halim
committed
Feb 13, 2020
1 parent
59463b7
commit 1aa6acf
Showing
6 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Gopkg.toml example | ||
# | ||
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html | ||
# for detailed Gopkg.toml documentation. | ||
# | ||
# required = ["github.com/user/thing/cmd/thing"] | ||
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] | ||
# | ||
# [[constraint]] | ||
# name = "github.com/user/project" | ||
# version = "1.0.0" | ||
# | ||
# [[constraint]] | ||
# name = "github.com/user/project2" | ||
# branch = "dev" | ||
# source = "github.com/myfork/project2" | ||
# | ||
# [[override]] | ||
# name = "github.com/x/y" | ||
# version = "2.4.0" | ||
# | ||
# [prune] | ||
# non-go = false | ||
# go-tests = true | ||
# unused-packages = true | ||
|
||
|
||
[[constraint]] | ||
name = "github.com/pulumi/pulumi" | ||
version = "1.9.1" | ||
|
||
[[constraint]] | ||
name = "github.com/pulumi/pulumi-aws" | ||
version = "1.20.0" | ||
|
||
[prune] | ||
go-tests = true | ||
unused-packages = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
build:: | ||
GOOS=linux GOARCH=amd64 go build -o ./handler/handler ./handler/handler.go | ||
zip -j ./handler/handler.zip ./handler/handler | ||
go build -o go-lambda main.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
name: go-lambda | ||
runtime: go | ||
description: Basic example of an AWS lambda | ||
template: | ||
config: | ||
aws:region: | ||
description: The AWS region to deploy into | ||
default: us-east-1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# AWS Golang Lambda | ||
This example creates a lambda that does a simple `ToUpper` on the string input and returns it. | ||
|
||
## Deploying the App | ||
|
||
To deploy your infrastructure, follow the below steps. | ||
|
||
### Prerequisites | ||
|
||
1. [Install Pulumi](https://www.pulumi.com/docs/get-started/install/) | ||
2. [Configure AWS Credentials](https://www.pulumi.com/docs/intro/cloud-providers/aws/setup/) | ||
3. [Clone aws-go-lambda](https://github.com/aws/aws-lambda-go) | ||
|
||
### Steps | ||
|
||
After cloning this repo, run these commands from the working directory: | ||
|
||
1. Build the go files: | ||
|
||
```bash | ||
make build | ||
``` | ||
|
||
2. Create a new Pulumi stack, which is an isolated deployment target for this example: | ||
|
||
```bash | ||
pulumi stack init | ||
``` | ||
|
||
3. Set the required configuration variables for this program: | ||
```bash | ||
pulumi config set aws:region us-east-1 | ||
``` | ||
|
||
4. Execute the Pulumi program to create our lambda: | ||
|
||
```bash | ||
pulumi up | ||
``` | ||
|
||
5. Call our lambda function from the aws cli | ||
|
||
```bash | ||
aws lambda invoke \ | ||
--function-name $(pulumi stack output lambda) \ | ||
--region $(pulumi config get aws:region) \ | ||
--payload '"foo"' \ | ||
output.json | ||
|
||
cat output.json # view the output file with your tool of choice | ||
# "FOO" | ||
``` | ||
|
||
6. From there, feel free to experiment. Simply making edits and running `make build` then `pulumi up` will update your lambda. | ||
|
||
7. Afterwards, destroy your stack and remove it: | ||
|
||
```bash | ||
pulumi destroy --yes | ||
pulumi stack rm --yes | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package main | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/aws/aws-lambda-go/lambda" | ||
) | ||
|
||
// handler is a simple function that takes a string and does a ToUpper. | ||
func handler(str string) (string, error) { | ||
return strings.ToUpper(str), nil | ||
} | ||
|
||
func main() { | ||
lambda.Start(handler) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/pulumi/pulumi-aws/sdk/go/aws/iam" | ||
"github.com/pulumi/pulumi-aws/sdk/go/aws/lambda" | ||
"github.com/pulumi/pulumi/sdk/go/pulumi" | ||
) | ||
|
||
func main() { | ||
pulumi.Run(func(ctx *pulumi.Context) error { | ||
// Create an IAM role. | ||
role, err := iam.NewRole(ctx, "task-exec-role", &iam.RoleArgs{ | ||
AssumeRolePolicy: pulumi.String(`{ | ||
"Version": "2012-10-17", | ||
"Statement": [{ | ||
"Sid": "", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": "lambda.amazonaws.com" | ||
}, | ||
"Action": "sts:AssumeRole" | ||
}] | ||
}`), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Set arguments for constructing the function resource. | ||
args := &lambda.FunctionArgs{ | ||
Handler: pulumi.String("handler"), | ||
Role: role.Arn, | ||
Runtime: pulumi.String("go1.x"), | ||
Code: pulumi.NewFileArchive("./handler/handler.zip"), | ||
} | ||
|
||
// Create the lambda using the args. | ||
function, err := lambda.NewFunction(ctx, "basicLambda", args) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Export the lambda ARN. | ||
ctx.Export("lambda", function.Arn) | ||
|
||
return nil | ||
}) | ||
} |