aws appconfig 理解和使用appconfig对应用程序进行动态配置
创始人
2024-05-29 23:20:59
0

参考资料

  • Automating Feature Release using AWS AppConfig Integration with AWS Codepipeline
  • Deploying application configuration to serverless: Introducing the AWS AppConfig Lambda extension
  • Create a pipeline that uses Amazon AppConfig as a deployment provider

appconfig的基本概念

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 托管配置,ssm 参数,github和codecommit仓库等
  • 使用自带的json校验,或者通过lambda函数定义配置校验规则
  • 控制配置的部署速度、部署时间和bake时间。
  • 监控配置过程,失败回滚

appconfig的关键概念

  • application,组织和管理配置数据的单位
  • envionment,环境是appconfig的逻辑部署组,application可以有多个环境
  • configuration profile,使得appconfig能够访问配置数据(可以附加校验器),可以设置的配置数据包括
    • yaml和json数据
    • s3对象
    • codepipeline管道
    • secret manager
    • parameter store
    • ssm documents

应用程序检查和获取配置数据的流程

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取代

在这里插入图片描述

appconfig入门使用

应用和环境

创建application,应用程序是一个管理和部署配置文件的单元

  • 应用程序可以关联插件,包括eventbridge,sqs和sns
aws appconfig create-application --name my-app

创建environment,环境是application的逻辑部署组

  • 环境可以配置监控,需要设置cw alarm和对应的iam角色
  • 环境可以关联插件,包括eventbridge,sqs和sns
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

  • 配置数据源
  • 访问配置数据的iam角色
  • 数据校验程序(json/yaml的静态校验,或lambda函数校验)

在这里插入图片描述

关于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在调用StartDeploymentValidateConfigurationActivity时验证lambda,需要为appconfig.amazonaws.com授权调用该lambda函数

配置文件可以创建两种类型

  • faeature flag,Create, manage and safely deploy a single or a group of feature flags. Feature Flags help you release features quickly and safely.
  • freeform configuration,Create your own configurations and store them within Amazon AppConfig or reference existing data in S3, Parameter Store, or Secrets Manager.

feature flag

创建功能标志(feature flag),主要用于启用或禁用应用程序中的功能。注意

  • 类型为,AWS.AppConfig.FeatureFlags
  • 位置uri为,hosted
aws appconfig create-configuration-profile \--application-id ml6665u \--name my-config-profile \--location-uri hosted \--type AWS.AppConfig.FeatureFlags

简单创建一个功能标志

在这里插入图片描述

freeform configuration

对于freeform的配置文件,可以选择更多的数据源

在这里插入图片描述

部署策略

创建部署策略,即部署速度、部署时间和bake时间等

https://docs.aws.amazon.com/zh_cn/appconfig/latest/userguide/appconfig-creating-deployment-strategy.html

  • 部署类型,支持线性指数部署类型
  • 步骤百分比,在部署的每个步骤中作为目标的调用
  • 部署时间,appconfig部署到主机的花费时间
  • Bake time,部署100%后,在完成之前对cw告警监控的时间,如果告警则回滚

类似codedeploy的部署策略,有三个预定义策略

  • AppConfig.AllAtOnce
  • AppConfig.Linear50PercentEvery30Seconds
  • AppConfig.Canary10Percent20Minutes

部署配置

实际就是将环境和配置文件绑定并发布

在这里插入图片描述

检索配置

将配置文件发布到环境中,就能够通过对应的接口检索到配置数据了

应用程序检索配置文件的逻辑参考上文的描述

  • 获取客户端会话

    $ 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集成

将appconfig和eks集成非常简单

  • appconfig作为一个sidecar从appconfig服务轮询并获取配置文件
  • application从appconfig-agent检索配置数据

创建以下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

相关内容

热门资讯

描写秋天的优美句子 描写秋天的优美句子  我喜欢秋天的残荷,没有了夏日浓烈的色彩,美艳的花骨朵儿早不见了踪影,残荷里只剩...
摘抄伤感的句子 摘抄伤感的句子  导语:运动使人充满生机活力,音乐使人充满浪漫快乐,思考使人充满智慧理智。以下是小编...
七夕的优美句子 关于七夕的优美句子  在学习、工作或生活中,许多人对一些广为流传的句子都不陌生吧,不同类型的句子具有...
孤独的影子伤感句子 孤独的影子伤感句子  1、与寂寞有染,和幸福无缘…  2、工作,退一步海阔天空;爱情,退一步人去楼空...
致自己的励志句子 致自己的励志句子(精选70句)  在日常的学习、工作、生活中,大家都对那些朗朗上口的句子很是熟悉吧,...
最新形容女子如水的句子 最新形容女子如水的句子  在日常学习、工作抑或是生活中,大家都收藏过令自己印象深刻的句子吧,从句法角...
人生感悟的语句摘抄 有关人生感悟的语句摘抄  对于人生,你有什么特别感悟呢?用一些经典的句子来表达吧。现在请欣赏小编带来...
干净三观正励志句子 干净三观正励志句子(精选420句)  在日常的学习、工作、生活中,许多人对一些广为流传的句子都不陌生...
小学班级规章制度细则   规章制度是指用人单位制定的组织劳动过程和进行劳动管理的规则和制度的总和。小编整理了班级规章制度细...
关于国庆节发朋友圈的说说 关于国庆节发朋友圈的说说  国庆节送欢乐,我在远方祝福你,生活美滋滋,秋收黄金果,快乐一箩筐,带着爱...
幼儿园教育笔记中班 幼儿园教育笔记中班  幼儿园教育笔记中班(一)  一、教师幸福感的来源1.源于幼儿真诚的爱与关心的师...
表达对老师感激之情的句子 表达对老师感激之情的句子(精选100句)  教诲如春风,师恩似海深。桃李满天下,春晖遍四方。下面是小...
逛街的日记 逛街的日记(15篇)  时间如快马般匆匆,一天又过去了,我们对人和事情也有了新的看法,让我们今天做个...
积极努力的正能量句子 积极努力的正能量句子大全  在平平淡淡的日常中,说到句子,大家肯定都不陌生吧,不同的句子在语言环境中...
军训日记 精选军训日记合集6篇军训日记 篇1  今天,我久久不能平静,经过了四天的艰苦训练,我们满载希望的果实...
乌龟观察日记 乌龟观察日记5篇  【导语】乌龟隶属于龟科、乌龟属的一种。有时特指乌龟别称金龟、草龟、泥龟和山龟等。...
观察小鹦鹉的日记 观察小鹦鹉的日记三篇  【导语】鹦鹉是鹦形目,众多羽毛艳丽、爱叫的鸟。典型的攀禽,对趾型足,两趾向前...
我的心情日记300字_心情日... 我的心情日记300字_心情日记  一天即将完结,相信你有很多感悟吧,是时候静下心来好好写写日记了。如...
《神秘的宇宙》读书笔记 《神秘的宇宙》读书笔记范文  当赏读完一本名著后,想必你有不少可以分享的东西,不能光会读哦,写一篇读...
写愚人节的玩笑小学日记 写愚人节的玩笑小学日记  愚人节的早晨,空气清新,阳光柔和,大地上的万物呈现出一派生机勃勃的景象。 ...