Issues with Amplify JS and LocalStack: Authentication and S3 Endpoint Problems

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:

  1. 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.
  1. S3 Endpoint Issues:
  • S3 operations, such as file uploads, do not correctly use the LocalStack endpoint.
  • Despite setting endpoint and s3ForcePathStyle, 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…
2024-07-19 00.10.14

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.

Hi @MrDaniel,

We have documentation available for Amplify, alongside Amplify LocalStack plugin, that should make the configuration easier:

Please also see a demo of Amplify recorded during our community hours that hopefully answers your questions!


Hello,

Thank you for the response. I have looked at this project, however, it appears to be more focused on deploying Amplify resources using the CLI which i am not using as i have existing AWS Resources that are not made via the Amplify CLI.

They are also not using the scoped configuration
I am using the scoped configurations (Configure Amplify categories - React - AWS Amplify Gen 1 Documentation) since i need more than one API endpoint i have to used the scoped version.

I’ll keep digging, but please let me know if you have any other ideas.

On further inspection it looks like the network tab is showing an attempt to access AWS and THEN localhost…

Regards,

Daniel

Hello @MrDaniel . Hopefully you have seen this but there is a PR in progress to fix your issue.