无服务器应用的持续交付实现

2020-02-23 258浏览

  • 1. 无服务器应用的持续交付实现 AWS解决方案架构师 陈洪正 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 2. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 3. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 4. 无服务器应用的持续交付 实现 AWS解决方案架构师 陈洪正 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 5. 主要议题 为何要讨论这个问题? 部署应用 构建与测试 创建你的管道 DEMO 一些总结 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 6. 为何要讨论这 个问题? © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. https://secure.flickr.com/photos/mgifford/4525333972
  • 7. 无服务器应用 事件源(Event Source) 数据状态 改变 节点请求 资源状态改 变 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 函数(Function) Node Python Java C# Golang 服务(任何目标)
  • 8. 可以触发Lambda的事件源 数据存储 Amazon S3 Amazon DynamoDB Amazon Kinesis 访问节点 Amazon API Gateway Amazon Cognito 开发和管理工具 AWS CloudFormation AWS CloudTrail AWS CodeCommit AWS Step Functions Amazon Alexa 事件/消息服务 Amazon SES Amazon CloudWatch …还有更多! © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS IoT Amazon SNS Cron events
  • 9. 无服务器应用开发 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 10. 构建一个无服务器的部署包 Node.js & Python • 打包正包含代码和所 有依赖的zip包 • 使用npm/pip来安装依 赖库 • 所有依赖包需要在zip 包的根目录内 Java C# (.NET Core) Go • • • Zip包需要包含Go的二 进制文件和所有依赖 • 使用go get来安装依赖 将所有源码和依赖打 包为一个zip包,或 者一个单独的jar包 • • 使用Maven/Eclipse IDE插件 • • 将编译好的类核资源 文件部署在根目录下, 需要的jar包部署在 /lib目录下 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 将所有源码和依赖打 包为一个zip包,或 者一个单独的dll文件 使用 NuGet/VisualStudio的 插件 所有程序集dll都需要 在根目录下
  • 11. CloudFormation模版 AWSTemplateFormatVersion: '2010-09-09' - arn:aws:iam::aws:policy/AmazonDynamoDBReadOnlyAccess Resources: - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole GetHtmlFunctionGetHtmlPermissionProd: AssumeRolePolicyDocument: Type: AWS::Lambda::Permission Version: '2012-10-17' Properties: Statement: Action: lambda:invokeFunction - Action: Principal: apigateway.amazonaws.com - sts:AssumeRole FunctionName: Effect: Allow Ref: GetHtmlFunction Principal: SourceArn: Fn::Sub: arn:aws:execute- api:${AWS::Region}:${AWS::AccountId}:${ServerlessRestApi}/Prod/ANY/* ServerlessRestApiProdStage: Type: AWS::ApiGateway::Stage Properties: DeploymentId: Ref: ServerlessRestApiDeployment RestApiId: Ref: ServerlessRestApi StageName: Prod ListTable: Service: - lambda.amazonaws.com ServerlessRestApiDeployment: Type: AWS::ApiGateway::Deployment Properties: RestApiId: Ref: ServerlessRestApi Description: 'RestApi deployment id: 127e3fb91142ab1ddc5f5446adb094442581a90d' StageName: Stage GetHtmlFunctionGetHtmlPermissionTest: Type: AWS::Lambda::Permission Properties: Type: AWS::DynamoDB::Table Action: lambda:invokeFunction Properties: Principal: apigateway.amazonaws.com ProvisionedThroughput: WriteCapacityUnits: 5 ReadCapacityUnits: 5 AttributeDefinitions: - AttributeName: id AttributeType: S FunctionName: Ref: GetHtmlFunction SourceArn: Fn::Sub: arn:aws:execute- api:${AWS::Region}:${AWS::AccountId}:${ServerlessRestApi}/*/ANY/* ServerlessRestApi: KeySchema: Type: AWS::ApiGateway::RestApi - KeyType: HASH Properties: AttributeName: id GetHtmlFunction: © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Type: AWS::Lambda::Function Properties: Body: info: version: '1.0' title:
  • 12. CloudFormation模版 AWSTemplateFormatVersion: '2010-09-09' - arn:aws:iam::aws:policy/AmazonDynamoDBReadOnlyAccess Resources: - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole GetHtmlFunctionGetHtmlPermissionProd: AssumeRolePolicyDocument: Type: AWS::Lambda::Permission Version: '2012-10-17' Properties: Statement: Action: lambda:invokeFunction - Action: Principal: apigateway.amazonaws.com - sts:AssumeRole FunctionName: Effect: Allow Ref: GetHtmlFunction Principal: SourceArn: Fn::Sub: arn:aws:execute- api:${AWS::Region}:${AWS::AccountId}:${ServerlessRestApi}/Prod/ANY/* ServerlessRestApiProdStage: Type: AWS::ApiGateway::Stage Properties: DeploymentId: Ref: ServerlessRestApiDeployment RestApiId: Ref: ServerlessRestApi StageName: Prod ListTable: Service: - lambda.amazonaws.com ServerlessRestApiDeployment: Type: AWS::ApiGateway::Deployment Properties: RestApiId: Ref: ServerlessRestApi Description: 'RestApi deployment id: 127e3fb91142ab1ddc5f5446adb094442581a90d' StageName: Stage GetHtmlFunctionGetHtmlPermissionTest: Type: AWS::Lambda::Permission Properties: Type: AWS::DynamoDB::Table Action: lambda:invokeFunction Properties: Principal: apigateway.amazonaws.com ProvisionedThroughput: WriteCapacityUnits: 5 ReadCapacityUnits: 5 AttributeDefinitions: - AttributeName: id AttributeType: S FunctionName: Ref: GetHtmlFunction SourceArn: Fn::Sub: arn:aws:execute- api:${AWS::Region}:${AWS::AccountId}:${ServerlessRestApi}/*/ANY/* ServerlessRestApi: KeySchema: Type: AWS::ApiGateway::RestApi - KeyType: HASH Properties: AttributeName: id GetHtmlFunction: © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Type: AWS::Lambda::Function Properties: Body: info: version: '1.0' title:
  • 13. 理解何为“CI/CD” Source • • 提交源代码, 比如.java 互相做代码 检查 Build • 原码编译 • 单元测试 • 代码格式检 查 • 创建可以部 署的文件包 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Test • • • • 集成测试 性能测试 UI测试 渗透测试 Production • 部署到生产 环境
  • 14. 理解何为“CI/CD” Source Build Continuous integration(持续集成) Continuous delivery(持续交付) Continuous deployment(持续部署) © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Test Production
  • 15. 开发管道的几个目标(Pipeline goals) 1. 安全地部署而不影响生产业务 2. 用一些方法来测试和验证代码: • 语法检查,单元测试(unit testing)等 • 集成和相关性测试(integration and dependency testing) • 全应用检查(entire application stack) 3. 支持多环境,包括: • 开发测试环境, 预览环境, 生产环境等 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 部署工具(Deployment Tools) 测试工具 (Testing Tools) 管道工具 (Pipelining Tools)
  • 16. 无服务器应用的开发 框架 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. https://secure.flickr.com/photos/lox/9408028555
  • 17. 无服务器开发框架 Chalice © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 18. Meet SAM! © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 19. AWS Serverless Application Model (SAM) 专为Serverless应用而简化的部署模型 模版语言驱动(template driven) 支持serverless资源类型,包括Function, APIs and tables 支持所有CloudFormation支持的资源和语法 pip install aws-sam-cli,完全开源(Apache 2.0) https://github.com/awslabs/serverless-application-model © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 20. SAM 模版功能 • 可以将其它 非SAM 的Cloudformation原生资源 混合为一个模版,例如:Step Functions, ElastiCache, CloudWatch Alarm • 支持parameters, mappings, Outputs等 • 支持intrinsic functions • 支持ImportValue (除了RestApiId, Policies, StageName attributes) • YAML or JSON © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 21. SAM 模版文件 AWSTemplateFormatVersion: '2010-09-09’ Transform: AWS::Serverless-2016-10-31 Resources: GetHtmlFunction: Type: AWS::Serverless::Function Properties: CodeUri: s3://sam-demo-bucket/todo_list.zip Handler: index.gethtml Runtime: nodejs4.3 Policies: AmazonDynamoDBReadOnlyAccess Events: GetHtml: Type: Api Properties: Path: /{proxy+} Method: ANY ListTable: Type: AWS::Serverless::SimpleTable © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 22. SAM 模版文件 AWSTemplateFormatVersion: '2010-09-09’ Transform: AWS::Serverless-2016-10-31 Resources: GetHtmlFunction: Type: AWS::Serverless::Function Properties: CodeUri: s3://sam-demo-bucket/todo_list.zip Handler: index.gethtml Runtime: nodejs4.3 Policies: AmazonDynamoDBReadOnlyAccess Events: GetHtml: Type: Api Properties: Path: /{proxy+} Method: ANY ListTable: Type: AWS::Serverless::SimpleTable © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 告诉CloudFormation这是一个SAM 模版,需要“transform”为原生模版 创建Lambda Function并作相关配 置 同时创建了API Gateway并且自动 配置好所有设定 创建DynamoDB Table
  • 23. 使用CloudFormation原生模版 APIG4XXErrorAlarm: Type: "AWS::CloudWatch::Alarm" Properties: ActionsEnabled: true AlarmDescription: "APIG too many 4xx errors" AlarmName: 'Fn::Sub': 'sam-apidemo-${Env} too many 4xx errors' ComparisonOperator: GreaterThanThreshold Dimensions: - Name: 'ApiName' Value: 'Fn::Sub': '${AWS::StackName}' - Name: 'Stage' Value: 'Prod' EvaluationPeriods: "1" MetricName: 4XXError Namespace: AWS/ApiGateway Statistic: "Sum" Threshold: "1" TreatMissingData: "notBreaching" Unit: "Count" © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 对SAM没有特殊支持的资源, 可直接应用CloudFormation的模 版。 https://docs.aws.amazon.com/zh_cn/AWSCloud Formation/latest/UserGuide/template- reference.html
  • 24. SAM 从模版生成完整堆栈 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 25. AWS SAM Local 用于本地调试无服务器应用的CLI工具 可直接模拟调用Lambda函数以及在代理 集成模式下的API网关服务 返回消息和日志均在本地机器可见 使用开源的docker-lambda来最小化 Lambda的运行时环境 • 可模拟超时,内存限制,运行时环境 等 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. https://github.com/awslabs/aws-sam-local
  • 26. SAM本地调试Lambda © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 27. SAM 的本地调试功能 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 28. SAM本地模拟API Gateway © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 29. SAM 两个重要的命令 – Package & Deploy Package • 创建部署包(.zip file) • 自动上传部署包到指定的S3 bucket临时存放 • 返回新的SAM YAML并且改写CodeUri属性指向这个S3 URI (以方便接下来通过CloudFormation部署) Deploy • 调用 CloudFormation ‘CreateChangeSet’ API创建ChangeSet • 调用 CloudFormation ‘ExecuteChangeSet’ API创建或更新 Serverless环境 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 30. SAM 命令的应用 #!/bin/bash REPLACE_THIS_WITH_YOUR_S3_BUCKET_NAME="hzchen-private-ore" REGION="us-west-2" STACK="sam-apidemo-prod" pip install -r requirements.txt -t apidemo/build/ && \ cp apidemo/*.py apidemo/build/ && \ sam validate && \ sam package --template-file template.yaml --output-template-file packaged.yaml --s3-bucket $REPLACE_THIS_WITH_YOUR_S3_BUCKET_NAME --region $REGION && \ sam deploy --template-file packaged.yaml --stack-name $STACK --capabilities CAPABILITY_IAM --parameter-overrides Env=staging DeploymentPreference=AllAtOnce --region $REGION && \ aws cloudformation describe-stacks --stack-name $STACK --query 'Stacks[].Outputs' --region $REGION © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 31. 部署应用 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. https://secure.flickr.com/photos/simononly/15386966677
  • 32. Lambda环境变量 • • • • 可以将动态的键值对传递给你的函数 可以通过标准的环境变量接口访问,比如Node.js的 proces.env,Python的os.environ 可选择使用AWS Key Management Service (KMS)做加密 • 允许你在 IAM中配置哪个角色(roles) 能访问并解密那 个键对应的值 便于控制访问不同阶段和环境 (i.e. dev, testing, production) © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 33. API Gateway的阶段变量 • 阶段变量(Stage Variable)近似于环境变量 • 使用阶段变量来存储配置数据 • 阶段变量可以从$context对象中获取 • 可以从API网关的大部分字段中获取到变量的值 • Lambda函数的资源名称(ARN) • HTTP访问节点 • 自定义授权方(Custom authorizer)函数的名字 • 参数映射 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 34. 阶段变量与Lambda别名 结合API网管的阶段变量与Lambda函数别名来管理多个环境与多个版本 myLambdaFunction 1 2 3 = prod 4 5 6 = beta 7 8 = dev © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. My First API Stage variable = lambdaAlias Prod lambdaAlias = prod Beta lambdaAlias = beta Dev lambdaAlias = dev
  • 35. 基于Lambda别名的流量灰度 myLambdaFunction 1 2 3 = prod 4 5 6 = prod 5% My First API Stage variable = lambdaAlias Prod lambdaAlias = prod aws lambda update-alias --name prod --function-name myLambdaFunction --routing-config AdditionalVersionWeights={”6"=0.05} © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 36. 基于Lambda别名的流量灰度 myLambdaFunction My First API Stage variable = lambdaAlias 5 6 = prod Prod lambdaAlias = prod aws lambda update-alias --name prod --function-name myLambdaFunction --function-version 6 --routing-config '' © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 37. 基于Lambda别名流量切换与AWS Step Functions结合 Blog link: http://amzn.to/2FjlWA7 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 38. SAM 安全部署(Safe Deployment) Globals: Function: Runtime: nodejs4.3 AutoPublishAlias: !Ref ENVIRONMENT MyLambdaFunction: Type: AWS::Serverless::Function Properties: Handler: index.handler DeploymentPreference: Type: Linear10PercentEvery10Minutes Alarms: # A list of alarms that you want to monitor - !Ref AliasErrorMetricGreaterThanZeroAlarm - !Ref LatestVersionErrorMetricGreaterThanZeroAlarm Hooks: # Validation Lambda functions that are run before & after traffic shifting PreTraffic: !Ref PreTrafficLambdaFunction PostTraffic: !Ref PostTrafficLambdaFunction © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. NEW!
  • 39. 基于别名的流量切换与SAM的集成 在SAM模版内: AutoPublishAlias 在使用该字段并赋值一个别名后, AWS SAM 将自动处理以下操作: • 根据提供的Lambda函数的S3 URI,检测 代码是否有更新 • 用最新的代码创建或者发布一个更新的 函数版本 • 创建一个你提供的Lambda别名(除非该 别名已存在)并将该别名指向最新的 Lambda函数版本 Deployment Preference类型 Canary10Percent30Minutes Canary10Percent5Minutes Canary10Percent10Minutes Canary10Percent15Minutes Linear10PercentEvery10Minutes Linear10PercentEvery1Minute Linear10PercentEvery2Minutes Linear10PercentEvery3Minutes AllAtOnce © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 40. 基于告警的迅速回滚 在SAM模版内: Alarms: # 以下每行代表一个相关联的告警 - !Ref AliasErrorMetricGreaterThanZeroAlarm - !Ref LatestVersionErrorMetricGreaterThanZeroAlarm Hooks: # 用于在切换流量前后用于验证的Lambda函数 PreTraffic: !Ref PreTrafficLambdaFunction PostTraffic: !Ref PostTrafficLambdaFunction 注意:您可以关联最多10个告警项 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 41. AWS CodeDeploy 支持Lambda部署 搭配使用AWS SAM来部署Serverless应用 支持Lambda根据别名的流量切换功能(Alias Traffic Shifting),提供金丝雀(Canary)发布或蓝绿部署(蓝/ 绿 部署) 支持基于CloudWatch监控和告警( Metrics/Alarms)做回 滚(rollback) Pre/Post Traffic Triggers与其它服务的集成(甚至调用 Lambda) © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 42. 基于别名的滚动发布 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 43. 构建与测试 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. https://secure.flickr.com/photos/spenceyc/7481166880
  • 44. 创建我们的测试与验证模型 我们需要确保我们的代码: • 不存在代码语法问题 • 满足团队或公司的开发规范 • 能编译成功 • 通过单元测试尽可能完整的覆盖所有代码 我们想要确定我们的Serverless应用: • 能与其它业务模块正常集成 • 对上下游的模块调用能很好的进行错误处理 我们想要确定整个应用与基础架构: • 端到端运作正常 • 严格遵循各种安全最佳实践 • 能够处理各种资源伸缩的动态需求 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 45. 测试工具 Code Inspection/Test Coverage: • Landscape – https://landscape.io/ (only for Python) • CodeClimate – https://codeclimate.com/ • Coveralls.io – https://coveralls.io/ Mocking/stubbing tools: • https://github.com/atlassian/localstack - ”A fully functional local AWS cloud stack. Develop and test your cloud apps offline!” • Includes: • • • https://github.com/spulec/moto - boto mock tool https://github.com/mhart/dynalite - DynamoDB testing tool https://github.com/mhart/kinesalite - Kinesis testing tool API Interface/UI testings: • Runscope – https://www.runscope.com/ - API Monitoring/Testing • Ghost Inspector – https://ghostinspector.com/ - Web interface testing © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 46. AWS CodeBuild 全托管的构建服务,用来编译源代码、运行测试以及打 包部署文件 可以很好的扩容并且同时进行多个构建任务 可以自行提供Docker镜像作自定义的编译环境 可以从SSM Parameter Store获取环境变量 可以运行在VPC内部 支持依赖缓存(Dependency Caching) © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 47. buildspec.yml 模版文件 version: 0.2 phases: install: commands: - echo Install python modules for this build... - pip install pytest aws-sam-cli pre_build: commands: - echo Run unit test... - echo python -m pytest -v ./tests build: commands: - pip install -r requirements.txt -t $APP/build/ - cp $APP/*.py $APP/build/ - sam package --template-file template.yaml --output-template-file packaged.yaml --s3-bucket $S3_BUCKET --region $REGION post_build: commands: - echo Build completed on `date` artifacts: files: packaged.yaml © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 48. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 49. 构建你的管道 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. https://www.flickr.com/photos/seattlemunicipalarchives/12504672623/
  • 50. AWS CodePipeline 为快速可靠的应用迭代需求而设计的持续交付服 务 为软件发布流程进行可视化的建模 每当代码发生变化的时候,自动透过管道进行自 动构建,自动测试与部署 除了AWS本身服务之外,与第三方服务业有很 好的结合 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 51. CodePipeline MyApplication Source Stage Source GitHub Transition Build JenkinsOnEC2 Jenkins NotifyDevelopers Lambda TestAPI Runscope Parallel actions Deploy JavaApp CodeDeploy © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Sequential actions Pipeline
  • 52. 通过CodePipeline交付应用 MyApplication MyBranch-Source 管道工作流程: 1. 2. 3. 提交代码到代码仓库 在CodeBuild里打包与测试 在CodePipeline里调用CloudFormation以SAM templates 来创建或更新堆栈(stacks) 你也可以:使用Cloudformation的ChangeSets (只有ChangeSets产生成功才继续更新堆栈,更为安全) 4. 5. 使用特别定义的stage/environment参数带入Lambda成为 环境变量 在不同的stages/environments之间测试应用 你也可以: 插入manual approvals要求手动同意才可以继续 Source CodeCommit Build test-build-source CodeBuild MyDev-Deploy create-changeset AWS CloudFormation execute-changeset AWS CloudFormation Run-stubs AWS Lambda © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 53. 生产环境的范例 MyApplication Source Source CodeCommit Deploy Staging create-changeset AWS CloudFormation execute-changeset AWS CloudFormation Build test-build-source CodeBuild Run-API-test Runscope QA-Sign-off Manual Approval Review Deploy Testing create-changeset AWS CloudFormation Deploy Prod execute-changeset create-changeset AWS CloudFormation AWS CloudFormation Run-stubs execute-changeset AWS Lambda AWS CloudFormation Post-Deploy-Slack AWS Lambda © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 这个管道: • 具有五个阶段(stages) • 通过CodeBuiid构建代码 • 部署到三个不同的环境 • 使用SAM/CloudFormation来更新与 部署环境 • 使用Lambda自定义function来运行 自己的测试 • 整合第三方工具或服务 • 部署到生产环境之前,引入了手动 允许机制(manual approval),需 要人工接入允许之后才能继续
  • 54. 在哪里做测试? 1. • 代码评审 MyApplication 2. • • • Lint/语法检查 单元测试通过 代码成功编译 4. Deploy Staging Source • • • Build 应用成功部署 与真实的服务做集成测试 (可能无法模拟生产环境中服 务版本之间的依赖) Deploy Testing 3. • • 应用成功部署 Mocked/stubbed 集 成测试 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Deploy Prod • • • 5. 金丝雀部署 等待阶段没有告错 全量部署
  • 55. 通过变量控制发布环境 Parameters: MyEnvironment: Type: String Default: testing AllowedValues: - testing - staging - prod Description: Environment of this stack of resources SpecialFeature1: Type: String Default: false AllowedValues: - true - false Description: Enable new SpecialFeature1 … © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. … #Lambda MyFunction: Type: 'AWS::Serverless::Function' Properties: … Environment: Variables: ENVIRONMENT: !Ref: MyEnvironment Spec_Feature1: !Ref: SpecialFeature1 … #API Gateway MyApiGatewayApi: Type: AWS::Serverless::Api Properties: … Variables: ENVIRONMENT: !Ref: MyEnvironment SPEC_Feature1: !Ref: SpecialFeature1 …
  • 56. 传递参数的方式 通过传入的参数配置文件来获取参数 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 通过参数覆盖(Parameter Overrides)
  • 57. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 58. DEMO © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 59. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 60. 总结 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 61. 一些最佳实践总结 对Lambda与API网关: 1. 对生产环境使用蓝/绿或金丝雀部署,并且让回滚最大限度 的自动化 2. 如果你需要支持多个版本以对多个消费者/调用方服务, Lambda的版本功能(Versioning)是一个很好的选择 3. API网关的阶段(Stages)功能与Lambda的版本(Versioning) 功能类似, 在你需要支持多版本API的时候很有帮助 4. 对开发、测试、预览和生产环境使用分开的堆栈(Stacks), 而不是使用阶段(Stages)或Lambda版本(Versioning)来区 别它们 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 62. 一些最佳实践总结 1. 代码互检(Peer review):大部分CI/CD流程的第一步 2. 持续集成(Continous Integration):最低要求! 3. 持续交付(Continous Delivery):基本要求,实现从代码提交到预发布环 境的自动化,使用一个闸道(Gate)或人工的审批/任务提交来最终部署 到生产环境 4. 多环境部署:对无服务器应用来说实现起来非常容易且低成本 5. 包含五个基础阶段的管道:Source,Build,Test,Pre-Production, Production © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 63. 完整流程 AWS CodeCommit Jenkins bucket Code Developers Git Repository AWS CodePipeline AWS CodeBuild AWS CloudFormation Amazon CloudWatch https://github.com/totorochina/sam-cicd-demo © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS Lambda Amazon API Gateway Amazon DynamoDB
  • 64. SAM文档 Serverless Application Model (SAM) - https://github.com/awslabs/serverless-application-model SAM Local - https://github.com/awslabs/aws-sam-local SAM Documents - https://awslabs.github.io/serverless-application- model/index.html Safe Deployment - https://awslabs.github.io/serverless-application- model/safe_lambda_deployments.html © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 65. aws.amazon.com/serverless © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 66. 其它服务资源 AWS Serverless: https://aws.amazon.com/serverless Serverless tools: https://aws.amazon.com/serverless/developer-tools AWS Lambda: https://aws.amazon.com/lambda API Gateway: https://aws.amazon.com/api-gateway CloudFormation: https://aws.amazon.com/cloudformation CodeDeploy: https://aws.amazon.com/codedeploy CodeCommit: https://aws.amazon.com/codecommit © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 67. ? © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. https://secure.flickr.com/photos/dullhunk/202872717/
  • 68. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 69. AWS Deck Template © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 70. Deck Guidelines Fonts, sizes, colors, and layouts are all pre-built in this template. Color palette Please do not use shadows or outlines on shape elements. Limit color use for chart graphics to grayscale plus one accent color. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 71. Helpful Resources AWS Logo (logos for both web and print) AWS Simple Icons (product and simple icons for architectural diagrams) Design Request (AWS Marketing Design wiki) Deck Asset Repository (up-to-date deck assets and templates) © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 72. Copy & Paste Content When pasting content from another presentation please paste using “Destination Theme.” Note: This works when copying entire slides from other presentations as long as the source presentation is also 16:9 WINDOWS © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. MAC
  • 73. Copy & Paste Code When pasting content Code into a Code template please use the “Keep Text Only Function” for Windows and “Use Destination Theme” for Mac WINDOWS © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. MAC
  • 74. Assets Usage © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 75. Re-sizing Assets Always hold down shift key and drag from corner when scaling assets with Shift © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. without Shift
  • 76. TEMPLATE © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 77. This is an illustrated title slide © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 78. This is a simple title slide PRESENTER | DATE © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 79. Title + Content © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 80. Alt title + content 1 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 81. Alt title + content 2 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 82. Code Snippet © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 83. Section Title © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 84. Two columns © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 85. Comparison © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 86. Three column © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 87. Four column with graphics © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 88. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 89. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 90. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 91. Six section with graphics Image caption 1 Image caption 2 Image caption 3 Image caption 4 Image caption 5 Image caption 6 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.