Skip to content

aws-lambda Added CodeBuild CloudWatch state event#45490

Merged
elibarzilay merged 10 commits intoDefinitelyTyped:masterfrom
ivanmartos:awsLambdaCodeBuildCloudWatchState
Jun 24, 2020
Merged

aws-lambda Added CodeBuild CloudWatch state event#45490
elibarzilay merged 10 commits intoDefinitelyTyped:masterfrom
ivanmartos:awsLambdaCodeBuildCloudWatchState

Conversation

@ivanmartos
Copy link
Copy Markdown
Contributor

@ivanmartos ivanmartos commented Jun 15, 2020

Please fill in this template.

  • Use a meaningful title for the pull request. Include the name of the package modified.
  • Test the change in your own code. (Compile and run.)
  • Add or edit tests to reflect the change. (Run with npm test.)
  • Follow the advice from the readme.
  • Avoid common mistakes.
  • Run npm run lint package-name (or tsc if no tslint.json is present).

If changing an existing definition:

  • Provide a URL to documentation or source code which provides context for the suggested changes: <>
  • If this PR brings the type definitions up to date with a new version of the JS library, update the version number in the header.
  • Include tests for your changes
  • If you are making substantial changes, consider adding a tslint.json containing { "extends": "dtslint/dt.json" }. If for reason the any rule need to be disabled, disable it for that line using // tslint:disable-next-line [ruleName] and not for whole package so that the need for disabling can be reviewed.

Related https://docs.aws.amazon.com/codebuild/latest/userguide/sample-build-notifications.html

@typescript-bot typescript-bot added Where is GH Actions? GH Actions didn't give a response to this PR Popular package This PR affects a popular package (as counted by NPM download counts). labels Jun 15, 2020
@typescript-bot
Copy link
Copy Markdown
Contributor

typescript-bot commented Jun 15, 2020

@ivanmartos Thank you for submitting this PR! I see this is your first time submitting to DefinitelyTyped 👋 — keep an eye on this comment as I'll be updating it with information as things progress.

Code Reviews

Because this PR edits the configuration file, it can be merged once it's reviewed by a DT maintainer.

Status

  • ✅ No merge conflicts
  • ✅ Continuous integration tests have passed
  • ❌ A DT maintainer needs to merge changes which affect module config files

Once every item on this list is checked, I'll ask you for permission to merge and publish the changes.


Diagnostic Information: What the bot saw about this PR
{
  "type": "info",
  "now": "-",
  "pr_number": 45490,
  "author": "ivanmartos",
  "owners": [
    "darbio",
    "skarum",
    "StefH",
    "tobyhede",
    "buggy",
    "y13i",
    "wwwy3y3",
    "OrthoDex",
    "MichaelMarner",
    "daniel-cottone",
    "kostya-misura",
    "coderbyheart",
    "palmithor",
    "daniloraisi",
    "simonbuchan",
    "Haydabase",
    "repl-chris",
    "aneilbaboo",
    "jeznag",
    "louislarry",
    "dpapukchiev",
    "ohookins",
    "trevor-leach",
    "jagregory",
    "dalen",
    "loikg",
    "skyzenr",
    "redlickigrzegorz",
    "juancarbonel",
    "pwmcintyre",
    "alex-bolenok-centralreach",
    "marianzange",
    "apepper",
    "apalumbo",
    "SachinShekhar"
  ],
  "dangerLevel": "ScopedAndConfiguration",
  "headCommitAbbrOid": "7cbc1f3",
  "headCommitOid": "7cbc1f3b3cf2f0087f9e7b78b0df90d4083715a1",
  "mergeIsRequested": false,
  "stalenessInDays": 6,
  "lastCommitDate": "2020-06-17T08:45:35.000Z",
  "lastCommentDate": "2020-06-17T07:33:12.000Z",
  "reviewLink": "https://github.com/DefinitelyTyped/DefinitelyTyped/pull/45490/files",
  "hasMergeConflict": false,
  "authorIsOwner": false,
  "isFirstContribution": true,
  "popularityLevel": "Popular",
  "anyPackageIsNew": false,
  "packages": [
    "aws-lambda"
  ],
  "files": [
    {
      "filePath": "types/aws-lambda/aws-lambda-tests.ts",
      "kind": "test",
      "package": "aws-lambda"
    },
    {
      "filePath": "types/aws-lambda/index.d.ts",
      "kind": "definition",
      "package": "aws-lambda"
    },
    {
      "filePath": "types/aws-lambda/test/codebuild-tests.ts",
      "kind": "test",
      "package": "aws-lambda"
    },
    {
      "filePath": "types/aws-lambda/trigger/codebuild-cloudwatch-state.d.ts",
      "kind": "definition",
      "package": "aws-lambda"
    },
    {
      "filePath": "types/aws-lambda/tsconfig.json",
      "kind": "package-meta",
      "package": "aws-lambda"
    }
  ],
  "hasDismissedReview": false,
  "ciResult": "pass",
  "lastReviewDate": "2020-06-17T09:00:48.000Z",
  "reviewersWithStaleReviews": [],
  "approvalFlags": 2,
  "isChangesRequested": false
}

