AWS SAM
Deploy AWS SAM templates with Harness CD by creating a SAM service, environment, and pipeline stage that build and deploy your serverless application
Harness supports AWS SAM (Serverless Application Model) deployments. SAM is an open-source framework provided by Amazon Web Services (AWS) that simplifies the deployment and management of serverless applications on AWS. It is an extension of AWS's infrastructure-as-code service, CloudFormation.
For SAM deployments in Harness, add your SAM directory to Harness and select the target region. Harness performs the SAM build and deploy.
What you will learn from this topic
How to create a Harness SAM service, environment, and pipeline stage for serverless deployments
AWS IAM permissions required for SAM deployments and how to attach them to the Harness connector
How to use Go templating with SAM templates and values.yaml files
Important limitations of SAM deployments in Harness, including rollback constraints
Before you begin
Before you begin, make sure you have the following:
Go to AWS permissions to create an AWS IAM role with the permissions your SAM template needs.
Have your AWS SAM directory and template ready to add to the Harness SAM service, with an optional values.yaml file if you use Go templating.
Have a Harness Kubernetes Cluster connector ready. SAM deployments run only in containerized step groups, so you need a Kubernetes cluster to host the step group pod. For more information, go to SAM step group.
SAM deployment overview
The following sections summarize the deployment flow, supported integrations, and limits of SAM deployments in Harness.
Deployment summary
SAM deployments involve the following steps:
Create Harness SAM service.
Add your SAM directory.
Add a values.yaml file (optional). Harness supports Go templating with SAM templates and values.yaml files.
Create Harness SAM environment.
Define the target infrastructure by adding a Harness AWS connector with the necessary AWS permissions for the deployment and a target AWS region.
Create Harness SAM pipeline stage.
Select the Harness SAM service to use.
Select the Harness SAM environment to use.
Define deployment strategy. Harness automatically generates the steps required to deploy your SAM template.
Deploy pipeline.
Supported integrations
For details on AWS support in Harness, including tooling, limitations, and repositories, go to Supported CD features and integrations.
Limits and behaviors
The following notes apply to SAM deployments:
Harness supports Go templating with SAM templates and values.yaml files.
You cannot add artifacts to your Harness SAM service.
You can manage whether Harness performs the SAM build within an ephemeral Docker container in a Kubernetes cluster using the
--use-containeroption in the Harness SAM Build step. You can manage the Kubernetes settings for these steps as needed. For more information, go to AWS documentation.Harness does not support a controlled AWS SAM Rollback after a deployment pipeline failure occurs. AWS SAM deploys the Lambda function, and if it fails during stack creation, CloudFormation rolls it back. After a successful AWS SAM deployment, Harness cannot initiate a rollback, due to the AWS SAM CLI's limitation to trigger rollback on demand.
AWS OpenID Connect (OIDC) connectors are supported for AWS SAM deployments starting with delegate version
855xxor later.
AWS SAM deployment demo
AWS permissions
When you set up the Harness environment for your deployment, you must add a Harness AWS connector to the Harness environment Infrastructure Definition. Typically, the AWS connector uses the access and secret keys for an AWS IAM user (there are other options). The permissions added to the IAM user account must permit whatever AWS service changes you are performing in your SAM template.
Containerized step images
Old images
Harness supports different images for Build and Deploy for AWS SAM deployment.
SAM Build: harnessdev/sam-build:1.82.0-latest
SAM Deploy: harnessdev/sam-deploy:1.82.0-latest
Harness recommends using the new images, which support multiple runtimes.
New SAM images
Harness supports multiple runtime images for nodejs24, nodejs22, nodejs20, nodejs18, python3.14, python3.13, python3.12, python3.11, ruby3.4, ruby3.3, ruby3.2, java25, java21, java17, java8 and go1.22. These are Linux AMD64 images.
Unlike old images, in new images a single image has the capability of handling all the SAM steps.
The following describes the runtime image:

