Deploy Next.js to AWS Lambda with GitLab CI
Automate your Next.js deployments to AWS Lambda using GitLab CI/CD.
Prerequisites
Before you start make sure to configure your infrastructure providers.
You can do this by following the get started guide.
Setup GitLab CI/CD
This example works with the “main” branch.
.gitlab-ci.yml
image: node:22
stages:
- deploy
variables:
CLOUDFLARE_API_TOKEN: $CLOUDFLARE_API_TOKEN
CLOUDFLARE_DEFAULT_ACCOUNT_ID: $CLOUDFLARE_DEFAULT_ACCOUNT_ID
CLOUDFLARE_EMAIL: $CLOUDFLARE_EMAIL
AWS_REGION: us-east-1
AWS_SDK_LOAD_CONFIG: "1"
STAGE: "production"
cache:
key:
files:
- pnpm-lock.yaml
paths:
- .pnpm-store
before_script:
- curl -fsSL https://get.pnpm.io/install.sh | sh -
- export PNPM_HOME="$HOME/.local/share/pnpm"
- export PATH="$PNPM_HOME:$PATH"
- pnpm config set store-dir .pnpm-store
- pnpm install --frozen-lockfile
deploy:production:
stage: deploy
environment:
name: production
only:
- main
script:
- aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
- aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
- aws configure set region $AWS_REGION
- pnpm sst deploy --stage=$STAGE
Remember to add the secrets in your GitLab repository CI/CD variables configuration.
Add sst.config.ts configuration. To initialize SST you can run:
pnpm dlx sst init
Note:
- This example uses “open-deployments” name. Name it by your own app name.
- This example uses opendeployments.com domain. Use your own domain name.
sst.config.ts
export default $config({
app(input) {
return {
name: "open-deployments",
removal: input?.stage === "production" ? "retain" : "remove",
protect: ["production"].includes(input?.stage),
home: "aws",
providers: {
aws: {
region: "us-east-1",
profile: input.stage === "production" ? "production" : undefined,
},
cloudflare: "latest",
},
};
},
async run() {
const isProduction = $app.stage === "production";
new sst.aws.Nextjs("OpenDeploymentsWebsite", {
domain: isProduction
? {
name: "opendeployments.com",
dns: sst.cloudflare.dns(),
aliases: ["www.opendeployments.com"],
}
: undefined,
});
},
});
Last updated on