You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS Mobile Hub开发与生产环境区分及React Native配置问询

Great question! Separating development and production environments is a critical best practice for any app, and your AWS Mobile Hub stack is totally set up to support this. Let’s walk through exactly how to pull this off for each part of your project, plus how to wire up your React Native code to switch between them seamlessly.

1. Setting Up Isolated Development/Production AWS Resources

First, you’ll want to create duplicate, independent resources for each environment to avoid cross-contamination (like dev users accidentally showing up in prod, or test data breaking your live app).

Cognito (User Authentication)

  • Create two separate Cognito User Pools – name them something like MyApp-UserPool-Dev and MyApp-UserPool-Prod. Each gets its own app client ID, password policies, and user database (so dev sign-ups never touch prod).
  • Pair each User Pool with a dedicated Cognito Identity Pool. This ensures dev and prod users get unique IAM permissions and don’t share access to other AWS resources like DynamoDB.

DynamoDB (NoSQL Database)

  • Spin up duplicate tables for dev and prod (e.g., MyApp-Items-Dev and MyApp-Items-Prod). You can copy the table schema from dev to prod via the AWS Console or CLI, but make sure to exclude any test data (or sanitize it if you need a sample prod dataset).
  • For cost savings, skip auto-scaling on dev tables unless you’re running high-volume tests – prod tables should have auto-scaling enabled to handle live traffic.

API Gateway + Lambda (Cloud Logic)

Yes, you absolutely should deploy a dedicated Production stage alongside your existing Development stage. Here’s why and how:

  • Stages let you run different versions of your Lambda functions: keep a dev version with debug logs and test endpoints, while prod uses an optimized, log-minimized version.
  • Set up separate throttling limits, API keys, and monitoring for each stage (e.g., stricter throttling on prod to prevent abuse).
  • For Lambda, create aliases like dev and prod pointing to the respective function versions, then link each API Gateway stage to the matching Lambda alias. This way, updating dev code won’t affect prod until you explicitly deploy the prod alias.
2. Configuring React Native to Switch Environments

The goal here is to load environment-specific configs without hardcoding values. Here are two reliable approaches:

Option 1: Use React Native’s Built-in __DEV__ Flag (Simple & No Dependencies)

  • Create separate config files for each environment:
    // env.dev.js
    export default {
      cognitoUserPoolId: 'us-east-1_XXXXXXXXX',
      cognitoAppClientId: 'XXXXXXXXXXXXXXXXXXXXXXXXXX',
      cognitoIdentityPoolId: 'us-east-1:XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
      apiGatewayUrl: 'https://xxxxxx.execute-api.us-east-1.amazonaws.com/Development',
      dynamoDbTable: 'MyApp-Items-Dev'
    };
    
    // env.prod.js
    export default {
      cognitoUserPoolId: 'us-east-1_YYYYYYYYY',
      cognitoAppClientId: 'YYYYYYYYYYYYYYYYYYYYYYYYYY',
      cognitoIdentityPoolId: 'us-east-1:YYYYYYYY-YYYY-YYYY-YYYY-YYYYYYYYYYYY',
      apiGatewayUrl: 'https://yyyyyy.execute-api.us-east-1.amazonaws.com/Production',
      dynamoDbTable: 'MyApp-Items-Prod'
    };
    
  • Create a loader file to pick the right config based on build mode:
    // env.js
    let envConfig;
    if (__DEV__) {
      // __DEV__ is auto-set to true for debug builds, false for release builds
      envConfig = require('./env.dev').default;
    } else {
      envConfig = require('./env.prod').default;
    }
    export default envConfig;
    
  • Import this config wherever you initialize AWS services (e.g., Amplify setup):
    import env from './env';
    import Amplify from 'aws-amplify';
    
    Amplify.configure({
      Auth: {
        userPoolId: env.cognitoUserPoolId,
        userPoolWebClientId: env.cognitoAppClientId,
        identityPoolId: env.cognitoIdentityPoolId
      },
      API: {
        endpoints: [
          {
            name: 'MyAppApi',
            endpoint: env.apiGatewayUrl
          }
        ]
      }
    });
    

Option 2: Use react-native-config (For Build-Time Flexibility)

If you need to switch environments without changing code (e.g., for staging builds), use the react-native-config package:

  • Install the package, then create .env.dev and .env.prod files with your config values.
  • Update your package.json scripts to specify which env file to use:
    "scripts": {
      "start:dev": "ENVFILE=.env.dev react-native start",
      "build:dev": "ENVFILE=.env.dev react-native run-android",
      "build:prod": "ENVFILE=.env.prod react-native run-android --variant=release"
    }
    
  • Access variables in code like Config.COGNITO_USER_POOL_ID.
3. Bonus: Automate Environment Deployments
  • For CI/CD, set up separate pipelines (e.g., AWS CodePipeline) for dev and prod. Each pipeline deploys to its respective AWS resources and uses the correct config.
  • If you’re using AWS Amplify, take advantage of its built-in multi-environment support: create dev and prod branches that auto-deploy to isolated AWS resources with zero manual config switching.

内容的提问来源于stack exchange,提问作者Mark McKim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:04:42