Skip to content

On every deploy, publish a new Lambda function version - #2676

Merged
eahefnawy merged 7 commits into
serverless:masterfrom
ryansb:feature/tagVersions
Nov 25, 2016
Merged

eahefnawy merged 7 commits into
serverless:masterfrom
ryansb:feature/tagVersions

Conversation

@ryansb

@ryansb ryansb commented Nov 8, 2016 •

Copy link
Copy Markdown
Contributor

What did you implement:

This change publishes a Lambda version for each deploy. Now Lambda deploys count as distinct function versions, which is really helpful for logging & monitoring.

How did you implement it:

This change adds:

  • Calculation of SHA256 sums of artifacts
  • An AWS::Lambda::Alias resource based on that SHA
  • A dependency between each deployed function to its alias
  • A stack output for each function LogicalIdQualifiedArn which is the
    function ARN with the version appended.

How can we verify it:

In any serverless project, apply this patch and do a deploy. After the deploy, check in the console for the new function alias. Alternatively, use the CLI command aws lambda list-versions-by-function --function-name yourservice-stage-functionName and note that in addition to $LATEST there's also a pinned version.

Todos:

  • Write tests
  • Write documentation
  • Fix linting errors
  • Make sure code coverage hasn't dropped
  • Provide verification config/commands/resources
  • Enable "Allow edits from maintainers" for this PR
  • Change ready for review message below

Is this ready for review?: YES

@ryansb
ryansb force-pushed the feature/tagVersions branch 2 times, most recently from 3c6b8be to 49969e0 Compare November 17, 2016 18:07
@pmuens pmuens added this to the 1.3 milestone Nov 18, 2016

@eahefnawy eahefnawy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@ryansb Awesome work 🙌 ... just couple of comments about logical IDs. Furthermore, could you add those new logical IDs to the resources guide docs? ☺️

Should be G2M afterwards!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For this one as well, could move the logical ID generation into the naming.js file?

And I'd go with: FunctionNameQualifiedArn for the same reasons above

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think the function logical ID makes more sense here though. If I just use function names, outputs look like this:

        {
            "Description": "Current Lambda function version", 
            "OutputKey": "fooQualifiedArn", 
            "OutputValue": "arn:aws:lambda:us-east-1:509803855674:function:bandlab-aws-vpc-resources-dev-foo:2"
        }, 
        {
            "Description": "Lambda function info", 
            "OutputKey": "FooLambdaFunctionArn", 
            "OutputValue": "arn:aws:lambda:us-east-1:509803855674:function:bandlab-aws-vpc-resources-dev-foo"
        },

IMO, this way (using logical ID) makes more sense:

        {
            "Description": "Lambda function info", 
            "OutputKey": "FooLambdaFunctionArn", 
            "OutputValue": "arn:aws:lambda:us-east-1:509803855674:function:bandlab-aws-vpc-resources-dev-foo"
        }, 
        {
            "Description": "Current Lambda function version", 
            "OutputKey": "FooLambdaFunctionQualifiedArn", 
            "OutputValue": "arn:aws:lambda:us-east-1:509803855674:function:bandlab-aws-vpc-resources-dev-foo:4"
        },

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

could move the logical ID generation into the naming.js file?

Also I think this name is shorter and expressive enough: {FunctionName}Version{Sha} ... so we add the function name not the function logical ID. This should be enough/short identification because all function names are unique

Comment thread lib/plugins/aws/lib/naming.js Outdated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Should I instead be using normalizedFunctionName rather than the plain function?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, so that the first character is capitalized ☺️

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@eahefnawy is this change still necessary?

@ryansb

ryansb commented Nov 18, 2016

Copy link
Copy Markdown
Contributor Author

The goal that this PR moves towards is bringing back logging/awareness that used to be in the version-0.x framework. In the old framework, you'd have one function and each stage was an alias.

