部署配置文件 - Amazon AppConfig
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

部署配置文件

以下每个示例都包含有关代码所执行操作的注释。本节中的示例调用以下 API:

Java
private void createDeployment() throws InterruptedException { AppConfigClient appconfig = AppConfigClient.create(); // Create an application CreateApplicationResponse app = appconfig.createApplication(req -> req.name("MyDemoApp")); // Create a hosted, freeform configuration profile CreateConfigurationProfileResponse configProfile = appconfig.createConfigurationProfile(req -> req .applicationId(app.id()) .name("MyConfigProfile") .locationUri("hosted") .type("AWS.Freeform")); // Create a hosted configuration version CreateHostedConfigurationVersionResponse hcv = appconfig.createHostedConfigurationVersion(req -> req .applicationId(app.id()) .configurationProfileId(configProfile.id()) .contentType("text/plain; charset=utf-8") .content(SdkBytes.fromUtf8String("my config data"))); // Create an environment CreateEnvironmentResponse env = appconfig.createEnvironment(req -> req .applicationId(app.id()) .name("Beta") // If you have CloudWatch alarms that monitor the health of your service, you can add them here and they // will trigger a rollback if they fire during an appconfig deployment //.monitors(Monitor.builder().alarmArn("arn:aws:cloudwatch:us-east-1:520900602629:alarm:MyAlarm") // .alarmRoleArn("arn:aws:iam::520900602629:role/MyAppConfigAlarmRole").build()) ); // Start a deployment StartDeploymentResponse deploymentResponse = appconfig.startDeployment(req -> req .applicationId(app.id()) .configurationProfileId(configProfile.id()) .environmentId(env.id()) .configurationVersion(hcv.versionNumber().toString()) .deploymentStrategyId("AppConfig.Linear50PercentEvery30Seconds") ); // Wait for deployment to complete List<DeploymentState> nonFinalDeploymentStates = Arrays.asList( DeploymentState.DEPLOYING, DeploymentState.BAKING, DeploymentState.ROLLING_BACK, DeploymentState.VALIDATING); GetDeploymentRequest getDeploymentRequest = GetDeploymentRequest.builder().applicationId(app.id()) .environmentId(env.id()) .deploymentNumber(deploymentResponse.deploymentNumber()).build(); GetDeploymentResponse deployment = appconfig.getDeployment(getDeploymentRequest); while (nonFinalDeploymentStates.contains(deployment.state())) { System.out.println("Waiting for deployment to complete: " + deployment); Thread.sleep(1000L); deployment = appconfig.getDeployment(getDeploymentRequest); } System.out.println("Deployment complete: " + deployment); }
Python
import boto3 appconfig = boto3.client('appconfig') # create an application application = appconfig.create_application(Name='MyDemoApp') # create an environment environment = appconfig.create_environment( ApplicationId=application['Id'], Name='MyEnvironment') # create a configuration profile config_profile = appconfig.create_configuration_profile( ApplicationId=application['Id'], Name='MyConfigProfile', LocationUri='hosted', Type='AWS.Freeform') # create a hosted configuration version hcv = appconfig.create_hosted_configuration_version( ApplicationId=application['Id'], ConfigurationProfileId=config_profile['Id'], Content=b'my config data', ContentType='text/plain') # start a deployment deployment = appconfig.start_deployment( ApplicationId=application['Id'], EnvironmentId=environment['Id'], ConfigurationProfileId=config_profile['Id'], ConfigurationVersion=str(hcv['VersionNumber']), DeploymentStrategyId='AppConfig.Linear20PercentEvery6Minutes')
JavaScript
import { AppConfigClient, CreateApplicationCommand, CreateEnvironmentCommand, CreateConfigurationProfileCommand, CreateHostedConfigurationVersionCommand, StartDeploymentCommand, } from "@aws-sdk/client-appconfig"; const appconfig = new AppConfigClient(); // create an application const application = await appconfig.send( new CreateApplicationCommand({ Name: "MyDemoApp" }) ); // create an environment const environment = await appconfig.send( new CreateEnvironmentCommand({ ApplicationId: application.Id, Name: "MyEnvironment", }) ); // create a configuration profile const config_profile = await appconfig.send( new CreateConfigurationProfileCommand({ ApplicationId: application.Id, Name: "MyConfigProfile", LocationUri: "hosted", Type: "AWS.Freeform", }) ); // create a hosted configuration version const hcv = await appconfig.send( new CreateHostedConfigurationVersionCommand({ ApplicationId: application.Id, ConfigurationProfileId: config_profile.Id, Content: "my config data", ContentType: "text/plain", }) ); // start a deployment await appconfig.send( new StartDeploymentCommand({ ApplicationId: application.Id, EnvironmentId: environment.Id, ConfigurationProfileId: config_profile.Id, ConfigurationVersion: hcv.VersionNumber.toString(), DeploymentStrategyId: "AppConfig.Linear20PercentEvery6Minutes", }) );