AWS Emulator¶
AWS Emulator is a local emulator of the AWS APIs. It runs a single HTTPS endpoint, generates a self-signed certificate and credentials during the startup, and requires each request to be signed with these credentials.
Note
AWS does not publish an official AWS Emulator image yet. The examples use an unofficial community image. Any image works as long as it uses the aws-emulator executable as its entrypoint.
Add the following dependency to your project file:
| NuGet | |
|---|---|
1 | |
You can start an AWS Emulator container instance from any .NET application. This example uses the default configuration:
1 | |
This example uses xUnit.net's IAsyncLifetime interface to manage the lifecycle of the container. The container is started in the InitializeAsync method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the container is removed in the DisposeAsync method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
The test example creates an S3 bucket, uploads an object and downloads it again. Use GetConnectionString() as the service URL of the AWS client and AwsEmulatorBuilder.Region as the authentication region.
The test example uses the following NuGet dependencies:
1 2 3 4 5 6 | |
To execute the tests, use the command dotnet test from a terminal.
Tip
For the complete source code of this example and additional information, please refer to our test projects.
Access AWS Emulator¶
The emulator generates its credentials and its certificate during the startup. Read them after the container has started:
| Gets the AWS Emulator connection string | |
|---|---|
1 | |
| Gets the AWS Emulator certificate | |
|---|---|
1 | |
| Gets the AWS Emulator credentials | |
|---|---|
1 | |
The emulator rejects requests that are not signed with the generated credentials. The credentials are valid for the emulator instance only and cannot authenticate to AWS.
Trust the certificate¶
The emulator accepts HTTPS requests only. Instead of accepting any certificate, configure the AWS client to trust the self-signed certificate of the emulator instance. In this example, the HTTP client factory validates the certificate against the certificate of the emulator. The factory is assigned to the HttpClientFactory property of the AWS client configuration, as shown in the usage example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
Note
The certificate is valid for localhost, 127.0.0.1 and ::1. If the Docker host is reached through another hostname or IP address, the certificate validation fails because of the name mismatch.
Limitations¶
Some AWS services return URLs or open additional listeners for their resources. These URLs contain the address and port of the emulator inside the container, and the module does not rewrite them or map the additional ports. For example, the SQS queue URL contains the container port. The AWS client uses it to identify the queue, but sends the requests to the configured service URL.
The module reads the generated certificate and credentials from a directory that depends on the HOME environment variable and the port of the emulator. Do not override them, otherwise GetCertificateAsync() and GetCredentialsAsync() fail.
Default configuration¶
The builder applies the following default configuration:
- Port:
9297(seeAwsEmulatorBuilder.AwsEmulatorPort) - Environment variable:
HOME=/tmp/testcontainers-aws - Environment variable:
EMULATOR_TELEMETRY=off - Command:
--bind 0.0.0.0 --port 9297 --auth on - Wait strategy: Waits until the HTTPS request
GET /__admin/healthto port9297is completed successfully.