简体   繁体   English

lambda function 未在 AWS 中使用 cloudformation 和管道进行更新

[英]lambda function is not being updated with cloudformation and pipeline in AWS

I'm trying to build a pipeline to automate a lambda function deployment on AWS.我正在尝试构建一个管道来自动化 AWS 上的 lambda function 部署。 I created a pipeline, a codebuild and needed IAM roles and integrated them with cloudformation.我创建了一个管道、一个代码构建和所需的 IAM 角色,并将它们与 cloudformation 集成。

here's my buildspec.yml:这是我的 buildspec.yml:

version: 0.2
phases:
  install:
    runtime-versions:
      nodejs: 14
    commands:
      - echo "Installing from package.json"
      # - npm install
  pre_build:
    commands:
      - echo "Build on `date`"
      # - npm run test
  post_build:
    commands:
      - zip -r lambda.zip index.js template.yml
      - aws s3api put-object --bucket github-lambda-demo --key lambda.zip --body lambda.zip


artifacts:
  files:
    - template.yml
  discard-paths: yes

And this is my cloudformation template:这是我的 cloudformation 模板:

AWSTemplateFormatVersion: '2010-09-09'
Description: Template for Lambda Sample.
Resources:
  LambdaRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName:
        Fn::Sub: lambda-role
      AssumeRolePolicyDocument:
        Statement:
          - Action:
            - sts:AssumeRole
            Effect: Allow
            Principal:
              Service:
              - lambda.amazonaws.com
        Version: 2012-10-17
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/AWSLambdaExecute
        - arn:aws:iam::aws:policy/AmazonS3FullAccess

  LambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        S3Bucket: github-lambda-demo
        S3Key: lambda.zip
      Description: demo for lambda deployment
      FunctionName: github-lambda-demo
      Handler: index.handler
      PackageType: Zip
      # Layers: 
      #   - String
      MemorySize: 256
      Role:
        Fn::GetAtt:
          - LambdaRole
          - Arn
      Runtime: nodejs14.x
      Timeout: 300

All the steps are done quite right with no errors in codePipeline but my lambda function source is not being update as it should be.所有步骤都完成得非常正确,codePipeline 中没有错误,但我的 lambda function 源没有按照应有的方式更新。

Any help?!有什么帮助吗?!

This is expected behavior for Lambda Resource in cloudformation, Straight from docs :这是 cloudformation 中Lambda 资源的预期行为,直接来自文档

Changes to a deployment package in Amazon S3 are not detected automatically during stack updates.在堆栈更新期间不会自动检测到对 Amazon S3 中的部署 package 的更改。 To update the function code, change the object key or version in the template.要更新 function 代码,请更改模板中的 object 密钥或版本。

So, typically two options:因此,通常有两种选择:

First option using versioned S3 Bucket :使用版本化 S3 Bucket 的第一个选项

  • Enable versioning of S3 bucket where artifact is stored.启用存储工件的 S3 存储桶的版本控制。
  • put-object cli you have used will return the version of the object.您使用的put-object cli 将返回 object 的版本。
  • Store the version in ssm parm or use a custom cloud formation resource to get latest version of an s3 object in cloudformation.版本存储在 ssm parm 中或使用自定义云形成资源在 cloudformation 中获取最新版本的 s3 object。

Second option using ssm parameter使用 ssm 参数的第二个选项

  • Suffix artifact name with a version (or even the git sha) before writing to S3.在写入 S3 之前,为工件名称加上版本(甚至是 git sha)后缀。
  • Store the version in an SSM parameter in build process itself.将版本存储在构建过程本身的 SSM 参数中。
  • Grab the version from SSM parameter in cloudformation.从 cloudformation 中的 SSM 参数中获取版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM