Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lambda): currentVersion, version.addAlias() #6771

Merged
merged 34 commits into from
Mar 25, 2020
Merged

Conversation

eladb
Copy link
Contributor

@eladb eladb commented Mar 17, 2020

Remaining work:

Commit Message

It is common for AWS services to require an explicit AWS Lambda Version when referencing functions. When an AWS::Lambda::Version resource is defined in CloudFormation is captures the AWS Lambda configuration *at the time of the creation of the version resource. This means that if the function's configuration or code is updated, the Version resource will no longer point to the function defined in the stack.

To address this, we introduce a property function.currentVersion which will create a new AWS::Lambda::Version resource every time the function's configuration changes. This is done by encoding a hash of the function's CloudFormation properties into the logical ID of the version resource.

Additionally, this change adds version.addAlias which makes it easier to define an AWS Lambda alias for a version.

The result is this:

fn.currentVersion.addAlias('live');

We employ an approach similar to apigateway's "Deployment" resource in order to implement currentVersion: during "prepare", we synthesize the CloudFormation template snippet of the AWS::Lambda::Function resource, calculate an MD5 for it and append it to the logical ID of the version resource.

Resolves #6750
Resolves #5334

End of Commit Message


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

It is a common use case to define an alias for the latest version of a function. In order to do that, one needs to define a Version resource that points to the latest version and then associate an Alias with it.

This was not trivial to do so far. Now it is:

    fn.addAlias('foo')

This will define a lambda.Version with a name the derives from the hash of the lambda's source code and then define a lambda.Alias associated with this version object. Since the name of the version resource is based on the hash, when the code changes, a new version will be created automatically.

To support this, `lambda.Code.bind()` now returns an optional `codeHash` attribute. It is supported for `lambda.Code.fromAsset` and `lambda.Code.fromInline`, for which we can calculate the source hash based on the content.

Resolves #6750
Resolves #5334
@eladb eladb requested a review from nija-at March 17, 2020 13:45
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Mar 17, 2020
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: 9d6c458
  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@skinny85
Copy link
Contributor

Just wanted to make sure this comment is taken into account in this functionality.

Co-Authored-By: Niranjan Jayakar <nija@amazon.com>
@eladb
Copy link
Contributor Author

eladb commented Mar 17, 2020

Just wanted to make sure this comment is taken into account in this functionality.

Thanks @skinny85. That's really good point. I will see what I can do about it.

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: 4ba3d1f
  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@nija-at nija-at assigned eladb and unassigned nija-at Mar 19, 2020
@iph
Copy link
Contributor

iph commented Mar 20, 2020

Just wanted to make sure this comment is taken into account in this functionality.

Posted a reply there: #5334 (comment)

I'm still for this change, we need to be a bit crafty about it though :)

@eladb eladb changed the title feat(lambda): add alias to latest version through code hash feat(lambda): currentVersion Mar 23, 2020
@eladb eladb changed the title feat(lambda): currentVersion feat(lambda): currentVersion and version.addAlias() Mar 23, 2020
@eladb eladb changed the title feat(lambda): currentVersion and version.addAlias() feat(lambda): currentVersion, version.addAlias() Mar 23, 2020
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: e5b0e25
  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@eladb eladb requested review from nija-at, rix0rrr and skinny85 March 23, 2020 09:28
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: 721f2d7
  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: da122b2
  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@rix0rrr rix0rrr added the pr/do-not-merge This PR should not be merged at this time. label Mar 23, 2020
Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Provisional approval

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: ee2b170
  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: 629c81c
  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: ac67061
  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: fe10bdb
  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: e7bddd1
  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: 5c804cd
  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: d240dfd
  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: 46366cb
  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: 88787ff
  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: c045300
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify
Copy link
Contributor

mergify bot commented Mar 25, 2020

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot merged commit c94ce62 into master Mar 25, 2020
@mergify mergify bot deleted the benisrae/code-hash branch March 25, 2020 09:44
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: fba86fd
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: 3a06e76
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

eladb pushed a commit that referenced this pull request Apr 10, 2020
README referenced a non-existing property called `function.codeHash`. This comment slipped in from a previous iteration of #6771
mergify bot pushed a commit that referenced this pull request Apr 10, 2020
README referenced a non-existing property called `function.codeHash`. This comment slipped in from a previous iteration of #6771
@iDVB
Copy link

iDVB commented May 30, 2020

Just wanted to make sure this comment is taken into account in this functionality.

Thanks @skinny85. That's really good point. I will see what I can do about it.

Hey @eladb did a resolve for this end up making it in?
I must admit, I'm a bit confused as to where things are right now with this.
I'm using aws-lambda-nodejs and need a way to handle version creations when something changes in the "bundled" versions of the function.

I'm super new to CDK and just poking around, so I added this to a local copy of aws-lambda-nodejs ...

    // Build with Parcel
    const builder = new Builder({
      entry,
      outDir: handlerDir,
      global: handler,
      minify: props.minify,
      sourceMaps: props.sourceMaps,
      cacheDir: props.cacheDir,
      nodeVersion: extractVersion(runtime),
      nodeDockerTag: props.nodeDockerTag || `${process.versions.node}-alpine`,
      projectRoot: path.resolve(projectRoot),
    });
    builder.build();
  
    super(scope, id, {
      ...props,
      runtime,
      code: lambda.Code.fromAsset(handlerDir),
      handler: `index.${handler}`,
    });

    this.shasum = getFileShaSum(`${handlerDir}/index.js`);
  }
}

function getFileShaSum(filePath: string): string {
  const relativePath = path.relative(process.cwd(), filePath);
  const shasum = crypto.createHash('sha256')
  const fileBuffer = fs.readFileSync(relativePath)
  shasum.update(fileBuffer)
  return `:sha256:${shasum.digest('hex')}`;
}

So I can do this in my main file...

    const replicaLambda = new lambda.NodejsFunction(this, 'lambda-handler', {
      minify: true,
      timeout: Duration.seconds(10),
    })
    replicaLambda.addVersion(replicaLambda.shasum)

Reading the above thread and a few other issues, it seems like you guys have gone around the block on where this functionality should live?

Would love to know how off base I am, or a workaround as this is really the last thing stopping us to diving into CDK.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution/core This is a PR that came from AWS.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enable access to SHA256 of built Lambda zipfile Deploying new version of lambda function
7 participants