I’ve been working on setting up AWS Amplify JS to work with LocalStack for local development. Initial login works fine, but subsequent authentication requests and S3 operations fail. I’m following the guidance from various GitHub issues and documentation but still facing problems.
My code works with AWS for my dev/prod environments. I am trying LocalStack for the first time and need Amplify to correctly use and continue to use the defined localstack configs.
As a new user i can only attach 2 links, the config does have http prefixed on the localhost address.
import Amplify from 'aws-amplify';
import { Auth, Storage } from 'aws-amplify';
Amplify.configure({
Auth: {
region: 'us-east-1',
userPoolId: 'us-east-1_example',
userPoolWebClientId: 'exampleclientid',
identityPoolId: 'us-east-1:exampleidentitypool',
mandatorySignIn: true,
authenticationFlowType: 'USER_SRP_AUTH',
endpoint: 'localhost:4566', // LocalStack endpoint for Cognito with http prefix
},
Storage: {
AWSS3: {
bucket: 'example-bucket',
region: 'us-east-1',
endpoint: 'localhost:4566', // LocalStack endpoint for S3 with http prefix
s3ForcePathStyle: true // Required for LocalStack
}
},
API: {
endpoints: [
{
name: "ormAPI",
endpoint: "localhost:4566/restapis/exampleOrmApi/dev", (Http prefix removed)
custom_header: async () => {
return { Authorization: `Bearer ${(await Auth.currentSession()).getAccessToken().getJwtToken()}` };
}
}
]
}
});
Issues:
- Subsequent Authentication Requests Fail:
- The initial login using
Auth.signIn
works fine. However, subsequent requests (e.g., refreshing the session) do not seem to use the LocalStack endpoint. - Network requests in the browser’s developer tools show that they are still going to AWS endpoints instead of LocalStack.
- S3 Endpoint Issues:
- S3 operations, such as file uploads, do not correctly use the LocalStack endpoint.
- Despite setting
endpoint
ands3ForcePathStyle
, requests are still directed to AWS S3 endpoints.
Attempts to Resolve:
- I followed the advice from [this GitHub issue] (Cannot post links as new user) , which suggests that the
endpoint
parameter should work, but it doesn’t seem to apply correctly for subsequent requests. - The [amplify-localstack project] (Cannot post links as new user) appears to be more focused on deploying Amplify resources to LocalStack rather than configuring Amplify JS to use LocalStack endpoints at runtime.
Here is the network log…
As a new user i can only embed a single media item and two links.
In the gif the Cognito requests go to - cognito-identity.ap-southeast-2.amazonaws.com/
In the gif the S3 request goes to -
storagestack-dev-databucketd8691f4e-kkco8jw9wvo9.s3.ap-southeast-2.amazonaws.com/public/logos/172ce12a-3534-44f9-8ede-73ed4d875ba0/cognito%20copy.png
Any help would be appreciated.