To access these images from Amazon ECR, go to the ECR Image Repository for SAM Plugin.
To access these images from Google Artifact Registry (GAR), go to the GAR Image Repository for SAM Plugin (US region) and GAR Image Repository for SAM Plugin (Europe region)
Harness releases new AWS SAM Plugin images once every 3 months. If you want to use the latest AWS SAM Plugin images, you can build your own image using the SAM Plugin Image Builder.
Build your own image
You can also build your own image based on the base image provided by Harness and use it in the SAM deployments.
How this pipeline works:
Harness provides a base image that has the AWS SAM plugin binary. The pipeline takes a pre-built scratch image containing the AWS SAM plugin binary and creates a properly tagged final image that combines it with the appropriate SAM runtime.
For more details, go to SAM Plugin Image Builder.
Use AWS IRSA for Harness AWS connector credentials
The Use IRSA option allows you to use a service account with a specific IAM role when making authenticated requests to resources. To use it, create a service account in the namespace where the step group will be run that has the relevant IAM role with IRSA. In this case it is the step group pod that makes the relevant API calls and hence needs the IRSA service account setup.
With this option, you can use AWS IAM roles for service accounts (IRSA) to associate a specific IAM role with the service account used by the step group pod.
Verify your firewall policy and make sure to allowlist all AWS endpoints for the services you are using. For more details, go to view AWS service endpoints.
Modify your step group YAML, as described below.
AWS SAM service
Harness supports standard SAM templates. You can add your SAM directory in the Harness SAM service.
To add your template, do the following:
In your Harness project, in Deployments, select Services.
Select New Service.
Name the service and select Save.
In Deployment Type, select AWS SAM.
In Manifests, select Add Manifest.
In Specify Manifest Type, select AWS SAM Directory, and then select Continue.
In Specify AWS SAM Directory Store, select a provider and follow the corresponding instructions in the following sections. You can also use the Harness File Store.
Git providers
To add the template from a Git provider, do the following:
Select your Git provider of choice.
Select or create a new Harness Git connector to your Git provider, and then select Continue.
In Manifest Details, enter the following:
Manifest Identifier: Enter a name for the template.
Git Fetch Type: Select how you want to fetch the template.
Branch/Commit Id: Enter the branch name or commit Id.
File/Folder Path: Enter the path to the template from the root of the repository.
Select Submit.
AWS S3
To add the template from AWS S3, do the following:
Select the AWS S3 provider.
Select or create a new Harness AWS connector to your AWS S3 provider, and then select Continue.
In Manifest Details, enter the following:
Manifest Identifier: Enter a name that identifies this SAM manifest.
Region: Select the AWS region to use for the connection.
Bucket Name: Enter the AWS S3 bucket name.
File/Folder Path: Enter the path from the root of the bucket to the SAM template file.
Select Submit.
Values YAML
You can also add a values.yaml file to use Go templating in your SAM template. Go templating allows you to select different values.yaml files for different environments.
Your values.yaml file can use standard Go formatting, like this:
Your template references the values.yaml file using the format {{.Values.KEY}}. The following is an example using {{.Values.runtime}}:
Variables
You can create service variables, and then use them in the values.yaml file you added.
Use a service variable reference as the value:
You can only use service variable references in the values.yaml file.
For information on how to override service variables at the environment level, go to Service overrides.
AWS SAM environment
To create an environment, do the following:
In your Harness project, in Deployments, select Environments.
Select New Environment, name the environment, select its Environment Type, and then select Save.
Next you will add the SAM infrastructure definition.
Define the infrastructure
The SAM infrastructure definition is the target AWS account and region for the SAM deployment.
In the environment, select Infrastructure Definitions.
Select Infrastructure Definitions.
Name the infrastructure definition and, in Deployment Type, select AWS SAM.
In Connector, select or create a Harness AWS connector. Ensure the IAM account uses the required permissions for the operations your SAM template requires.
In Region, select the target region.
Select Save.
When a pipeline stage uses this infrastructure definition, it will deploy your SAM template changes in the target region.
If your deployment requires infrastructure provisioning, go to Provision Infrastructure to explore the provisioning options available in Harness.
AWS SAM stage
After you create your SAM service and environment, create your SAM pipeline.
When you add a Deploy stage to a pipeline, you can select the AWS SAM deployment type.
In your Harness project, in Deployments, create or open a pipeline.
Select Add Stage, and then select Deploy.
Name your stage.
In Deployment Type, select AWS SAM, and then select Set Up Stage.
In Service, select the SAM service to use, and then select Continue.
In Environment, select the SAM environment and infrastructure definition to use, and then select Continue.
Select the deployment strategy to use, and then select Use Strategy.
AWS SAM basic strategy and steps
The basic strategy consists of the following execution setup:
SAM step group: Uses your Kubernetes cluster as a step runtime infrastructure.
DinD Background step: Creates a Docker in Docker container for each step's execution resources.
Download Manifests step: Fetches your SAM template.
SAM Build step: Runs a SAM build.
SAM Deploy step: Runs a SAM deploy.

