Using the GetLatestConfig API action to read a freeform configuration profile - Amazon AppConfig
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Using the GetLatestConfig API action to read a freeform configuration profile

Each of the following samples includes comments about the actions performed by the code. The samples in this section call the following APIs:

Java
public void retrieveConfigFromApi() { /* The example below uses two AppConfigData APIs: StartConfigurationSession and GetLatestConfiguration. For more information on these APIs, see Amazon AppConfig Data */ AppConfigDataClient appConfigData = AppConfigDataClient.create(); /* Start a new configuration session using the StartConfigurationSession API. This operation does not return configuration data. Rather, it returns an initial configuration token that should be passed to GetLatestConfiguration. IMPORTANT: This operation should only be performed once (per configuration), prior to the first GetLatestConfiguration call you preform. Each GetLatestConfiguration will return a new configuration token that you should then use in the next GetLatestConfiguration call. */ StartConfigurationSessionResponse session = appConfigData.startConfigurationSession(req -> req .applicationIdentifier("MyDemoApp") .configurationProfileIdentifier("MyConfigProfile") .environmentIdentifier("Beta")); /* Retrieve configuration data using the GetLatestConfiguration API. The first time you call this API your configuration data will be returned. You should cache that data (and the configuration token) and update that cache asynchronously by regularly polling the GetLatestConfiguration API in a background thread. If you already have the latest configuration data, subsequent GetLatestConfiguration calls will return an empty response. If you then deploy updated configuration data the next time you call GetLatestConfiguration it will return that updated data. You can also avoid all the complexity around writing this code yourself by leveraging our agent instead. For more information about the agent, see How to use Amazon AppConfig Agent */ // The first getLatestConfiguration call uses the token from StartConfigurationSession String configurationToken = session.initialConfigurationToken(); GetLatestConfigurationResponse configuration = appConfigData.getLatestConfiguration(GetLatestConfigurationRequest.builder().configurationToken(configurationToken).build()); System.out.println("Configuration retrieved via API: " + configuration.configuration().asUtf8String()); // You'll want to hold on to the token in the getLatestConfiguration response because you'll need to use it // the next time you call configurationToken = configuration.nextPollConfigurationToken(); configuration = appConfigData.getLatestConfiguration(GetLatestConfigurationRequest.builder().configurationToken(configurationToken).build()); // Try creating a new deployment at this point to see how the output below changes. if (configuration.configuration().asByteArray().length != 0) { System.out.println("Configuration contents have changed since the last GetLatestConfiguration call, new contents = " + configuration.configuration().asUtf8String()); } else { System.out.println("GetLatestConfiguration returned an empty response because we already have the latest configuration"); } }
Python
# the example below uses two AppConfigData APIs: StartConfigurationSession and GetLatestConfiguration. # # for more information on these APIs, see # Amazon AppConfig Data # import boto3 application_name = 'MyDemoApp' environment_name = 'MyEnvironment' config_profile_name = 'MyConfigProfile' appconfigdata = boto3.client('appconfigdata') # start a new configuration session. # this operation does not return configuration data. # rather, it returns an initial configuration token that should be passed to GetLatestConfiguration. # # note: this operation should only be performed once (per configuration). # all subsequent calls to AppConfigData should be via GetLatestConfiguration. scs = appconfigdata.start_configuration_session( ApplicationIdentifier=application_name, EnvironmentIdentifier=environment_name, ConfigurationProfileIdentifier=config_profile_name) initial_token = scs['InitialConfigurationToken'] # retrieve configuration data from the session. # this operation returns your configuration data. # each invocation of this operation returns a unique token that should be passed to the subsequent invocation. # # note: this operation does not always return configuration data after the first invocation. # data is only returned if the configuration has changed within Amazon AppConfig (i.e. a deployment occurred). # therefore, you should cache the data returned by this call so that you can use it later. glc = appconfigdata.get_latest_configuration(ConfigurationToken=initial_token) config = glc['Configuration'].read()
JavaScript
// the example below uses two AppConfigData APIs: StartConfigurationSession and GetLatestConfiguration. // for more information on these APIs, see // Amazon AppConfig Data import { AppConfigDataClient, GetLatestConfigurationCommand, StartConfigurationSessionCommand, } from "@aws-sdk/client-appconfigdata"; const appconfigdata = new AppConfigDataClient(); const application_name = "MyDemoApp"; const environment_name = "MyEnvironment"; const config_profile_name = "MyConfigProfile"; // start a new configuration session. // this operation does not return configuration data. // rather, it returns an initial configuration token that should be passed to GetLatestConfiguration. // // note: this operation should only be performed once (per configuration). // all subsequent calls to AppConfigData should be via GetLatestConfiguration. const scs = await appconfigdata.send( new StartConfigurationSessionCommand({ ApplicationIdentifier: application_name, EnvironmentIdentifier: environment_name, ConfigurationProfileIdentifier: config_profile_name, }) ); const { InitialConfigurationToken } = scs; // retrieve configuration data from the session. // this operation returns your configuration data. // each invocation of this operation returns a unique token that should be passed to the subsequent invocation. // // note: this operation does not always return configuration data after the first invocation. // data is only returned if the configuration has changed within Amazon AppConfig (i.e. a deployment occurred). // therefore, you should cache the data returned by this call so that you can use it later. const glc = await appconfigdata.send( new GetLatestConfigurationCommand({ ConfigurationToken: InitialConfigurationToken, }) ); const config = glc.Configuration.transformToString();