|
| 1 | +--- |
| 2 | +title: Aspire |
| 3 | +description: Use the Aspire framework with LocalStack |
| 4 | +template: doc |
| 5 | +sidebar: |
| 6 | + order: 5 |
| 7 | +--- |
| 8 | + |
| 9 | +## Introduction |
| 10 | + |
| 11 | +[Aspire](https://aspire.dev/) is an opinionated, cloud-ready stack for building observable, production-ready distributed applications. It provides a consistent approach to service discovery, configuration, telemetry, and health checks across cloud-native applications. |
| 12 | + |
| 13 | +With Aspire, developers can orchestrate cloud-native applications locally using the same AWS resources they deploy in production. By combining Aspire with LocalStack, teams can emulate their full cloud environment—including Lambda, SQS, S3, and DynamoDB—with minimal configuration and no AWS costs. |
| 14 | + |
| 15 | +LocalStack integrates with Aspire through the [`LocalStack.Aspire.Hosting`](https://github.com/localstack-dotnet/dotnet-aspire-for-localstack) package, enabling seamless local development and testing of AWS-powered applications within the Aspire orchestration framework. This package extends the official [AWS integrations for .NET Aspire](https://github.com/aws/integrations-on-dotnet-aspire-for-aws) to provide LocalStack-specific functionality. |
| 16 | + |
| 17 | +## Getting started |
| 18 | + |
| 19 | +This guide demonstrates how to integrate LocalStack into Aspire projects for local AWS service emulation. |
| 20 | + |
| 21 | +### Prerequisites |
| 22 | + |
| 23 | +- [.NET 8 SDK](https://dotnet.microsoft.com/download/dotnet/8.0) or later |
| 24 | +- [Docker Desktop](https://docs.docker.com/get-docker/) or compatible container runtime |
| 25 | +- [Node.js](https://nodejs.org/) (for AWS CDK infrastructure provisioning) |
| 26 | +- Basic familiarity with [Aspire concepts](https://aspire.dev/get-started/welcome/) |
| 27 | + |
| 28 | +### Installation |
| 29 | + |
| 30 | +Add the LocalStack Aspire integration to your App Host project: |
| 31 | + |
| 32 | +```bash |
| 33 | +dotnet add package LocalStack.Aspire.Hosting |
| 34 | +``` |
| 35 | + |
| 36 | +For projects that need to interact with AWS services, add the LocalStack.NET client: |
| 37 | + |
| 38 | +```bash |
| 39 | +dotnet add package LocalStack.Client |
| 40 | +``` |
| 41 | + |
| 42 | +## Usage |
| 43 | + |
| 44 | +Configure LocalStack integration in your Aspire AppHost project using auto-configuration: |
| 45 | + |
| 46 | +```csharp |
| 47 | +var builder = DistributedApplication.CreateBuilder(args); |
| 48 | + |
| 49 | +// 1. Set up AWS SDK configuration (optional) |
| 50 | +var awsConfig = builder.AddAWSSDKConfig() |
| 51 | + .WithProfile("default") |
| 52 | + .WithRegion(RegionEndpoint.USWest2); |
| 53 | + |
| 54 | +// 2. Add LocalStack container |
| 55 | +var localstack = builder |
| 56 | + .AddLocalStack(awsConfig: awsConfig, configureContainer: container => |
| 57 | + { |
| 58 | + container.Lifetime = ContainerLifetime.Session; |
| 59 | + container.DebugLevel = 1; |
| 60 | + container.LogLevel = LocalStackLogLevel.Debug; |
| 61 | + }); |
| 62 | + |
| 63 | +// 3. Add your AWS resources as usual |
| 64 | +var awsResources = builder.AddAWSCloudFormationTemplate("resources", "template.yaml") |
| 65 | + .WithReference(awsConfig); |
| 66 | + |
| 67 | +var project = builder.AddProject<Projects.MyService>("api") |
| 68 | + .WithReference(awsResources); |
| 69 | + |
| 70 | +// 4. Auto-configure LocalStack for all AWS resources |
| 71 | +builder.UseLocalStack(localstack); |
| 72 | + |
| 73 | +builder.Build().Run(); |
| 74 | +``` |
| 75 | + |
| 76 | +The `UseLocalStack()` method automatically: |
| 77 | + |
| 78 | +- Detects all AWS resources (CloudFormation, CDK stacks) |
| 79 | +- Configures LocalStack endpoints for all AWS services and project resources |
| 80 | +- Sets up proper dependency ordering and CDK bootstrap if needed |
| 81 | +- Transfers LocalStack configuration to service projects via environment variables |
| 82 | + |
| 83 | +## AWS SDK Configuration |
| 84 | + |
| 85 | +When using the AWS SDK for .NET with LocalStack in an Aspire context, the SDK clients need to be configured to point to the LocalStack endpoint. |
| 86 | + |
| 87 | +### Using LocalStack.NET |
| 88 | + |
| 89 | +The [`LocalStack.Client`](https://github.com/localstack-dotnet/localstack-dotnet-client) library simplifies AWS SDK configuration: |
| 90 | + |
| 91 | +```csharp |
| 92 | +services.AddLocalStack(configuration); |
| 93 | +services.AddDefaultAWSOptions(configuration.GetAWSOptions()); |
| 94 | +services.AddAwsService<IAmazonS3>(); |
| 95 | +services.AddAwsService<IAmazonDynamoDB>(); |
| 96 | +``` |
| 97 | + |
| 98 | +This automatically configures AWS service clients to use the LocalStack endpoint when running locally. See the [.NET](/aws/integrations/aws-sdks/net/) guide for more information. |
| 99 | + |
| 100 | +## Infrastructure Provisioning |
| 101 | + |
| 102 | +LocalStack integrates well with Infrastructure as Code tools within the Aspire orchestration model. |
| 103 | + |
| 104 | +### AWS CDK Integration |
| 105 | + |
| 106 | +You can provision AWS resources using AWS CDK during application startup: |
| 107 | + |
| 108 | +```csharp |
| 109 | +var awsConfig = builder.AddAWSSDKConfig() |
| 110 | + .WithProfile("default") |
| 111 | + .WithRegion(RegionEndpoint.USWest2); |
| 112 | + |
| 113 | +var localstack = builder.AddLocalStack("localstack"); |
| 114 | + |
| 115 | +var customStack = builder |
| 116 | + .AddAWSCDKStack("custom", scope => new CustomStack(scope, "Aspire-custom")) |
| 117 | + .WithReference(awsConfig); |
| 118 | +``` |
| 119 | + |
| 120 | +You can use AWS CDK Stack classes to define and deploy resources: |
| 121 | + |
| 122 | +```csharp |
| 123 | +// An excerpt of a CDK Stack class |
| 124 | +internal sealed class CustomStack : Stack |
| 125 | +{ |
| 126 | + public CustomStack(Construct scope, string id) : base(scope, id) |
| 127 | + { |
| 128 | + // Example resources |
| 129 | + var bucket = new Bucket(this, "Bucket"); |
| 130 | + var topic = new Topic(this, "ChatTopic"); |
| 131 | + |
| 132 | + var queue = new Queue(this, "ChatMessagesQueue", new QueueProps |
| 133 | + { |
| 134 | + VisibilityTimeout = Duration.Seconds(30), |
| 135 | + }); |
| 136 | + |
| 137 | + topic.AddSubscription(new SqsSubscription(queue)); |
| 138 | + |
| 139 | + // ... (rest of the stack) |
| 140 | + } |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +:::note |
| 145 | +For detailed AWS CDK integration patterns with LocalStack and Aspire, refer to the [provisioning playground example](https://github.com/localstack-dotnet/dotnet-aspire-for-localstack/tree/master/playground/provisioning). |
| 146 | +::: |
| 147 | + |
| 148 | +## Configuration Reference |
| 149 | + |
| 150 | +For comprehensive configuration options, including environment variables, container settings, and advanced scenarios, refer to the [Configuration Guide](https://github.com/localstack-dotnet/dotnet-aspire-for-localstack/blob/master/docs/CONFIGURATION.md). |
| 151 | + |
| 152 | +## Sample Projects |
| 153 | + |
| 154 | +### Playground Examples |
| 155 | + |
| 156 | +The [playground examples](https://github.com/localstack-dotnet/dotnet-aspire-for-localstack/tree/master/playground) include Lambda development patterns and infrastructure provisioning with AWS CDK. |
| 157 | + |
| 158 | +### LocalStack Serverless .NET Demo |
| 159 | + |
| 160 | +A reference implementation demonstrating serverless applications with Lambda functions, S3, DynamoDB, SQS, and CDK provisioning: [localstack-serverless-dotnet-demo](https://github.com/localstack-dotnet/localstack-serverless-dotnet-demo) |
| 161 | + |
| 162 | +### OpenTelemetry with Aspire and LocalStack |
| 163 | + |
| 164 | +An event registration system showcasing distributed tracing and observability patterns with Lambda and SQS: [dotnet-otel-aspire-localstack-demo](https://github.com/Blind-Striker/dotnet-otel-aspire-localstack-demo) |
| 165 | + |
| 166 | +## Resources |
| 167 | + |
| 168 | +- [Aspire Documentation](https://aspire.dev/) |
| 169 | +- [LocalStack.Aspire.Hosting on GitHub](https://github.com/localstack-dotnet/dotnet-aspire-for-localstack) |
| 170 | +- [LocalStack.Client on GitHub](https://github.com/localstack-dotnet/localstack-dotnet-client) |
| 171 | +- [AWS Aspire Integration](https://github.com/aws/integrations-on-dotnet-aspire-for-aws) |
| 172 | +- [AWS SDK for .NET Documentation](https://docs.aws.amazon.com/sdk-for-net/) |
| 173 | +- [LocalStack Serverless .NET Demo](https://github.com/localstack-dotnet/localstack-serverless-dotnet-demo) |
| 174 | +- [OpenTelemetry with Aspire and LocalStack Demo](https://github.com/Blind-Striker/dotnet-otel-aspire-localstack-demo) |
0 commit comments