Now each stage has its own stack and thus own group of functions. What I want is to have each stage use an alias, so that logs are version-specific right now, in serverless v1 all logs are tagged under groups named [$LATEST]<long hash>. In the future, my goal is to have each log group associated to a specific function version for easier tracking of whether a bug has been fixed (or not).

@pmuens pmuens left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey @ryansb thank you very much for this! 💯
This is a very useful addition! Just looked through the code which looks great!

I tested it successfully on my local machine with the following service:

service: simple-service

provider:
  name: aws
  runtime: nodejs4.3

functions:
  hello:
    handler: handler.hello
  world:
    handler: handler.world

A simple rebase should resolve the conflicts (we've refactored our test structure a little bit).

It should be GTM after that and @eahefnawy second look 👀 .

Having Lambda deploys count as distinct function versions is really
helpful for logging, since it cleanly separates out logs between
sources.

This change adds:
- Calculation of SHA256 sums of artifacts
- An `AWS::Lambda::Alias` resource based on that SHA
- A dependency between each deployed function to its alias
- A stack output for each function `LogicalIdQualifiedArn` which is the
  function ARN with the version appended.
@ryansb
ryansb force-pushed the feature/tagVersions branch from 549c38f to d7a4686 Compare November 24, 2016 13:07
@ryansb

ryansb commented Nov 24, 2016

Copy link
Copy Markdown
Contributor Author

@pmuens Rebase done, thanks!

@eahefnawy

Copy link
Copy Markdown
Contributor

Thanks @ryansb this works great 🙌 ... I'll merge then make a tiny change in the logical ID to keep it consistent with our pattern. Don't wanna get this PR blocked by that tiny change.

Thanks again!

@eahefnawy
eahefnawy merged commit 5afaedf into serverless:master Nov 25, 2016
@ryansb
ryansb deleted the feature/tagVersions branch November 26, 2016 13:54
@nikgraf

nikgraf commented Dec 2, 2016 •

Copy link
Copy Markdown
Contributor

@ryansb Nice feature!

I couldn't find a valid use-case, but it kind of troubles me that someone using Serverless from now on will have legacy functions around that are actually invokable … is there a valid use-case that could lead to troubles? Do people need to do something extra e.g. define ARN's with $LATEST so older version can't be invoked --> arn:aws:lambda:aws-region:acct-id:function:helloworld:$LATEST?

More info here: http://docs.aws.amazon.com/lambda/latest/dg/versioning-aliases-permissions.html

@nikgraf

nikgraf commented Dec 2, 2016

Copy link
Copy Markdown
Contributor

For example in case you have an old version of a Lambda that cause a security breach. You probably want to make sure this version is gone for sure. As mentioned here there is a way of deleting a specific version.

If that's the worst case then I believe it's good enough to make sure framework users are aware of this feature. Thoughts?

@ryansb

ryansb commented Dec 5, 2016

Copy link
Copy Markdown
Contributor Author

No, they don't need to use a qualified (arn:aws:....:hello:$LATEST) ARN, the regular function ARN will still point to $LATEST. This is the first part of adding the ability to have aliases that point to the "live" version, so that rollbacks don't always require a full deploy, and can just be done by modifying an alias.

There's not a serverless command for deleting a specific version, right now either the console or CLI would work.

@ajagnanan

Copy link
Copy Markdown

Is there an option to disable the version creation?

@pgasiorowski

Copy link
Copy Markdown

I would also like to know if there is a way to disable it

@pmuens

pmuens commented Dec 6, 2016

Copy link
Copy Markdown
Contributor

Right now there isn't. Opened up an issue to track this and discuss an implementation:

#2871

@pmuens pmuens mentioned this pull request Dec 16, 2016
@thadeubrito

Copy link
Copy Markdown

Great job @ryansb, thanks for it. I have a question, how can I get the version attribute from ApiLambdaVersionU7fLjAh4hB6aBdBfab0OMjP36BIJFx0JvnYXBQ6H3w with this dynamic hash?
something like this:

!GetAtt ApiLambdaVersion${hash}.Version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants