参考资料
appconfig主要的功能是创建、管理以及快速部署应用程序配置。可以集成的服务包括ec2,lambda,ecs,eks。iot等。
来自chatgpt的解释
AWS AppConfig is a service provided by Amazon Web Services (AWS) that allows you to manage and deploy application configurations across different environments. It helps you deploy configurations and feature flags for your applications in a controlled manner, with the ability to monitor and roll back changes easily.
使用appconfig的优势如下
appconfig的关键概念
应用程序检查和获取配置数据的流程
https://docs.aws.amazon.com/appconfig/latest/userguide/what-is-appconfig.html#learn-more-appconfig-how-it-works
appconfig的api在2021-11-18进行了迁移,GetConfiguration的api调用被GetLatestConfiguration取代
创建application,应用程序是一个管理和部署配置文件的单元
aws appconfig create-application --name my-app
创建environment,环境是application的逻辑部署组
aws appconfig create-environment \--application-id ml6665u \--name my-testenv \--description "my test appconfig env"#--monitors "AlarmArn=ARN_of_the_Amazon_CloudWatch_alarm,AlarmArnRole=ARN_of_the_IAM role_for_AWS AppConfig_to_monitor_AlarmArn" \#--tags User_defined_key_value_pair_metadata_of_the_environment
创建配置文件(configuration profile),包含具体的配置数据,需要指定以下信息
https://docs.aws.amazon.com/zh_cn/appconfig/latest/userguide/appconfig-creating-configuration-and-profile.html
关于lambda函数校验
appconfig调用lambda函数校验的event示例如下
{"ApplicationId": "The application Id of the configuration profile being validated", "ConfigurationProfileId": "The configuration profile Id of the configuration profile being validated","ConfigurationVersion": "The configuration version of the configuration profile being validated","Content": "Base64EncodedByteString", "Uri": "The uri of the configuration"
}
appconfig在调用StartDeployment
和ValidateConfigurationActivity
时验证lambda,需要为appconfig.amazonaws.com
授权调用该lambda函数
配置文件可以创建两种类型
创建功能标志(feature flag),主要用于启用或禁用应用程序中的功能。注意
aws appconfig create-configuration-profile \--application-id ml6665u \--name my-config-profile \--location-uri hosted \--type AWS.AppConfig.FeatureFlags
简单创建一个功能标志
对于freeform的配置文件,可以选择更多的数据源
创建部署策略,即部署速度、部署时间和bake时间等
https://docs.aws.amazon.com/zh_cn/appconfig/latest/userguide/appconfig-creating-deployment-strategy.html
类似codedeploy的部署策略,有三个预定义策略
实际就是将环境和配置文件绑定并发布
将配置文件发布到环境中,就能够通过对应的接口检索到配置数据了
应用程序检索配置文件的逻辑参考上文的描述
获取客户端会话
$ aws appconfigdata start-configuration-session \--application-identifier ml6665u \--environment-identifier ez41c69 \--configuration-profile-identifier my-config-profile
{"InitialConfigurationToken": "AYADeKP07JoESGyhSikVkg/JFc8AXwABABVhd3MtY3J5cHRvLXB1YmxpYy1rZXkAREFraGZmN3JKU2FHa0x4RnZXQ1hiV2xieU1KSzRhelEvR0FUOU5TaG9oUUkwNEtDbUx3TmZ0azZSL1ZKQTZyckxCZz09AAEAB2F3cy1rbXMAT2Fybjphd3MtY246a21zOmNuLW5vcnRoLTE6NDYzNDg3MTE3ODUzOmtleS9kZjQ0NjJjYi04ZTRkLTQ0ZjUtODQ0Ni1lMmY5NzgwMDdmZGEA9gEBA/83mxxxTS7TErRNGSXrJSRHsPbaRH1jcwJ+13883JraBviC0heW1uaOUwm3icxYUcCMHtohdSrBx990aU/etwA4vQVIBsmFQkXyL4ZC6KPis7Gx/Ssi89aH25FKZr2b3gxrg=="
}
请求配置,并得到了NextPollConfigurationToken
$ export token=AYADeKP07JoxA4vQVIBsmFQkXyL4ZC6KPis7Gx/Ssi89aH25FKZr2b3gxrg==
$ aws appconfigdata get-latest-configuration \--configuration-token $token mydata.json
{"NextPollConfigurationToken": "AYADeJNIG08gqrex7XXHbUValqIAXwxtY/a0auK9QWr+EAjEA/a2Lp8/rMhdaMGgcrwI4JyBAGM1C9525wkkjJbFrmHSpZw6T+dxkwalgOa3gi4Xp","NextPollIntervalInSeconds": "60","ContentType": "application/json"
}
$ cat mydata.json
{
"action":{"action":"eat","age":26,"enabled":true,"gender":"male","married":true,"name":"zhangsan"}
}
后续调用必须提供NextPollConfigurationToken
将appconfig和eks集成非常简单
创建以下deployment
kind: Pod
apiVersion: v1
metadata:name: test-appconfiglabels:app: test-appconfig
spec:#serviceAccountName: eks-appconfigcontainers:- name: amazonlinuximage: xxxxxxx.dkr.ecr.cn-north-1.amazonaws.com.cn/amazonlinux:latestcommand: ["/bin/sh","-c","sleep 3600"]- name: appconfig-agentimage: public.ecr.aws/aws-appconfig/aws-appconfig-agent:2.xports:- name: httpcontainerPort: 2772protocol: TCPenv:- name: SERVICE_REGIONvalue: cn-north-1
默认情况下,appconfig代理在端口 2772 上运行
在amazonlinux中访问appconfig
$ curl "http://localhost:2772/applications/my-app/environments/my-config-profile/configurations/my-config-profile"
curl: (3) URL using bad/illegal format or missing URL
curl: (97) Could not resolve host: curl
{"Message":"User: arn:aws-cn:sts::xxxxxxx:assumed-role/eksctl-test124-nodegroup-test124-NodeInstanceRole-XTF9X5WL2NMR/i-0a661ae8d299361
96 is not authorized to perform: appconfig:StartConfigurationSession on resource: arn:aws-cn:appconfig:cn-north-1:xxxxxxx:application/m
l6665u/environment/my-config-profile/configuration/thtqvv5 because no identity-based policy allows the appconfig:StartConfigurationSession a
ction"}
创建策略如下
{"Version": "2012-10-17","Statement": [{"Sid": "myappconfig","Effect": "Allow","Action": "appconfig:*","Resource": "*"}]
}
我们需要给appconfig-agent配置appconfig的权限
eksctl create iamserviceaccount \--cluster test124 \--name eks-appconfig \--namespace default \--attach-policy-arn arn:aws-cn:iam::xxxxxxx:policy/MyAppconfigFullAccess \--override-existing-serviceaccounts \--region cn-north-1 \--approve
之后取消pod中的sa注释,重新创建pod
再次访问成功拿到配置数据
bash-5.2# curl "http://localhost:2772/applications/my-app/environments/my-config-profile/configurations/my-config-profile"
{"Message":"Environment not found","ReferencedBy":{"EnvironmentIdentifier":"my-config-profile"},"ResourceType":"Environment"}
此外,appconfig代理的环境变量配置
https://docs.aws.amazon.com/zh_cn/appconfig/latest/userguide/appconfig-integration-containers-agent.html#appconfig-integration-containers-agent-configuring
上一篇:MySQL日志文件