Skip to content

Commit

Permalink
Created example for AWS Lambda in Go (pulumi#553)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tasia Halim committed Feb 13, 2020
1 parent 59463b7 commit 1aa6acf
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 0 deletions.
38 changes: 38 additions & 0 deletions aws-go-lambda/Gopkg.toml
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
4 changes: 4 additions & 0 deletions aws-go-lambda/Makefile
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
8 changes: 8 additions & 0 deletions aws-go-lambda/Pulumi.yaml
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
61 changes: 61 additions & 0 deletions aws-go-lambda/README.md
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
```
16 changes: 16 additions & 0 deletions aws-go-lambda/handler/handler.go
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)
}
48 changes: 48 additions & 0 deletions aws-go-lambda/main.go
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
})
}

0 comments on commit 1aa6acf

Please sign in to comment.