Skip to main content

Search the Dev Center

Search tutorials, templates, examples, and more.

AWS Serverless Application

The AWS Serverless Application template scaffolds a Pulumi project that deploys a serverless web application to AWS. It provisions an Amazon S3 bucket for static website hosting, an AWS Lambda function for the application backend, and an Amazon API Gateway REST API that routes requests to the static content and the Lambda function. The template ships with a placeholder web page that displays the current time so the project deploys end to end out of the box.

Architecture diagram of the AWS Serverless Application template AWS API GatewayREST API S3 Bucket Lambda Function

Before you begin#

For this walkthrough, you'll need:

Create a new project#

Create a new project from the template:

Terminal window
mkdir my-serverless-app; cd my-serverless-app
pulumi new serverless-aws-typescript

The pulumi new command walks you through creating and configuring the new project and stack. A stack is an instance of a project, and you can have many of them (dev, staging, prod), each with its own configuration settings. Depending on the language, you may also be asked for your preferred package manager or toolchain.

This template’s settings include:

KeyDescriptionDefault
aws:regionThe AWS region to deploy intous-west-2
You can change values like these later with pulumi config set :
Terminal window
pulumi config set aws:region <value>

Review the code#

When setup completes, you’ll see a few files:

  • Pulumi.yaml contains the project name and the language it’s written in.
  • Pulumi.dev.yaml contains the dev stack’s settings, including the values you just entered.
  • index.ts contains the program code that declares your cloud resources.

Take a moment to open them up and have a look. Here’s what’s in index.ts:

index.ts
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as apigateway from "@pulumi/aws-apigateway";
// A Lambda function to invoke
const fn = new aws.lambda.CallbackFunction("fn", {
callback: async (ev, ctx) => {
return {
statusCode: 200,
body: new Date().toISOString(),
};
}
})
// An AWS provider for the REST API to deploy its resources with
const provider = new aws.Provider("provider", {
region: aws.config.region,
});
// A REST API to route requests to HTML content and the Lambda function
const api = new apigateway.RestAPI("api", {
routes: [
{ path: "/", localPath: "www"},
{ path: "/date", method: "GET", eventHandler: fn },
]
}, { providers: [provider] });
// The URL at which the REST API will be served.
export const url = api.url;

Deploy the project#

Deploy with pulumi up. It starts with a preview of what it’s about to create and asks before it changes anything. Choose yes to deploy.

Terminal window
pulumi up

Pulumi adds a random suffix to most resource names, so the Lambda function fn becomes something like fn-4f8a2c1. This is autonaming. It lets you deploy the same program to more than one stack, and lets Pulumi create a replacement before it deletes the old resource, without names colliding.

When it finishes, Pulumi prints the stack’s outputs, values it only knows once the resources exist:

url

The HTTPS URL of the application, served by API Gateway.

Read one any time with pulumi stack output <name>, or pass it to another command. This opens the app in your browser:

Terminal window
open $(pulumi stack output url)

The page gets the time from the Lambda function at the /date path. Call it from the terminal with curl; it prints the current time:

Terminal window
curl -s "$(pulumi stack output url)date"
Need help?

If pulumi up reports No valid credential sources found or ExpiredToken, sign in again:

Terminal window
aws login

Still stuck? Let us know.

Customize the project#

The template has no configuration settings of its own; aws:region is the only one pulumi new sets. It deploys the website from the www folder.

The Lambda function’s code is the callback in index.ts.

Using your own web content#

If you already have a website you’d like to deploy, you can do so by replacing the contents of the www folder and redeploying with pulumi up.

Customizing the application’s functionality#

You can customize the placeholder website by editing the Lambda function to perform another action, such as displaying a countdown to a future time. You can also add new functionality by creating a new Lambda function, adding a new path to the REST API, and updating the HTML to call the new path.

To start small, change the format of the time the function returns:

In index.ts:

const fn = new aws.lambda.CallbackFunction("fn", {
callback: async (ev, ctx) => {
return {
statusCode: 200,
body: new Date().toISOString(),
body: new Date().toUTCString(),
};

Deploy the change and call /date again:

Terminal window
pulumi up
curl -s "$(pulumi stack output url)date"

The time now comes back like Wed, 21 Oct 2026 14:05:09 GMT.

To see what else you can add, browse the AWS provider in the Pulumi Registry.

Cleaning up#

You can cleanly destroy the stack and all of its infrastructure with pulumi destroy:

Terminal window
pulumi destroy

Learn more#

Related

The infrastructure platform for humans and agents.