AppConfig Python

AWS Appconfig Python

  • Without having to perform time-consuming code deployments, you can manage and quickly deploy application configurations with AWS AppConfig. Using AWS AppConfig, you may produce an application configuration, validate it for syntax or semantic errors, and deploy it to your targets at a controlled rate while the application is operating.
  • To reduce application outages, AWS AppConfig rolls back the configuration if issues are encountered during deployment. Your application must poll AWS AppConfig after the deployment is finished to get the latest configuration update.
  • You can include the AWS AppConfig helper library, a model open-source Python library, to your code to easily integrate AWS AppConfig with your application while adhering to best practices.

These are the High-Level Steps

  • Create the "AWS AppConfig" resources that will be used to store the configuration. Create the Python program with the example AWS AppConfig helper library included.
  • Check out the program.
  • To observe the changes take effect, deploy a fresh configuration version and test the application.

Prerequisites

  • A command-line environment with an AWS account and AWS credentials set up (typically in /.aws/credentials).
  • Python 3 is set up. (Python 2's sunset date has passed. The AWS AppConfig helper library doesn't support it.)
  • A device for making HTTP requests. On the command line, you can, for instance, use curl or httpie. If Graphical User Interface is your preference, then Postman or web browser is a choice to rely on.

How to Create AWS AppConfig Resources

To save the configuration for your application, first, build the AWS AppConfig resources. These sources consist of:

  • An application is a logical piece of code that gives your customers capabilities.
  • A deployment group known as an environment (for example, beta or production).
  • A configuration profile that grants access to your configuration data to AWS AppConfig.

AWS CloudFormation template can be launched to create a variety of resources. Ascertain that your command line environment is configured to use AWS Region us-east-1, which is the same region as the template.

In Order to Create, on Your Own, Resources, the Below-Given Steps must be Followed:

  • Log in to the AWS Management Console and make sure you're using the same AWS Region as the one defined in your command line environment.
  • In the search box, type AppConfig to open the AWS Systems Manager console.
  • Select Create configuration data if the AWS AppConfig welcome screen opens. Choose to Create application if not.
  • In the Name field, type DemoApp, and then click Create application.
  • Make a choice to Create an environment on the details page.
  • Enter prod in the Name field, then click Create environment.
  • Select Start deployment from the details page.
  • To create a configuration profile, select it.
  • Type main into Name.
  • Select AWS AppConfig hosted configuration as the configuration source.
  • Select JSON under Content, type the following JSON, and then select Next. Your app will be given this configuration via AWS AppConfig.
  • On the Add validators page, you should not select either option. To create a configuration profile, select it.
  • Select primary under Deployment details under Configuration.
  • Select 1 under Hosted configuration version.
  • Pick AppConfig from the Deployment strategy menu.AllAtOnce (Quick).
  • Select Launch the deployment.

The JSON document of yours as a configuration version 1 will be handed in when the software requests it.

How to Create the Python Application

Make the Python programme next. The Virtual Environment space should be utilized to install the libraries(Python) as a good exercise.The solution makes use of the FastAPI package to provide API capabilities, as well as the AWS AppConfig utility and unicorn to provide an HTTP server.

Note: The Python package installer, pip, and python were taken into consideration when writing these instructions since they both presume a Python 3 installation. Instead, pip3 and python3 may be required.

1. Make a folder where you will build and store the project.

2. Create a virtual environment:

3. Get the virtual environment active:

Note: You must run this command in each new terminal window or shell that you open.

In your editor, save the following text as rqumnts.txt in the appconfig-demo folder.

4. Install the libraries.

5. Create a new file in your editor with the following contents, and then save it as main.py and place it in the appconfig-demo folder.

This code first imports the necessary libraries before creating an instance of the AppConfigHelper class. The library has instructed the configuration to obtain from the AWS AppConfig API by the first three values given. These are the names you supplied to the application, environment, and profile previously. The final value is the time between API queries for new configuration modifications.

When calling the API, the library adheres to AWS AppConfig's best practices. It mentions a client ID and the configuration document's most recent receipt version. AWS AppConfig can deliver the configuration in intervals in accordance with the deployment strategy being used, thanks to the client ID.

The API can rapidly alert you whether you already have the right configuration version, thanks to the last-received version of the configuration. If this value is omitted from the API request, AWS AppConfig responds with the whole configuration file, which results in an extra fee.

A client sending a request with an Assuming-Modified-Since header and obtaining a 304 Not Modified response code is analogous to this (if you are familiar with HTTP requests and caching).

The code creates a handler for your HTTP API's /process-string path, which accepts an input parameter to operate with. The AppConfigHelper instance's update config() function is the first thing the handler calls. The library makes a call to the AWS AppConfig API to see whether any new configurations have been added if it has been more than 15 seconds since the previous check.

The handler then examines the values of the two feature flags in the received configuration (which was made available by the library as the config property) and modifies the input text as needed. The JSON file was automatically processed by the helper library. Finally, the result is returned to FastAPI and unicorn so that it can be transmitted to the requestor.

Test the Application

Start the application now and test it to check if it reverses the input string but does not capitalize it in accordance with the feature flags you set.

  1. Make sure your terminal environment is set up to use the same AWS Region where the resources were generated and that it has the necessary credentials. Make sure the virtual environment is turned on in this terminal.
  2. Start the web server:
  1. Open your browser and type http://localhost:8000/process-string/hello%20world or use curl, httpie, postman, or another protocol.
  2. The result "dlrow olleh" should appear in a JSON response that you should receive.

Your program has the configuration and is inverting the input as instructed, but it is not changing it to all capitalised characters.

Deploy a Configuration Update that Enables all-caps Mode

  1. Go to the AWS Systems Manager dashboard and select AWS AppConfig to edit the parameters.
  2. Select the DemoApp program.
  3. The Configuration profiles tab should be selected.
  4. Select the main (primary) profile.
  5. Select Create from the list of Hosted configuration versions.
  6. The current configuration's content is displayed on the Create hosted configuration version page. Set transform allcaps to true rather than false, then click Create hosted configuration version.
  7. Select Start deployment when the home page reloads.
  8. Select the prod environment, the new configuration version (2), and the AppConfig on the Start deployment screen. Rapid (all-at-once) deployment strategy.
  9. Before clicking Start deployment, wait for the state to change from Deploying to Complete.

Now, the output value must be capitalized and in reverse. Before making the adjustment again, wait 15 seconds to ensure the library checks for a new configuration version.

Cleaning Up

Although there are no fees associated with storing AWS AppConfig resources, if you won't be using them again, you may want to wipe away the resources.

  1. Select AWS AppConfig from the AWS Systems Manager console, then go to the DemoApp application.
  2. Select Delete next to Version, and then select Delete once more to confirm.
  3. Up until there are no hosted configuration versions, repeat step 2.
  4. Select the DemoApp link from the page's breadcrumb at the top.
  5. Select the prod environment, then select Delete and Delete again to confirm.
  6. Select the primary profile, click Delete, and then click Delete again to confirm on the Configuration profiles tab.
  7. Select Delete application, and then select Delete again to ensure deletion.

Conclusion

Using the AWS AppConfig helper module, we discovered how to combine AWS AppConfig with a Python application.

Some other features, like what we call syntactic configuration and semantic configuration validation, deployment rate controls, monitoring your application, and rolling back in the event of errors, along with dynamically deploying configuration updates to your applications, can be verified by using the codes given above.






Latest Courses