

# 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?](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html) in the AWS SAM Developer Guide.

## Write the template
<a name="backend-lambda-deploy-template"></a>

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

```yaml
# 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](backend-lambda.md).

## Deploy the function for an environment
<a name="backend-lambda-deploy-env"></a>

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:

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

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

1. Run `npm install` in the function’s folder.

1. 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`.

   ```bash
   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`.

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

   ```bash
   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
<a name="backend-lambda-deploy-order"></a>

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](backend-data.md).

## Deploy from CI
<a name="backend-lambda-deploy-ci"></a>

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](build-with-amplifygen3-github-actions.md).

## Pull request previews
<a name="backend-lambda-deploy-previews"></a>

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
<a name="backend-lambda-deploy-rollback"></a>

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](deploy-your-app-promote.md).
+  **The function** – Deploy the function’s earlier code with `sam deploy`, from the commit that you want to return to.