Skip to content
Get Started for Free

CloudFormation

CloudFormation is a service provided by Amazon Web Services (AWS) that allows you to define and provision infrastructure as code. It enables you to create, update, and manage resources in a repeatable and automated manner using declarative templates. With CloudFormation, you can use JSON or YAML templates to define your desired infrastructure state. You can specify resources, their configurations, dependencies, and relationships in these templates.

LocalStack supports CloudFormation, allowing you to use the CloudFormation APIs in your local environment to declaratively define your architecture on the AWS, including resources such as S3 Buckets, Lambda Functions, and much more. The API Coverage section and feature coverage provides information on the extent of CloudFormation’s integration with LocalStack.

This guide is designed for users new to CloudFormation and assumes basic knowledge of the AWS CLI and our awslocal wrapper script.

Start your LocalStack container using your preferred method. We will demonstrate how to deploy a simple CloudFormation stack consisting of a single S3 Bucket with the AWS CLI.

CloudFormation stack is a collection of AWS resources that you can create, update, or delete as a single unit. Stacks are defined using JSON or YAML templates. Use the following code snippet and save the content in either cfn-quickstart-stack.yaml or cfn-quickstart-stack.json, depending on your preferred format.

Resources:
LocalBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: cfn-quickstart-bucket

You can deploy the CloudFormation stack using the AWS CLI with the deploy command. The deploy command creates and updates CloudFormation stacks. Run the following command to deploy the stack:

Terminal window
awslocal cloudformation deploy \
--stack-name cfn-quickstart-stack \
--template-file "./cfn-quickstart-stack.yaml"

You can verify that the stack was created successfully by listing the S3 buckets in your LocalStack container using the ListBucket API. Run the following command to list the buckets:

Terminal window
awslocal s3api list-buckets

You can delete the CloudFormation stack using the delete-stack command. Run the following command to delete the stack along with all the resources created by the stack:

Terminal window
awslocal cloudformation delete-stack \
--stack-name cfn-quickstart-stack

The LocalStack Web Application provides a Resource Browser for managing CloudFormation stacks to manage your AWS resources locally. You can access the Resource Browser by opening the LocalStack Web Application in your browser, navigating to the Resources section, and then clicking on CloudFormation under the Management/Governance section.

CloudFormation Resource Browser

The Resource Browser allows you to perform the following actions:

  • Create Stack: Create a new CloudFormation stack by clicking on Create Stack and provide a template file or URL, including the stack name and parameters.
  • Edit Stack: Edit an existing CloudFormation stack by clicking on Edit Stack and editing the stack name and parameters and clicking on Submit.
  • View Stack: View an existing CloudFormation stack by clicking on the Stack Name and viewing the stack details, including the stack name, status, and resources.
  • Delete Stack: Delete an existing CloudFormation stack by clicking on the Stack Name and clicking on Actions and then Remove Selected.

The following code snippets and sample applications provide practical examples of how to use CloudFormation in LocalStack for various use cases:

CloudFormation templates that target both real AWS and LocalStack should avoid hardcoded values that differ between the two environments. Using pseudo parameters and intrinsic functions keeps a single template portable without conditional logic or environment-specific parameter overrides.

Use AWS::URLSuffix for service domain names

Section titled “Use AWS::URLSuffix for service domain names”

Hardcoding amazonaws.com (or conversely localhost.localstack.cloud) when building service URLs is one of the most common causes of templates that deploy on AWS but fail on LocalStack, or vice versa. This typically shows up in API Gateway invoke URLs, Step Functions API integration targets, and other places where a template constructs a fully qualified endpoint.

The AWS::URLSuffix pseudo parameter resolves to amazonaws.com on AWS (or amazonaws.com.cn in China Regions) and to the configured LOCALSTACK_HOST on LocalStack, which defaults to localhost.localstack.cloud. Referencing it lets the same template produce a valid URL in either environment.

Avoid hardcoding the AWS suffix:

Outputs:
ApiUrl:
Value: !Sub "https://${MyApi}.execute-api.${AWS::Region}.amazonaws.com/${StageName}"

Reference AWS::URLSuffix instead:

Outputs:
ApiUrl:
Value: !Sub "https://${MyApi}.execute-api.${AWS::Region}.${AWS::URLSuffix}/${StageName}"

The same pattern applies when wiring an API Gateway stage into a Step Functions task, when building a WebSocket invoke URL, or any other integration Uri that embeds a service domain. The LocalStack team contributed this practice upstream to the AWS SAM application templates and to the AWS serverless patterns collection that backs serverlessland.com/patterns and the VS Code Application Builder.

