|
| 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 | +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. |
| 14 | + |
| 15 | +## Getting started |
| 16 | + |
| 17 | +This guide demonstrates how to integrate LocalStack into Aspire projects for local AWS service emulation. |
| 18 | + |
| 19 | +### Prerequisites |
| 20 | + |
| 21 | +- [.NET 8 SDK](https://dotnet.microsoft.com/download/dotnet/8.0) or later |
| 22 | +- [Docker Desktop](https://docs.docker.com/get-docker/) or compatible container runtime |
| 23 | +- [Node.js](https://nodejs.org/) (for AWS CDK infrastructure provisioning) |
| 24 | +- Basic familiarity with [Aspire concepts](https://aspire.dev/get-started/welcome/) |
| 25 | + |
| 26 | +### Installation |
| 27 | + |
| 28 | +Add the LocalStack Aspire integration to your App Host project: |
| 29 | + |
| 30 | +```bash |
| 31 | +dotnet add package LocalStack.Aspire.Hosting |
| 32 | +``` |
| 33 | + |
| 34 | +For projects that need to interact with AWS services, add the LocalStack.NET client: |
| 35 | + |
| 36 | +```bash |
| 37 | +dotnet add package LocalStack.Client |
| 38 | +``` |
| 39 | + |
| 40 | +## Usage |
| 41 | + |
| 42 | +Configure LocalStack integration in your Aspire AppHost project using auto-configuration: |
| 43 | + |
| 44 | +```csharp |
| 45 | +var builder = DistributedApplication.CreateBuilder(args); |
| 46 | + |
| 47 | +// 1. Set up AWS SDK configuration (optional) |
| 48 | +var awsConfig = builder.AddAWSSDKConfig() |
| 49 | + .WithProfile("default") |
| 50 | + .WithRegion(RegionEndpoint.USWest2); |
| 51 | + |
| 52 | +// 2. Add LocalStack container |
| 53 | +var localstack = builder |
| 54 | + .AddLocalStack(awsConfig: awsConfig, configureContainer: container => |
| 55 | + { |
| 56 | + container.Lifetime = ContainerLifetime.Session; |
| 57 | + container.DebugLevel = 1; |
| 58 | + container.LogLevel = LocalStackLogLevel.Debug; |
| 59 | + }); |
| 60 | + |
| 61 | +// 3. Add your AWS resources as usual |
| 62 | +var awsResources = builder.AddAWSCloudFormationTemplate("resources", "template.yaml") |
| 63 | + .WithReference(awsConfig); |
| 64 | + |
| 65 | +var project = builder.AddProject<Projects.MyService>("api") |
| 66 | + .WithReference(awsResources); |
| 67 | + |
| 68 | +// 4. Auto-configure LocalStack for all AWS resources |
| 69 | +builder.UseLocalStack(localstack); |
| 70 | + |
| 71 | +builder.Build().Run(); |
| 72 | +``` |
| 73 | + |
| 74 | +The `UseLocalStack()` method automatically: |
| 75 | + |
| 76 | +- Detects all AWS resources (CloudFormation, CDK stacks) |
| 77 | +- Configures LocalStack endpoints for all AWS services and project resources |
| 78 | +- Sets up proper dependency ordering and CDK bootstrap if needed |
| 79 | +- Transfers LocalStack configuration to service projects via environment variables |
| 80 | + |
| 81 | +## AWS SDK Configuration |
| 82 | + |
| 83 | +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. |
| 84 | + |
| 85 | +### Using LocalStack.NET |
| 86 | + |
| 87 | +The [`LocalStack.Client`](https://github.com/localstack-dotnet/localstack-dotnet-client) library simplifies AWS SDK configuration: |
| 88 | + |
| 89 | +```csharp |
| 90 | +services.AddLocalStack(configuration); |
| 91 | +services.AddDefaultAWSOptions(configuration.GetAWSOptions()); |
| 92 | +services.AddAwsService<IAmazonS3>(); |
| 93 | +services.AddAwsService<IAmazonDynamoDB>(); |
| 94 | +``` |
| 95 | + |
| 96 | +This automatically configures AWS service clients to use the LocalStack endpoint when running locally. |
| 97 | + |
| 98 | +## Infrastructure Provisioning |
| 99 | + |
| 100 | +LocalStack integrates well with Infrastructure as Code tools within the Aspire orchestration model. |
| 101 | + |
| 102 | +### AWS CDK Integration |
| 103 | + |
| 104 | +You can provision AWS resources using AWS CDK during application startup: |
| 105 | + |
| 106 | +```csharp |
| 107 | +var awsConfig = builder.AddAWSSDKConfig() |
| 108 | + .WithProfile("default") |
| 109 | + .WithRegion(RegionEndpoint.USWest2); |
| 110 | + |
| 111 | +var localstack = builder.AddLocalStack("localstack"); |
| 112 | + |
| 113 | +var customStack = builder |
| 114 | + .AddAWSCDKStack("custom", scope => new CustomStack(scope, "Aspire-custom")) |
| 115 | + .WithReference(awsConfig); |
| 116 | +``` |
| 117 | + |
| 118 | +You can use AWS CDK Stack classes to define and deploy resources: |
| 119 | + |
| 120 | +```csharp |
| 121 | +internal sealed class CustomStack : Stack |
| 122 | +{ |
| 123 | + public IBucket Bucket { get; } |
| 124 | + |
| 125 | + public ITopic ChatTopic { get; } |
| 126 | + |
| 127 | + public IQueue ChatMessagesQueue { get; } |
| 128 | + |
| 129 | + public ITable ChatMessagesTable { get; } |
| 130 | + |
| 131 | + public CustomStack(Construct scope, string id) |
| 132 | + : base(scope, id) |
| 133 | + { |
| 134 | + // Keep bucket for potential future demonstration |
| 135 | + Bucket = new Bucket(this, "Bucket"); |
| 136 | + |
| 137 | + ChatTopic = new Topic(this, "ChatTopic"); |
| 138 | + |
| 139 | + ChatMessagesQueue = new Queue(this, "ChatMessagesQueue", new QueueProps |
| 140 | + { |
| 141 | + VisibilityTimeout = Duration.Seconds(30), |
| 142 | + }); |
| 143 | + |
| 144 | + ChatTopic.AddSubscription(new SqsSubscription(ChatMessagesQueue)); |
| 145 | + |
| 146 | + var chatMessagesTable = new Table(this, "ChatMessagesTable", new TableProps |
| 147 | + { |
| 148 | + TableName = "ChatMessages", |
| 149 | + PartitionKey = new Attribute { Name = "MessageId", Type = AttributeType.STRING }, |
| 150 | + SortKey = new Attribute { Name = "Timestamp", Type = AttributeType.NUMBER }, |
| 151 | + BillingMode = BillingMode.PAY_PER_REQUEST, |
| 152 | + }); |
| 153 | + |
| 154 | + chatMessagesTable.AddGlobalSecondaryIndex(new GlobalSecondaryIndexProps |
| 155 | + { |
| 156 | + IndexName = "TimestampIndex", |
| 157 | + PartitionKey = new Attribute { Name = "Timestamp", Type = AttributeType.NUMBER }, |
| 158 | + ProjectionType = ProjectionType.ALL, |
| 159 | + }); |
| 160 | + |
| 161 | + ChatMessagesTable = chatMessagesTable; |
| 162 | + } |
| 163 | +} |
| 164 | +``` |
| 165 | + |
| 166 | +:::note |
| 167 | +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). |
| 168 | +::: |
| 169 | + |
| 170 | +## Configuration Reference |
| 171 | + |
| 172 | +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). |
| 173 | + |
| 174 | +## Sample Projects |
| 175 | + |
| 176 | +### Playground Examples |
| 177 | + |
| 178 | +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. |
| 179 | + |
| 180 | +### LocalStack Serverless .NET Demo |
| 181 | + |
| 182 | +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) |
| 183 | + |
| 184 | +### OpenTelemetry with Aspire and LocalStack |
| 185 | + |
| 186 | +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) |
| 187 | + |
| 188 | +## Resources |
| 189 | + |
| 190 | +- [Aspire Documentation](https://aspire.dev/) |
| 191 | +- [LocalStack.Aspire.Hosting on GitHub](https://github.com/localstack-dotnet/dotnet-aspire-for-localstack) |
| 192 | +- [LocalStack.Client on GitHub](https://github.com/localstack-dotnet/localstack-dotnet-client) |
| 193 | +- [AWS Aspire Integration](https://github.com/aws/integrations-on-dotnet-aspire-for-aws) |
| 194 | +- [AWS SDK for .NET Documentation](https://docs.aws.amazon.com/sdk-for-net/) |
| 195 | +- [LocalStack Serverless .NET Demo](https://github.com/localstack-dotnet/localstack-serverless-dotnet-demo) |
| 196 | +- [OpenTelemetry with Aspire and LocalStack Demo](https://github.com/Blind-Striker/dotnet-otel-aspire-localstack-demo) |
0 commit comments