Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion alchemy/src/aws/control/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { loadConfig } from "@smithy/node-config-provider";
import { AwsClient } from "aws4fetch";
import { logger } from "../../util/logger.ts";
import { safeFetch } from "../../util/safe-fetch.ts";
import { awsEndpoint } from "../utils.ts";
import {
AlreadyExistsError,
CloudControlError,
Expand Down Expand Up @@ -355,7 +356,7 @@ export class CloudControlClient {
while (true) {
try {
const args = [
`https://cloudcontrolapi.${this.region}.amazonaws.com/?Action=${action}&Version=2021-09-30`,
`${awsEndpoint("cloudcontrolapi", this.region)}/?Action=${action}&Version=2021-09-30`,
{
method: "POST",
headers: {
Expand Down
8 changes: 5 additions & 3 deletions alchemy/src/aws/ec2/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { logger } from "../../util/logger.ts";
import { flattenParams } from "../../util/params.ts";
import type { AwsClientProps } from "../client-props.ts";
import { resolveAwsCredentials } from "../credentials.ts";
import { getRegion } from "../utils.ts";
import { awsEndpoint, getRegion } from "../utils.ts";

/**
* Shared EC2 utilities for all EC2 resources
Expand Down Expand Up @@ -100,8 +100,10 @@ export async function callEC2Api<T>(
// Try the API call, and retry once with fresh credentials on auth failure
for (let attempt = 1; attempt <= 2; attempt++) {
try {
// Use the client's region instead of the global region
const url = `https://ec2.${client.region}.amazonaws.com/`;
// Use the client's region instead of the global region.
// Honour AWS_ENDPOINT_URL / AWS_ENDPOINT_URL_EC2 for emulators
// (LocalStack, moto, etc.) — matches AWS SDK v3 behaviour.
const url = `${awsEndpoint("ec2", client.region ?? "us-east-1")}/`;

const body = new URLSearchParams({
Action: action,
Expand Down
22 changes: 20 additions & 2 deletions alchemy/src/aws/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,29 @@ export const getRegion: Provider<string> = loadConfig({
default: "us-east-1",
});

/**
* Resolve the endpoint URL for an aws4fetch service request, honouring the
* same `AWS_ENDPOINT_URL_<SERVICE>` / `AWS_ENDPOINT_URL` env vars as the AWS
* SDK / CLI so aws4fetch-based resources can be pointed at LocalStack, moto,
* or any other emulator.
*/
export function awsEndpoint(service: string, region: string): string {
return (
process.env[
`AWS_ENDPOINT_URL_${service.toUpperCase().replace(/-/g, "_")}`
] ??
process.env.AWS_ENDPOINT_URL ??
`https://${service}.${region}.amazonaws.com`
).replace(/\/+$/, "");
}

/**
* AWS Service Configuration
*/
export interface AwsServiceConfig {
service: string;
version: string;
endpoint: (region: string) => string;
endpoint?: (region: string) => string;
}

/**
Expand Down Expand Up @@ -63,7 +79,9 @@ export function getAwsApiCaller<T = any>(
for (let attempt = 1; attempt <= 2; attempt++) {
try {
const region = await getRegion();
const url = config.endpoint(region);
const url = config.endpoint
? config.endpoint(region)
: `${awsEndpoint(config.service, region)}/`;

const body = new URLSearchParams({
Action: action,
Expand Down
Loading