Prefer composing ARNs with AWS::Partition, AWS::Region, and AWS::AccountId rather than embedding a literal arn:aws:... prefix. The resulting template also works on AWS GovCloud and AWS China without changes:

ManagedPolicyArns:
- !Sub "arn:${AWS::Partition}:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs"

Reference resources with !Ref and Fn::GetAtt

Section titled “Reference resources with !Ref and Fn::GetAtt”

When one resource needs the address of another, read it from the resource itself with !Ref or !GetAtt rather than constructing the URL from service domains. For example, use !GetAtt MyQueue.QueueUrl or !GetAtt MyBucket.DomainName so LocalStack returns the local endpoint while AWS returns the real one.

FeatureSupport
ParametersPartial
Dynamic ReferencesFull
Rules-
MappingsFull
ConditionsFull
TransformFull
OutputsFull
Custom resourcesPartial
Drift detection-
Importing Resources-
Change setsFull
Nested stacksPartial
StackSetsPartial
Intrinsic FunctionsPartial
Intrinsic FunctionSupportedExplanation
Fn::AndYesPerforms a logical AND operation on two or more expressions.
Fn::OrYesPerforms a logical OR operation on two or more expressions.
Fn::Base64YesConverts a binary string to a Base64-encoded string.
Fn::SubYesPerforms a string substitution operation.
Fn::SplitYesSplits a string into an array of strings.
Fn::LengthYesReturns the length of a string.
Fn::JoinYesJoins an array of strings into a single string.
Fn::FindInMapYesFinds a value in a map.
Fn::RefYesReferences a resource in the template.
Fn::GetAttYesGets an attribute from a resource.
Fn::IfYesPerforms a conditional evaluation.
Fn::ImportYesImports a value from another template.
Fn::ToJsonStringNoConverts an object or map into a json string.
Fn::CidrNoGenerates a CIDR block from the inputs.
Fn::GetAZsNoReturns a list of the Availability Zones of a region.

Pseudo parameters are built-in variables that CloudFormation resolves at deployment time. You can reference them with the Ref intrinsic function (for example, !Ref AWS::Region) or with Fn::Sub (for example, !Sub "${AWS::Region}"). LocalStack resolves each pseudo parameter to the equivalent value for the local environment, which lets the same template deploy against both AWS and LocalStack.

Pseudo ParameterSupportedValue in LocalStackValue in AWS
AWS::AccountIdYesThe account ID used by the stack (default: 000000000000)The AWS account ID of the account deploying the stack
AWS::NotificationARNsPartialEmpty listThe list of SNS topic ARNs passed to the stack via --notification-arns
AWS::NoValueYesRemoves the corresponding property when used as a return value in Fn::IfSame
AWS::PartitionYesawsaws, aws-cn, or aws-us-gov depending on the Region
AWS::RegionYesThe Region of the encompassing resourceSame
AWS::StackIdYesThe ARN of the stackSame
AWS::StackNameYesThe name of the stackSame
AWS::URLSuffixYesThe configured LOCALSTACK_HOST (default: localhost.localstack.cloud)amazonaws.com, or amazonaws.com.cn in China Regions
Resource TypeServiceCreateDeleteUpdate
AWS::ACMPCA::CertificateACMPCA-
AWS::ACMPCA::CertificateAuthorityACMPCA-
AWS::ACMPCA::CertificateAuthorityActivationACMPCA-
AWS::ACMPCA::PermissionACMPCA-
AWS::Amplify::AppAmplify-
AWS::Amplify::BranchAmplify-
AWS::ApiGateway::AccountApiGateway-
AWS::ApiGateway::ApiKeyApiGateway
AWS::ApiGateway::AuthorizerApiGateway-
AWS::ApiGateway::BasePathMappingApiGateway-
AWS::ApiGateway::DeploymentApiGateway-
AWS::ApiGateway::DomainNameApiGateway-
AWS::ApiGateway::GatewayResponseApiGateway-
AWS::ApiGateway::MethodApiGateway-
AWS::ApiGateway::ModelApiGateway-
AWS::ApiGateway::RequestValidatorApiGateway-
AWS::ApiGateway::ResourceApiGateway-
AWS::ApiGateway::RestApiApiGateway
AWS::ApiGateway::StageApiGateway-
AWS::ApiGateway::UsagePlanApiGateway-
Page 1 of 14
OperationImplementedVerified on Kubernetes
Page 1 of 0
Was this page helpful?