@typescript-bot
Copy link
Copy Markdown
Contributor

@typescript-bot typescript-bot removed the Where is GH Actions? GH Actions didn't give a response to this PR label Jun 15, 2020
@typescript-bot
Copy link
Copy Markdown
Contributor

👋 Hi there! I’ve run some quick measurements against master and your PR. These metrics should help the humans reviewing this PR gauge whether it might negatively affect compile times or editor responsiveness for users who install these typings.

Let’s review the numbers, shall we?

Comparison details 📊
master #45490 diff
Batch compilation
Memory usage (MiB) 72.0 74.9 +4.1%
Type count 10754 10947 +2%
Assignability cache size 2339 2362 +1%
Language service
Samples taken 2501 2366 -5%
Identifiers in tests 2884 2916 +1%
getCompletionsAtPosition
    Mean duration (ms) 294.2 303.4 +3.1%
    Mean CV 9.5% 9.1%
    Worst duration (ms) 374.4 381.9 +2.0%
    Worst identifier event CustomHandler
getQuickInfoAtPosition
    Mean duration (ms) 295.2 305.6 +3.5%
    Mean CV 9.7% 9.6% -1.3%
    Worst duration (ms) 385.8 389.3 +0.9%
    Worst identifier typedCallback cognitoIdentityPoolId

It looks like nothing changed too much. I won’t post performance data again unless it gets worse.

@typescript-bot typescript-bot added the Perf: Same typescript-bot determined that this PR will not significantly impact compilation performance. label Jun 15, 2020
Copy link
Copy Markdown
Contributor

@simonbuchan simonbuchan left a comment

Choose a reason for hiding this comment

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

Thanks for all this work! A few notes and quibbles, but this looks fine to merge overall to me.

| 'BUILD_GENERAL1_2XLARGE';
export type CodeBuildEnvironmentVariableType = 'PARAMETER_STORE' | 'PLAINTEXT';