These steps are described in detail below.
SAM step group
Harness automatically generates a containerized step group containing the steps needed for the SAM build and deploy.
When the step group setting Enable container based execution is enabled, the step group is containerized. SAM runs on containerized step groups only. You do not need to install the SAM CLI on the Harness Delegate, because Harness-provided SAM step images have the SAM CLI baked in. SAM steps run in the containers that are brought up inside the pod within the Kubernetes cluster configured in the step group configuration.
When the Enable container based execution setting is disabled, Harness Delegate cannot perform the SAM deployment.
You need to configure the following mandatory settings:
Kubernetes Cluster: Add a Harness Kubernetes Cluster connector to connect to the cluster that will be used as the runtime step infrastructure.
Namespace: Enter the name of the cluster namespace to use.
Harness Docker Hub connector and image for all steps
The DinD Background, SAM Build, and SAM Deploy steps are containerized. In each step, you must provide a Harness connector to a container registry and an image for the container the step uses.
When you create the Execution section of your stage the first time and select the Basic strategy, Harness supplies the container registry connector and image for each step automatically.
You can add/select a different container registry connector and image for each step, but the image should support the SAM template operations you are deploying.
DinD Background step
The following describes the SAM deployment's use of the DinD Background step:
Open the dind Background step. The automatically generated step is named
dind.In Container Registry, the Harness container registry connector is already set up. You can add/select your own container registry connector if you like.
In Image, you can see the
docker:dindDocker in Docker image is set up.The remaining settings are optional.
Download manifests step
The Download Manifests step fetches the SAM template in the Harness service you selected for this stage. This step does not require configuration.
The following is an example of the step tasks:
Git initialization: The command
git initis executed to initialize a new Git repository.Setting remote origin: The command
git remote add origin <repository-url>is executed to set the remote origin for the Git repository.Fetching remote branches: The command
git fetch --depth=50 origin +refs/heads/main:is executed to fetch the remote branches from the origin.Checking out main branch: The command
git checkout -b main origin/mainis executed to create and switch to a new branch namedmainbased on the remote branchorigin/main.
SAM Build step
The SAM Build step performs a standard SAM build as well as setting up the container environment, resolving expressions in the SAM template, and building the SAM application using the specified dependencies and Docker container image.
To configure the SAM Build step, do the following:
Open the SAM Build step.
In Container Registry, you can see that the Harness Docker Registry connector is already set up.
In Image, you can use the latest Harness images that support multiple runtimes. For example: harness/aws-sam-plugin:nodejs20.x-1.120.0-1.0.0-beta-linux-amd64. You can use a different image as well as per your need.
AWS SAM build command options (required)
The --use-container option is set up by Harness automatically. This command ensures that AWS SAM packages and builds the application code inside a Docker container that is configured with the necessary dependencies and runtime environment specified in the AWS SAM template.
Pre-execution command
The pre-execution command runs before your actual SAM Build and Deploy step. You can use this field to install any dependencies needed before the plugin logic executes.

SAM build Docker container registry (required for beta)
In SAM Build Docker Container Registry, you can use the same Harness Docker Registry connector automatically set up in the Container Registry setting or add/select your own connector.
Step execution summary
The following is a summary of the step's tasks visible in the step log once it has run:
Setting up environment:
Changes the current directory, such as
m1/SAM/sam-nodejs-multi-step.The command
docker psis executed to check if Docker is running, but no containers are listed.
Exporting variables:
Several environment variables are set using the
exportcommand, includingPLUGIN_SAM_TEMPLATE_FILE_PATH,PLUGIN_VALUES_YAML_FILE_PATH, andPLUGIN_VALUES_YAML_CONTENT. These variables are exported to be used in subsequent commands.
Resolving expressions in SAM template:
The command
/opt/harness/client-tools/go-template/v0.4.15/go-template -t template.yaml -f values.yaml -o .is executed to resolve expressions in the SAM template file based on the values provided in values.yaml.The resolved SAM template content is displayed in the log before and after expression resolution.
Building SAM application code:
The command
java -jar /opt/harness/sam-build.jaris executed to build the SAM application.The SAM CLI collects telemetry data, and it provides a link to the telemetry documentation.
The Docker container
image public.ecr.aws/sam/build-nodejs18.x:latest-x86_64is fetched to build the SAM application.A builder is used to pack, install dependencies, and clean up the environment for each template function.
Build result:
The build is marked successful, and the built artifacts are located in the
.aws-sam/builddirectory.The built SAM template is located at
.aws-sam/build/template.yaml.
SAM Deploy step
The SAM Deploy step performs a standard SAM deploy.
To configure the SAM Deploy step, do the following:
Open the SAM Deploy step.
In Container Registry, you can see that the Harness Docker Registry connector is already set up.
In Image, you can use the latest Harness images that support multiple runtimes. For example: harness/aws-sam-plugin:nodejs20.x-1.120.0-1.0.0-beta-linux-amd64. You can use a different image as well as per your need.
In Stack Name, enter the name of the CloudFormation stack that will be created or updated as part of the deployment process.
The following is a summary of the step's tasks visible in the step log once it has run:
The
aws configureauthentication steps are executed.The directory is changed to the working directory, such as
m1/SAM/sam-nodejs-multi-step.The command
docker psis executed.The command
java -jar /opt/harness/sam-deploy.jaris executed.Information about SAM CLI telemetry collection is displayed.
Information about the managed S3 bucket is displayed.
Details of the deployment are displayed, including stack name, region, and other parameters.
The deployment is initiated.
The SAM deployment is successful.
Deploy the pipeline
After the pipeline is complete, deploy it.
Select Run, and then select Run Pipeline to initiate the deployment.
Observe the execution logs as Harness deploys the template and checks for a successful deployment.
In the SAM Deploy step logs, you can see the CloudFormation events from stack operations and CloudFormation outputs from the deployment stack.
If the deployment is successful, you will see:
FAQs
For frequently asked questions about AWS deployments in Harness, go to AWS deployment FAQs.
Next steps
Go to AWS deployment FAQs for answers to common AWS deployment questions.
Go to Provisioning overview to configure dynamically provisioned infrastructure.
Go to CD best practices for recommended practices.
Last updated
Was this helpful?