【问题标题】:Automating Deployment of Lambda-based Applications自动部署基于 Lambda 的应用程序
【发布时间】:2017-06-26 20:07:21
【问题描述】:

我检查了下面链接中的教程并尝试了。 http://docs.aws.amazon.com/lambda/latest/dg/automating-deployment.html

它对我有用,但如何部署与该 lambda 相关的环境变量和配置更改。从教程中我可以了解如何部署代码更改,但我不确定如何部署配置更改。

【问题讨论】:

    标签: amazon-web-services deployment aws-lambda aws-codepipeline


    【解决方案1】:

    为了让 Lambda 拉取配置信息,有多种选择:

    • 对于非常基本的配置,您可以简单地使用可以在 CLI 调用期间根据文档设置的环境变量(rep 阻止我添加两个以上的链接,因此您必须使用谷歌搜索“lambda 环境变量”来获取它)
    • Lambda 可以从SSM Parameter Store 读取数据,尽管这通常用于提取 EC2 实例的配置信息(数据库字符串等)。但是,如果您主要使用无服务器,这是一种方法
    • 您可以使用选择的数据库 (RDS/DynamoDB) 来提取/存储数据。请记住,如果您超过免费层级限制,您将按小时收费。
    • 您可以使用存储在 S3 存储桶中的选择格式(JSON、YAML、CSV 等)并加载 python(这将违背您的调用分配)
    • 对于 Lambda 函数的复杂链接,您可能需要考虑 Step Functions

    至于您如何实现上述自动化,这实际上取决于您现有的自动化是什么。要么使用 CLI 来编排整个事情,要么使用您选择的脚本语言和适当的 AWS 开发工具包。

    【讨论】:

    • 谢谢。我打算使用 YAML 文件来设置配置。是否有任何链接或教程。因为我是 lambda 新手,所以云被卡住了。
    • @NileshPhrate 当您说 YAML 配置时,您是在谈论 CloudFormation YAML 模板吗?另外,您打算将以上哪一项用于您的配置?
    • 是的,CloudFormation YAML 模板。我正在使用 S3 存储桶方法进行配置。
    【解决方案2】:

    我没有手动执行http://docs.aws.amazon.com/lambda/latest/dg/automating-deployment.html 中描述的步骤,而是编写了一个执行相同功能的 CloudFormation 模板。换句话说,您可以部署我的模板,结果是一个新创建的 Code Commit 存储库和关联的代码管道,它构建并将您定义的任何 SAM 模板部署到新的 CloudFormation 堆栈。您需要做的就是将 buildspec.yml 和 samTemplate.yaml 添加到新创建的 Code Commit 存储库并推送您的更改。

    我的模板可在下面的链接中找到。请注意,这是一个早期的草案,还有很大的改进空间......但它确实与上面链接的 AWS 指南非常相似:https://github.com/matwerber1/cloudformation-pipeline-template

    这是模板代码,samTemplate.yaml:

    AWSTemplateFormatVersion: '2010-09-09'
    Description: Creates Private Code Commit repo and Deployment Pipeline to CloudFormation
    Parameters: 
      ProjectNameParameter:
        Type: String
        Default: myProject
        Description: "the name to assign to your newly-created code repo, build project, pipeline, and IAM resources."
    
      CodeBuildS3BucketParameter:
        Type: String
        Default: "myCodeBuildS3Bucket"
        Description: "a pre-existing S3 bucket in which to store Code Build artifacts."
    
      CodePipelineS3BucketParameter:
        Type: String
        Default: "myCodePipelineS3Bucket"
        Description: "a pre-existing S3 bucket in which to store Code Pipeline resources."
    Resources:
    
      MyRepo:
        Type: "AWS::CodeCommit::Repository"
        Properties: 
          RepositoryName: !Sub '${ProjectNameParameter}'
    
      CloudFormationRole:
       Type: "AWS::IAM::Role"
       Properties:
        RoleName: !Sub "${AWS::Region}-${ProjectNameParameter}-cloudformation"
        AssumeRolePolicyDocument:
          Statement:
            - Effect: Allow
              Principal:
                Service:
                  - cloudformation.amazonaws.com
              Action:
                - "sts:AssumeRole"
        Path: "/"
        Policies:
          - PolicyName: cloudformation-service
            PolicyDocument:
              Version: "2012-10-17"
              Statement:
                - Action:
                  - "*"
                  Resource: "*"
                  Effect: Allow
    
      CodePipelineRole:
       Type: "AWS::IAM::Role"
       Properties:
        RoleName: !Sub "${AWS::Region}-${ProjectNameParameter}-codepipeline"
        AssumeRolePolicyDocument:
          Statement:
            - Effect: Allow
              Principal:
                Service:
                  - codepipeline.amazonaws.com
              Action:
                - "sts:AssumeRole"
        Path: "/"
        Policies:
          - PolicyName: codepipeline-service
            PolicyDocument:
              Version: "2012-10-17"
              Statement:
                - Action:
                  - "codecommit:GetBranch"
                  - "codecommit:GetCommit"
                  - "codecommit:UploadArchive"
                  - "codecommit:GetUploadArchiveStatus"
                  - "codecommit:CancelUploadArchive"
                  Resource: "*"
                  Effect: Allow
    
                - Action:
                  - "s3:GetObject"
                  - "s3:GetObjectVersion"
                  - "s3:GetBucketVersioning"
                  Resource: "*"
                  Effect: Allow
    
                - Action:
                  - "s3:PutObject"
                  Resource:
                    - "arn:aws:s3:::codepipeline*"
                    - "arn:aws:s3:::elasticbeanstalk*"
                  Effect: Allow
    
                - Action:
                  - "codedeploy:CreateDeployment"
                  - "codedeploy:GetApplicationRevision"
                  - "codedeploy:GetDeployment"
                  - "codedeploy:GetDeploymentConfig"
                  - "codedeploy:RegisterApplicationRevision"
                  Resource: "*"
                  Effect: Allow
    
                - Action:
                  - "elasticbeanstalk:*"
                  - "ec2:*"
                  - "elasticloadbalancing:*"
                  - "autoscaling:*"
                  - "cloudwatch:*"
                  - "s3:*"
                  - "sns:*"
                  - "cloudformation:*"
                  - "rds:*"
                  - "sqs:*"
                  - "ecs:*"
                  - "iam:PassRole"
                  Resource: "*"
                  Effect: Allow
    
                - Action:
                  - "lambda:InvokeFunction"
                  - "lambda:ListFunctions"
                  Resource: "*"
                  Effect: Allow
    
                - Action:
                  - "opsworks:CreateDeployment"
                  - "opsworks:DescribeApps"
                  - "opsworks:DescribeCommands"
                  - "opsworks:DescribeDeployments"
                  - "opsworks:DescribeInstances"
                  - "opsworks:DescribeStacks"
                  - "opsworks:UpdateApp"
                  - "opsworks:UpdateStack"
                  Resource: "*"
                  Effect: Allow
    
                - Action:
                  - "cloudformation:CreateStack"
                  - "cloudformation:DeleteStack"
                  - "cloudformation:DescribeStacks"
                  - "cloudformation:UpdateStack"
                  - "cloudformation:CreateChangeSet"
                  - "cloudformation:DeleteChangeSet"
                  - "cloudformation:DescribeChangeSet"
                  - "cloudformation:ExecuteChangeSet"
                  - "cloudformation:SetStackPolicy"
                  - "cloudformation:ValidateTemplate"
                  - "iam:PassRole"
                  Resource: "*"
                  Effect: Allow
    
                - Action:
                  - "codebuild:BatchGetBuilds"
                  - "codebuild:StartBuild"
                  Resource: "*"
                  Effect: Allow
    
      CodeBuildRole:
       Type: "AWS::IAM::Role" 
       Properties:
        RoleName: !Sub "${AWS::Region}-${ProjectNameParameter}-codebuild"
        AssumeRolePolicyDocument:
          Statement:
            - Effect: Allow
              Principal:
                Service:
                  - codebuild.amazonaws.com
              Action:
                - "sts:AssumeRole"
        Path: "/"
        Policies:
          - PolicyName: codebuild-service
            PolicyDocument:
              Version: "2012-10-17"
              Statement:
                - Action:
                  - "logs:CreateLogGroup"
                  - "logs:CreateLogStream"
                  - "logs:PutLogEvents"
                  Resource:
                  - !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/codebuild/${ProjectNameParameter}"
                  - !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/codebuild/${ProjectNameParameter}:*"
                  Effect: Allow
    
                - Action: 
                  - "s3:PutObject"
                  - "s3:GetObject"
                  - "s3:GetObjectVersion"
                  Resource: !Sub "arn:aws:s3:::codepipeline-${AWS::Region}-*"
                  Effect: Allow
    
                - Action: "ssm:GetParameters"
                  Resource:  !Sub "arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/CodeBuild/*"
                  Effect: Allow
    
                - Action: "s3:PutObject"
                  Resource: !Sub "arn:aws:s3:::${CodeBuildS3BucketParameter}*"
                  Effect: Allow
    
      MyBuild:
        Type: "AWS::CodeBuild::Project"
        Properties: 
          Artifacts:
            Type: CODEPIPELINE
          BadgeEnabled: false
          Environment:
            ComputeType: BUILD_GENERAL1_SMALL
            Image: "aws/codebuild/python:3.5.2"
            Type: LINUX_CONTAINER
          Name: !Sub '${ProjectNameParameter}'
          ServiceRole: !Ref CodeBuildRole
          Source:
            Type: CODEPIPELINE
          TimeoutInMinutes: 60
    
      MyPipeline:
        Type: "AWS::CodePipeline::Pipeline"
        Properties:
          ArtifactStore:
            Location: !Ref CodePipelineS3BucketParameter
            Type: S3
          Name: !Sub "${ProjectNameParameter}"
          RestartExecutionOnUpdate: false
          RoleArn: !GetAtt CodePipelineRole.Arn
          Stages:
            - Name: "Source"
              Actions:
                - ActionTypeId:
                    Category: Source
                    Owner: AWS
                    Provider: CodeCommit
                    Version: "1"
                  Configuration:
                    RepositoryName: !GetAtt MyRepo.Name
                    BranchName: master
                    PollForSourceChanges: true
                  Name: Source
                  OutputArtifacts:
                    - Name: MyApp
                  RunOrder: 1
    
            - Name: "Build"
              Actions:
                - ActionTypeId:
                    Category: Build
                    Owner: AWS
                    Provider: CodeBuild
                    Version: "1"
                  Configuration:
                    ProjectName: !Ref MyBuild
                  InputArtifacts:
                    - Name: MyApp
                  Name: "Build"
                  OutputArtifacts:
                    - Name: MyAppBuild
                  RunOrder: 2
    
            - Name: "Staging"
              Actions:
                - ActionTypeId:
                    Category: Deploy
                    Owner: AWS
                    Provider: CloudFormation
                    Version: "1"
                  Configuration:
                    ActionMode: CHANGE_SET_REPLACE
                    StackName: !Ref ProjectNameParameter
                    Capabilities: CAPABILITY_NAMED_IAM
                    ChangeSetName: MyChangeSet
                    RoleArn: !GetAtt CloudFormationRole.Arn
                    TemplatePath: MyAppBuild::NewSamTemplate.yaml
                  InputArtifacts:
                    - Name: MyAppBuild
                  Name: "build_changeset"
                  RunOrder: 3
    
                - ActionTypeId:
                    Category: Deploy
                    Owner: AWS
                    Provider: CloudFormation
                    Version: "1"
                  Configuration:
                    ActionMode: CHANGE_SET_EXECUTE
                    StackName: !Ref ProjectNameParameter
                    Capabilities: CAPABILITY_NAMED_IAM
                    ChangeSetName: MyChangeSet
                  Name: "execute_changeset"
                  RunOrder: 4
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-28
      • 2017-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多