View a markdown version of this page

Deploy functions with your app - AWS Amplify

Deploy functions with your app

Deploy one copy of each AWS Lambda function for each environment of your AWS Amplify app, such as preview and production. Each copy gets that environment’s backend URL, publishable key and branch ARN, and calls only that environment’s backend branch. With the AWS Serverless Application Model (AWS SAM), each copy is a stack of its own. For more information, see What is AWS SAM? in the AWS SAM Developer Guide.

Write the template

Give the template one parameter for each value that differs between environments:

# functions/template.yaml AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Parameters: BackendUrl: Type: String PublishableKey: Type: String BranchArn: Type: String AppOrigin: Type: String Resources: NotesReport: Type: AWS::Serverless::Function Properties: CodeUri: notes-report/ Handler: index.handler Runtime: nodejs22.x Architectures: [arm64] Timeout: 15 Environment: Variables: AMPLIFY_BACKEND_URL: !Ref BackendUrl AMPLIFY_BACKEND_PUBLISHABLE_KEY: !Ref PublishableKey Policies: - Statement: - Effect: Allow Action: amplifygen3:Connect Resource: !Ref BranchArn FunctionUrlConfig: AuthType: NONE Cors: AllowOrigins: [!Ref AppOrigin] AllowHeaders: [authorization, content-type] AllowMethods: [GET] Outputs: FunctionUrl: Value: !GetAtt NotesReportUrl.FunctionUrl FunctionName: Value: !Ref NotesReport

For the permission and the function URL, see Connect an AWS Lambda function.

Deploy the function for an environment

To deploy the function for an environment, do the following:

  1. Run the following command to get the environment’s backend URL, publishable key and branch ARN:

    aws amplifygen3 backend info --env production --output json

    Note the BackendUrl, PublishableKey and BranchArn values in the output.

  2. Run npm install in the function’s folder.

  3. Run the following command in the functions folder. Replace the values with the ones from step 1, and replace https://www.example.com with the environment’s website URL, from the Visit: line of aws amplifygen3 deploy.

    sam deploy --template-file template.yaml --stack-name my-app-functions-production \ --resolve-s3 --capabilities CAPABILITY_IAM --no-confirm-changeset --no-fail-on-empty-changeset \ --parameter-overrides BackendUrl=<BackendUrl> PublishableKey=<PublishableKey> \ BranchArn=<BranchArn> AppOrigin=https://www.example.com

    sam deploy prints the stack’s outputs, FunctionUrl and FunctionName.

  4. Run the following command to give the environment the function’s URL:

    aws amplifygen3 var set NEXT_PUBLIC_NOTES_REPORT_URL <FunctionUrl> --env production

    Your app reads the new value after the environment’s next deployment.

Repeat these steps for each environment, with its own --env value and stack name.

Deploy in order

Deploy your app first, with aws amplifygen3 deploy, and then the function. Each deployment of your app applies your new migrations, so the function’s new code finds the tables and columns that it reads.

Keep each migration compatible with the code that is still running, in your app and in your functions. For more information, see Tables and migrations.

Deploy from CI

To deploy from a CI workflow, run aws amplifygen3 deploy and then sam deploy in the same job, in that order. For the workflow that deploys your app, see Deploy with GitHub Actions.

Pull request previews

A pull request preview deploys to the preview environment and reads its variables, so it calls the preview function. The function reads and writes the backend branch whose ARN you deployed it with.

Roll back

Roll back your app and the function separately:

  • Your app – Promote an earlier deployment. Amplify serves the earlier website and server code without a new build. The database keeps its current schema: to undo a migration, deploy a new migration. For more information, see Promote a deployment.

  • The function – Deploy the function’s earlier code with sam deploy, from the commit that you want to return to.