AWS Golang Lambda With API Gateway
Basic example of an AWS lambda With API Gateway
This example lives in the pulumi/examples repository. Check out just this directory to use it:
git clone --filter=blob:none --sparse https://github.com/pulumi/examples pulumi-examplesgit -C pulumi-examples sparse-checkout set aws-go-lambda-gatewaycd pulumi-examples/aws-go-lambda-gatewayThis example creates a lambda that does a simple ToUpper on the path input of an API request and returns it.
Deploying the App#
To deploy your infrastructure, follow the below steps.
Prerequisites#
Steps#
After cloning this repo, run these commands from the working directory:
-
Build the handler:
-
For developers on Linux and macOS:
Terminal window make build -
For developers on Windows:
-
Get the
build-lambda-ziptool:Terminal window set GO111MODULE=ongo.exe get -u github.com/aws/aws-lambda-go/cmd/build-lambda-zip -
Use the tool from your GOPATH:
Terminal window set GOOS=linuxset GOARCH=amd64set CGO_ENABLED=0go build -o handler\handler handler\handler.go%USERPROFILE%\Go\bin\build-lambda-zip.exe -o handler\handler.zip handler\handler
-
-
-
Create a new Pulumi stack, which is an isolated deployment target for this example:
Terminal window pulumi stack init -
Set the required configuration variables for this program:
Terminal window pulumi config set aws:region us-west-2 -
Execute the Pulumi program to create our lambda:
Terminal window pulumi upPreviewing update (dev):Type Name Plan+ pulumi:pulumi:Stack go-lambda-dev create+ ├─ aws:apigateway:RestApi UpperCaseGateway create+ ├─ aws:iam:Role task-exec-role create+ ├─ aws:apigateway:Resource UpperAPI create+ ├─ aws:iam:RolePolicy lambda-log-policy create+ ├─ aws:apigateway:Method AnyMethod create+ ├─ aws:lambda:Function basicLambda create+ ├─ aws:lambda:Permission APIPermission create+ ├─ aws:apigateway:Integration LambdaIntegration create+ └─ aws:apigateway:Deployment APIDeployment createResources:+ 10 to createDo you want to perform this update? yesUpdating (dev):Type Name Status+ pulumi:pulumi:Stack go-lambda-dev created+ ├─ aws:apigateway:RestApi UpperCaseGateway created+ ├─ aws:iam:Role task-exec-role created+ ├─ aws:apigateway:Resource UpperAPI created+ ├─ aws:iam:RolePolicy lambda-log-policy created+ ├─ aws:apigateway:Method AnyMethod created+ ├─ aws:lambda:Function basicLambda created+ ├─ aws:apigateway:Integration LambdaIntegration created+ ├─ aws:lambda:Permission APIPermission created+ └─ aws:apigateway:Deployment APIDeployment createdOutputs:invocation URL: "https://<gateway-id>.execute-api.us-west-2.amazonaws.com/prod/{message}"Resources:+ 10 createdDuration: 29s -
Call our lambda function from the cli:
Terminal window curl https://<gateway-id>.execute-api.us-west-2.amazonaws.com/prod/helloworldHELLOWORLD% -
From there, feel free to experiment. Simply making edits, rebuilding your handler, and running
pulumi upwill update your lambda. -
Afterwards, destroy your stack and remove it:
Terminal window pulumi destroy --yespulumi stack rm --yes