Serverless REST API
Basic example of using AWS 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-ts-apigatewaycd pulumi-examples/aws-ts-apigatewayA simple REST API that counts the number of times a route has been hit. For a detailed walkthrough of this example, see the article Create a Serverless REST API.
Deploying and running the program#
Note: some values in this example will be different from run to run. These values are indicated
with ***.
-
Create a new stack:
Terminal window pulumi stack init count-api-testing -
Set the AWS region:
$ pulumi config set aws:region us-east-2 -
Restore NPM modules via
npm installoryarn install. -
Run
pulumi upto preview and deploy changes:$ pulumi upPreviewing update of stack 'count-api-testing'...Updating (count-api-testing):Type Name Status+ pulumi:pulumi:Stack aws-ts-apigateway-count-api-testing created+ ├─ aws:apigateway:x:API hello-world created+ │ ├─ aws:iam:Role hello-world4fcc7b60 created+ │ ├─ aws:iam:RolePolicyAttachment hello-world4fcc7b60-32be53a2 created+ │ ├─ aws:lambda:Function hello-world4fcc7b60 created+ │ ├─ aws:apigateway:RestApi hello-world created+ │ ├─ aws:apigateway:Deployment hello-world created+ │ ├─ aws:lambda:Permission hello-world-a552609d created+ │ └─ aws:apigateway:Stage hello-world created+ └─ aws:dynamodb:Table counterTable createdOutputs:endpoint: "https://***execute-api.us-east-2.amazonaws.com/stage/"Resources:+ 10 createdDuration: 24s -
View the endpoint URL and curl a few routes:
Terminal window pulumi stack outputCurrent stack outputs (1):OUTPUT VALUEendpoint https://***.us-east-2.amazonaws.com/stage/curl $(pulumi stack output endpoint)/hello{"route":"hello","count":1}curl $(pulumi stack output endpoint)/hello{"route":"hello","count":2}curl $(pulumi stack output endpoint)/woohoo{"route":"woohoo","count":1} -
To view the runtime logs of the Lambda function, use the
pulumi logscommand. To get a log stream, usepulumi logs --follow.
Clean up#
-
Run
pulumi destroyto tear down all resources. -
To delete the stack itself, run
pulumi stack rm. Note that this command deletes all deployment history from the Pulumi console.