export interface CodeBuildCloudWatchStateEvent {
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.

This should be able to use the CloudWatchEvent<> (?) template, passing in the detail type and detail.

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.

not sure what you mean here. Could you please explain a bit more? I based this PR on the codepipeline-cloudwatch* types. Can't seem to find if there

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.

Sorry, was me being on a phone again. I was referring to the EventBridgeEvent<TDetailType extends string, TDetail> in trigger/eventbridge.d.ts which seems to be what this structure is based on. It's more for users to be able to compose their own custom event structures though.

/* CodeBuild CloudWatch Events
* see https://docs.aws.amazon.com/codebuild/latest/userguide/sample-build-notifications.html
*/
const CodeBuildCloudWatchStateEvent: CodeBuildCloudWatchStateEvent = {
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.

Ideally tests should be using the handler types, like a user would, but we still have several in this style. It's much less important when there isn't a result type though.

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.

after I gave it a bit of thought it makes more sense to remove those Codebuild specific handlers. In my project I'm currently receiving these types as a message of SNSEvent triggered by CloudWatch event. That means that if I needed to create a handler, I would be just replicating SNS types
I already removed those handlers
Thanks

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.

Sorry, wrote the first review on a phone, so I was a bit terse. I'm referring to writing the test that uses the types as a user would use the types, e.g. writing a handler that reads the fields. This catches a lot of broken changes or unexpected warts in using types.

So for this package that would be writing a handler type, normally something like:

const someHandler: SomeHandler = async (event) => {
  strOrUndefined = event.someOptionalField;
  ...
};

though it's a bit more complicated with a result type.

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 understand what you mean about the test of handler. What I wanted to say is that handler does not directly receive CodeBuildCloudWatchStateEvent, but it receives SNSEvent with record that contains CodeBuildCloudWatchStateEvent as a JSON string in Message

Something like

import { CodeBuildCloudWatchStateEvent, SNSEvent } from 'aws-lambda'

export const handleCodebuildEvent = async (event: SNSEvent) => {
  const record = event.Records[0]

  const codebuildEvent: CodeBuildCloudWatchStateEvent = JSON.parse(record.Sns.Message)
}

I haven't tried integration with EventBridge, only via CloudWatch Event and SNS.
Basically the use case is that you have CloudWatch event defined that checks CodeBuild state changes. Upon event it pushes a message to SNS topic and there is lambda subscribed to this SNS topic. This lambda consumes the messages as in a code of block above.

I already have working monitoring of CodePipelines in the same way - CloudWatch event checks CodePipeline executions and upon event it pushes messages to SNS topic and there is lambda subscribed to that topic

import { CodePipelineCloudWatchActionEvent, SNSEvent } from 'aws-lambda'

export const handlePipelineEvent = async (event: SNSEvent) => {
  const record = event.Records[0]

  const pipelineEvent: CodePipelineCloudWatchActionEvent = JSON.parse(record.Sns.Message)
}

In this use case I'm not sure it makes sense to create a test for handler, cause I would be testing the SNSEvent in the handler, not the CodeBuildCloudWatchStateEvent - since handler receives type of SNSEvent and the CodeBuildCloudWatchStateEvent is just a stringified JSON in the SNSEvent
However if you feel strong about it I can add the test that will contain SNSEvent as an event type of handler and it will contain JSON string of CodeBuildCloudWatchStateEvent and in the end I will just check whether the Message of SNSEvent consists the same stringified JSON

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.

EventBridge is just CloudWatch Events renamed. Presumably you can just send these events straight to lambda: (I haven't messed with anything other than scheduled events), this would be the handler type.

I would somewhat strongly prefer that the test was reading from the event, not creating one, as that catches problems with changes to the structure.

With SNS usage as the test, this might look something like:

export const handlePipelineEvent = async (snsEvent: SNSEvent) => {
  const event: CodePipelineCloudWatchActionEvent = JSON.parse(snsEvent.Records[0].Sns.Message)

  str = event.version;
  str = event.id;
  // and so on for the rest of the fields, using types from `aws-lambda-tests.ts` where appropriate.
}

but obviously, that's quite a decent bit of work, so I'm not going to block on that, much better to have this merged.

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 comparison, here's an example of a test parsing data from the event to get further data:
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/aws-lambda/test/cloudwatch-tests.ts#L13-L15

Note that unless you're getting this as an event directly in a handler, this type shouldn't have the ...Event suffix, maybe something like ...MessageData? But I think you can and thus should use the EventBridge handler structure.

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.

ok, no problem
I'll modify it and add the tests 👍

| 'BUILD_GENERAL1_MEDIUM'
| 'BUILD_GENERAL1_LARGE'
| 'BUILD_GENERAL1_2XLARGE';
export type CodeBuildEnvironmentVariableType = 'PARAMETER_STORE' | 'PLAINTEXT';
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.

I'm not a big fan of calling types "types", but really it's more important to be consistent with the property name, documentation and the rest of the package, in that order, and it's not really that important.

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 based the naming on the sdk documentation and there they explicitly use type
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CodeBuild.html

@typescript-bot typescript-bot added the Owner Approved A listed owner of this package signed off on the pull request. label Jun 15, 2020
@typescript-bot typescript-bot removed the Owner Approved A listed owner of this package signed off on the pull request. label Jun 16, 2020
@typescript-bot
Copy link
Copy Markdown
Contributor

@simonbuchan Thank you for reviewing this PR! The author has pushed new commits since your last review. Could you take another look and submit a fresh review?

Copy link
Copy Markdown
Contributor

@simonbuchan simonbuchan left a comment

Choose a reason for hiding this comment

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

I'm going to put this on hold for a second, as I'm a bit confused about the handler type being removed.

According to my understanding of the CodeBuild notification docs, these structures are what are delivered to SNS, not to Lambda, and this would only be the structure if the SNS template is a passthrough. But other documentation refers to the event being delivered to EventBridge:

In Detail type, choose Basic if you want only the information provided to Amazon EventBridge included in the notification.

But I can't seem to find the documentation for the actual EventBridge stuff that lambda could receive, which is what this package generally is focused on, structures defined by AWS, and for lambda triggers, not just structures that you could happen to use in lambda. I think there are a few examples of structures that are delivered as JSON in an event, but they're pretty rare.

Just to reiterate, this isn't a no: just a check that this does actually make sense for this to be added in this package.

/* CodeBuild CloudWatch Events
* see https://docs.aws.amazon.com/codebuild/latest/userguide/sample-build-notifications.html
*/
const CodeBuildCloudWatchStateEvent: CodeBuildCloudWatchStateEvent = {
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.

Sorry, wrote the first review on a phone, so I was a bit terse. I'm referring to writing the test that uses the types as a user would use the types, e.g. writing a handler that reads the fields. This catches a lot of broken changes or unexpected warts in using types.

So for this package that would be writing a handler type, normally something like:

const someHandler: SomeHandler = async (event) => {
  strOrUndefined = event.someOptionalField;
  ...
};

though it's a bit more complicated with a result type.

| 'BUILD_GENERAL1_2XLARGE';
export type CodeBuildEnvironmentVariableType = 'PARAMETER_STORE' | 'PLAINTEXT';

export interface CodeBuildCloudWatchStateEvent {
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.

Sorry, was me being on a phone again. I was referring to the EventBridgeEvent<TDetailType extends string, TDetail> in trigger/eventbridge.d.ts which seems to be what this structure is based on. It's more for users to be able to compose their own custom event structures though.

@typescript-bot typescript-bot added the Revision needed This PR needs code changes before it can be merged. label Jun 16, 2020
@typescript-bot
Copy link
Copy Markdown
Contributor

@ivanmartos One or more reviewers has requested changes. Please address their comments. I'll be back once they sign off or you've pushed new commits or comments. If you disagree with the reviewer's comments, you can "dismiss" the review using GitHub's review UI. Thank you!

@typescript-bot typescript-bot removed the Revision needed This PR needs code changes before it can be merged. label Jun 17, 2020
Copy link
Copy Markdown
Contributor

@simonbuchan simonbuchan left a comment

Choose a reason for hiding this comment

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

That was fast! Thanks for putting up with me through this!

@typescript-bot typescript-bot added the Owner Approved A listed owner of this package signed off on the pull request. label Jun 17, 2020
@typescript-bot
Copy link
Copy Markdown
Contributor

Updated numbers for you here from 7ab9466.

Comparison details 📊
master #45490 diff
Batch compilation
Memory usage (MiB) 73.2 74.2 +1.4%
Type count 10754 11010 +2%
Assignability cache size 2339 2365 +1%
Language service
Samples taken 2405 2461 +2%
Identifiers in tests 2884 3074 +7%
getCompletionsAtPosition
    Mean duration (ms) 299.9 320.0 +6.7%
    Mean CV 9.0% 8.2%
    Worst duration (ms) 391.7 510.9 +30.4% 🔸
    Worst identifier iamRolesToOverride triggerSource
getQuickInfoAtPosition
    Mean duration (ms) 301.1 318.0 +5.6%
    Mean CV 9.0% 8.3% -7.9%
    Worst duration (ms) 396.3 398.3 +0.5%
    Worst identifier Callback request

Looks like there were a couple significant differences—take a look at worst-case duration for getting completions at a position to make sure everything looks ok.

@typescript-bot typescript-bot added Perf: Worse typescript-bot determined that this PR has a negative impact on compilation performance. and removed Perf: Same typescript-bot determined that this PR will not significantly impact compilation performance. labels Jun 17, 2020
@typescript-bot typescript-bot added the Check Config Changes a module config files label Jun 24, 2020
@elibarzilay elibarzilay merged commit 413fe84 into DefinitelyTyped:master Jun 24, 2020
@typescript-bot
Copy link
Copy Markdown
Contributor

I just published @types/aws-lambda@8.10.57 to npm.

ngbrown pushed a commit to ngbrown-forks/DefinitelyTyped that referenced this pull request Jul 11, 2020
)

* added type and handler for CodeBuild Cloudwatch State event

* added type and handler for CodeBuild Cloudwatch event

* added test for codebuild cloudwatch event

* extended definitions block

* added SECRETS_MANAGER as environment variable type

* removed handlers

* removed unnecessary general codebuild types

* CodeBuildCloudWatchStateEvent extends EventBridgeEvent and added CodeBuildCloudWatchStateHandler

* added general type strArrayOrUndefined for tests

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

Labels

Check Config Changes a module config files Owner Approved A listed owner of this package signed off on the pull request. Perf: Worse typescript-bot determined that this PR has a negative impact on compilation performance. Popular package This PR affects a popular package (as counted by NPM download counts).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants