Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup different configuration(awsconfiguration.json) with AWSMobileClient for debug and release build types

I want to have different configuration for debug and release builds. All the configuration is stored inside awsconfiguration.json, for example I have two different config files how can I set which file should be used.

When using AWSMobileClient.getInstance() it gets default configuration from file awsconfiguration.json

Configuration file example:

{
  "Version": "1.0",
  "CredentialsProvider": {
    "CognitoIdentity": {
      "Default": {
        "PoolId": "DIFFERENT_VALUES",
        "Region": "DIFFERENT_VALUES"
      }
    }
  },
  "IdentityManager": {
    "Default": {}
  },
  "CognitoUserPool": {
    "Default": {
      "AppClientSecret": "DIFFERENT_VALUES",
      "AppClientId": "DIFFERENT_VALUES",
      "PoolId": "DIFFERENT_VALUES",
      "Region": "DIFFERENT_VALUES"
    }
  }
}

Update There is option to use different awsconfiguration.json by puting different files in main\res\raw and release\res\raw, for example by following this answer and it works. But I'm wondering whether there is an option to do it programmatically.

like image 845
Roman Nazarevych Avatar asked Jun 26 '19 11:06

Roman Nazarevych


2 Answers

This can also be acheived by setting the configuration value in AWSConfiguration and then initializing the AWSMobileClient.

    AWSConfiguration awsConfiguration = new AWSConfiguration(context);
    awsConfiguration.setConfiguration("Stage"); // BuildConfig can be used here.

    AWSMobileClient.getInstance().initialize(context, awsConfiguration,  new Callback<UserStateDetails>() {

        @Override
        public void onResult(UserStateDetails userStateDetails) {
        }

        @Override
        public void onError(Exception e) {
        }
    });

And the awsconfiguration.json file can be updated as below

{
 "Version": "1.0",
 "CredentialsProvider": {
    "CognitoIdentity": {
        "Default": {
            "PoolId": "DIFFERENT_VALUES",
            "Region": "DIFFERENT_VALUES"
        },
        "Stage": {
            "PoolId": "STAGE_VALUES",
            "Region": "STAGE_VALUES"
        }
    }
 },
 "IdentityManager": {
    "Default": {},
    "Stage": {}
 },
 "CognitoUserPool": {
    "Default": {
        "AppClientSecret": "DIFFERENT_VALUES",
        "AppClientId": "DIFFERENT_VALUES",
        "PoolId": "DIFFERENT_VALUES",
        "Region": "DIFFERENT_VALUES"
    },
    "Stage": {
        "AppClientSecret": "STAGE_VALUES",
        "AppClientId": "STAGE_VALUES",
        "PoolId": "STAGE_VALUES",
        "Region": "STAGE_VALUES"
    }
 }
}
like image 188
Abhishek Anand Avatar answered Nov 14 '22 13:11

Abhishek Anand


I've been trying to achieve something similar; selecting an AWS configuration at runtime based on a selected profile. I got it partially working by hacking the AWS SDK but then stumbled across release notes for AWS SDK version 2.11.0. Quoting:

Added the option of passing the configuration as an in-memory object (i.e. [String: Any]/NSDictionary) instead of the default awsconfiguration.json through the new API

I've also found it documented(!) in the amplify getting started guide here.

So since 9th September 2019 it IS possible to select an AWS configuration at runtime.


Edit: Just noticed that this question is for Android rather than iOS. I'm not an Android developer but a quick searched revealed something similar in AWS Android SDK release 2.13.6 (7th June 2019). Quoting the release notes:

Add AWSConfiguration(JSONObject) constructor to construct a AWSConfiguration object from the configuration passed via a JSONObject

... which looks promising.

like image 8
Nick Ager Avatar answered Nov 14 '22 13:11

Nick Ager