Skip to main content

AWS Golang Lambda

Basic example of an AWS lambda

This example lives in the pulumi/examples repository. Check out just this directory to use it:

Get started with this example
git clone --filter=blob:none --sparse https://github.com/pulumi/examples pulumi-examples
git -C pulumi-examples sparse-checkout set aws-go-lambda
cd pulumi-examples/aws-go-lambda

This example creates an AWS Lambda function that does a simple ToUpper on the string input and returns it.

Prerequisites#

  1. Install Pulumi
  2. Configure AWS credentials
  3. Install Go

Deploying the example#

  1. Build the handler:

    • For developers on Linux and macOS:

      Terminal window
      make build
    • For developers on Windows:

      • Get the build-lambda-zip tool:

        Terminal window
        set GO111MODULE=on
        go.exe get -u github.com/aws/aws-lambda-go/cmd/build-lambda-zip
      • Use the tool from your GOPATH:

        Terminal window
        set GOOS=linux
        set GOARCH=amd64
        set CGO_ENABLED=0
        go build -o handler\bootstrap handler\handler.go
        %USERPROFILE%\Go\bin\build-lambda-zip.exe -o handler\handler.zip handler\bootstrap
  2. Create a new stack, which is an isolated deployment target for this example:

    Terminal window
    pulumi stack init
  3. Install dependencies:

    Terminal window
    go mod download
  4. Set the AWS region to deploy into:

    Terminal window
    pulumi config set aws:region us-east-1
  5. Deploy the stack:

    Terminal window
    pulumi up
  6. Call the Lambda function from the AWS CLI with “foo” as the payload:

    Terminal window
    aws lambda invoke \
    --function-name $(pulumi stack output lambda) \
    --region $(pulumi config get aws:region) \
    --cli-binary-format raw-in-base64-out \
    --payload '"foo"' \
    output.json
    cat output.json # view the output file with your tool of choice
    # "FOO"

    From there, feel free to experiment. Simply making edits, rebuilding your handler, and running pulumi up will update your function.

Cleaning up#

Once you’re finished experimenting, tear down your stack’s resources by destroying and removing it:

Terminal window
pulumi destroy
pulumi stack rm

Related

The infrastructure as code platform for any cloud.