Is it possible to match this event with an AWS EventBridge pattern? - amazon-web-services

I'm trying to create an EventBridge Event Pattern that matches a CodeBuild state change only when a specific string is present in "phase-context" of one of the objects within the "phases" array.
I've been unable to find an example of how to match a single objects property within an array of objects. Is it possible? I know I could look to match a CodeBuild phases change event instead but want to avoid that if possible.
I have marked in the string I want to match in the example CodeBuild state change event below:
{
"version": "0",
"id": "c030038d-8c4d-6141-9545-00ff7b7153EX",
"detail-type": "CodeBuild Build State Change",
"source": "aws.codebuild",
"account": "123456789012",
"time": "2017-09-01T16:14:28Z",
"region": "us-west-2",
"resources":[
"arn:aws:codebuild:us-west-2:123456789012:build/my-sample-project:8745a7a9-c340-456a-9166-edf953571bEX"
],
"detail":{
"build-status": "SUCCEEDED",
"project-name": "my-sample-project",
"build-id": "arn:aws:codebuild:us-west-2:123456789012:build/my-sample-project:8745a7a9-c340-456a-9166-edf953571bEX",
"additional-information": {
"artifact": {
"md5sum": "da9c44c8a9a3cd4b443126e823168fEX",
"sha256sum": "6ccc2ae1df9d155ba83c597051611c42d60e09c6329dcb14a312cecc0a8e39EX",
"location": "arn:aws:s3:::codebuild-123456789012-output-bucket/my-output-artifact.zip"
},
"environment": {
"image": "aws/codebuild/standard:4.0",
"privileged-mode": false,
"compute-type": "BUILD_GENERAL1_SMALL",
"type": "LINUX_CONTAINER",
"environment-variables": []
},
"timeout-in-minutes": 60,
"build-complete": true,
"initiator": "MyCodeBuildDemoUser",
"build-start-time": "Sep 1, 2017 4:12:29 PM",
"source": {
"location": "codebuild-123456789012-input-bucket/my-input-artifact.zip",
"type": "S3"
},
"logs": {
"group-name": "/aws/codebuild/my-sample-project",
"stream-name": "8745a7a9-c340-456a-9166-edf953571bEX",
"deep-link": "https://console.aws.amazon.com/cloudwatch/home?region=us-west-2#logEvent:group=/aws/codebuild/my-sample-project;stream=8745a7a9-c340-456a-9166-edf953571bEX"
},
"phases": [
{
"phase-context": [],
"start-time": "Sep 1, 2017 4:12:29 PM",
"end-time": "Sep 1, 2017 4:12:29 PM",
"duration-in-seconds": 0,
"phase-type": "SUBMITTED",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [],
"start-time": "Sep 1, 2017 4:12:29 PM",
"end-time": "Sep 1, 2017 4:13:05 PM",
"duration-in-seconds": 36,
"phase-type": "PROVISIONING",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [],
"start-time": "Sep 1, 2017 4:13:05 PM",
"end-time": "Sep 1, 2017 4:13:10 PM",
"duration-in-seconds": 4,
"phase-type": "DOWNLOAD_SOURCE",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [],
"start-time": "Sep 1, 2017 4:13:10 PM",
"end-time": "Sep 1, 2017 4:13:10 PM",
"duration-in-seconds": 0,
"phase-type": "INSTALL",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [],
"start-time": "Sep 1, 2017 4:13:10 PM",
"end-time": "Sep 1, 2017 4:13:10 PM",
"duration-in-seconds": 0,
"phase-type": "PRE_BUILD",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [
"something here" <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< THIS STRING
],
"start-time": "Sep 1, 2017 4:13:10 PM",
"end-time": "Sep 1, 2017 4:14:21 PM",
"duration-in-seconds": 70,
"phase-type": "BUILD",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [],
"start-time": "Sep 1, 2017 4:14:21 PM",
"end-time": "Sep 1, 2017 4:14:21 PM",
"duration-in-seconds": 0,
"phase-type": "POST_BUILD",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [],
"start-time": "Sep 1, 2017 4:14:21 PM",
"end-time": "Sep 1, 2017 4:14:21 PM",
"duration-in-seconds": 0,
"phase-type": "UPLOAD_ARTIFACTS",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [],
"start-time": "Sep 1, 2017 4:14:21 PM",
"end-time": "Sep 1, 2017 4:14:26 PM",
"duration-in-seconds": 4,
"phase-type": "FINALIZING",
"phase-status": "SUCCEEDED"
},
{
"start-time": "Sep 1, 2017 4:14:26 PM",
"phase-type": "COMPLETED"
}
]
},
"current-phase": "COMPLETED",
"current-phase-context": "[]",
"version": "1"
}
}

EventBridge supports value matches within array (https://docs.aws.amazon.com/eventbridge/latest/userguide/arrays-in-eventbridge-event-patterns.html). I'm not certain but AFAIK it doesn't support patterns within an array. To be sure, I'd recommend trying out the filters specified in https://docs.aws.amazon.com/eventbridge/latest/userguide/content-filtering-with-event-patterns.html within a rule pattern.
If this fails, you would need to setup a lambda (or fargate / ec2) to transform the input before calling put-events again.

Related

how to customise codebuild/codepipeline notifications for slack?

What I am trying to achieve :- When a codebuild stage( action) is is successful, I want to send a notification to slack. The real catch is in the message I want to send the codebuild build logs console link.
In my current workflow when codebuild build is successful it sends default event to sns -> which triggers lambda -> slack
I am pretty sure there must be a way to receive codebuild build logs console link. Since URL for console link for every build is dynamic it cant be set static in my opinion.
Also:- Since this is not possible in default event, I was thinking to create a custom event in lambda ( after getting triggered from sns ) and add the console link and then pass on to slack.Is this the right approach?
You can get log group link in two way.
First one is on Codebuild page you can create "Create notification rule" under "Notify" menu. Then you need to select only "Succeeded" on Build State of "Events that trigger notifications" then only two option to forward this message SNS or AWS Chatbot.
When you choose SNS, attach a Lambda as subscription to this SNS.On lambda function you will get a message like
message = json.loads(event.Records[0].SNS.Message) then get log from message message.detail.additional-information.logs.deep-link
{
"Records": [
{
"EventSource": "aws:sns",
"EventVersion": "1.0",
"EventSubscriptionArn": "arn:aws:sns:us-east-1: 3595xxx02761:codestar-notifications-test: 3648ad36-735e-4ce7-83ad-f5340d503c5d",
"Sns": {
"Type": "Notification",
"MessageId": "df6e2eed-651e-5e21-b24b-72d5c7cdb684",
"TopicArn": "arn:aws:sns:us-east-1: 3595xxx02761:codestar-notifications-test",
"Subject": None,
"Message": "{"account": "3595xxx02761",
"detailType": "CodeBuild Build State Change",
"region": "us-east-1",
"source": "aws.codebuild",
"time": "2023-01-11T11:42:06Z",
"notificationRuleArn": "arn:aws:codestar-notifications:us-east-1:3595xxx02761:notificationrule/e0b422a57a890c96aad913a2d9b0ca0d2f845223",
"detail": {
"build-status": "SUCCEEDED",
"project-name": "test-build",
"build-id": "arn:aws:codebuild:us-east-1:3595xxx02761:build/test-build:88cfdd6f-0028-4bd6-ae43-ce44fcf5bbc2",
"additional-information": {
"cache": {
"type": "NO_CACHE"
},
"build-number": 1.0,
"timeout-in-minutes": 60.0,
"build-complete": true,
"initiator": "root",
"build-start-time": "Jan 11, 2023 11:41:22 AM",
"source": {
"git-clone-depth": 1.0,
"type": "NO_SOURCE"
},
"artifact": {
"location": ""
},
"environment": {
"image": "aws/codebuild/amazonlinux2-x86_64-standard:4.0",
"privileged-mode": false,
"image-pull-credentials-type": "CODEBUILD",
"compute-type": "BUILD_GENERAL1_SMALL",
"type": "LINUX_CONTAINER",
"environment-variables": []
},
"logs": {
"group-name": "/aws/codebuild/test-build",
"stream-name": "88cfdd6f-0028-4bd6-ae43-ce44fcf5bbc2",
"deep-link": "https://console.aws.amazon.com/cloudwatch/home?region=us-east-1#logEvent:group=/aws/codebuild/test-build;stream=88cfdd6f-0028-4bd6-ae43-ce44fcf5bbc2"
},
"phases": [
{
"phase-context": [],
"start-time": "Jan 11, 2023 11:41:22 AM",
"end-time": "Jan 11, 2023 11:41:22 AM",
"duration-in-seconds": 0.0,
"phase-type": "SUBMITTED",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [],
"start-time": "Jan 11, 2023 11:41:22 AM",
"end-time": "Jan 11, 2023 11:41:24 AM",
"duration-in-seconds": 1.0,
"phase-type": "QUEUED",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [
": "
],
"start-time": "Jan 11, 2023 11:41:24 AM",
"end-time": "Jan 11, 2023 11:41:57 AM",
"duration-in-seconds": 33.0,
"phase-type": "PROVISIONING",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [
": "
],
"start-time": "Jan 11, 2023 11:41:57 AM",
"end-time": "Jan 11, 2023 11:42:00 AM",
"duration-in-seconds": 2.0,
"phase-type": "DOWNLOAD_SOURCE",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [
": "
],
"start-time": "Jan 11, 2023 11:42:00 AM",
"end-time": "Jan 11, 2023 11:42:00 AM",
"duration-in-seconds": 0.0,
"phase-type": "INSTALL",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [
": "
],
"start-time": "Jan 11, 2023 11:42:00 AM",
"end-time": "Jan 11, 2023 11:42:00 AM",
"duration-in-seconds": 0.0,
"phase-type": "PRE_BUILD",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [
": "
],
"start-time": "Jan 11, 2023 11:42:00 AM",
"end-time": "Jan 11, 2023 11:42:00 AM",
"duration-in-seconds": 0.0,
"phase-type": "BUILD",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [
": "
],
"start-time": "Jan 11, 2023 11:42:00 AM",
"end-time": "Jan 11, 2023 11:42:00 AM",
"duration-in-seconds": 0.0,
"phase-type": "POST_BUILD",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [
": "
],
"start-time": "Jan 11, 2023 11:42:00 AM",
"end-time": "Jan 11, 2023 11:42:00 AM",
"duration-in-seconds": 0.0,
"phase-type": "UPLOAD_ARTIFACTS",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [
": "
],
"start-time": "Jan 11, 2023 11:42:00 AM",
"end-time": "Jan 11, 2023 11:42:04 AM",
"duration-in-seconds": 4.0,
"phase-type": "FINALIZING",
"phase-status": "SUCCEEDED"
},
{
"start-time": "Jan 11, 2023 11:42:04 AM",
"phase-type": "COMPLETED"
}
],
"queued-timeout-in-minutes": 480.0
},
"current-phase": "COMPLETED",
"current-phase-context": "[: ]",
"version": "1"
},
"resources": [
"arn:aws:codebuild:us-east-1:3595xxx02761:build/test-build:88cfdd6f-0028-4bd6-ae43-ce44fcf5bbc2"
],
"additionalAttributes": {}
}", "Timestamp": "2023-01-11T11: 42: 11.284Z", "SignatureVersion": "1", "Signature": "QyI4CAlszBYE8MJLy+Wwxpn/Ie4kFYigaaRzPEAz4v6vFDE7x7TjaG7yWSiLNGFnBXLpg6Cl3/B70eTLJKmNv/5adXOsDz0Pw53iAcXumLsgtSuPoCpG3zISPvgR1nLBfZia7fv0w02C6d9LrjQq5v8jRZi/GJUt5frigL4Tfq3Q+wAFkmjl31GfxjcSTc0X7ujO1t+/qRIcVaa+XXOhLcDTWG9Q0qWMh0jyf5/126d3CUSRhlM6v9OzqZ4w5a/qEkq+XMaH2ElDdF9rEN+D+GayqltA4hkxwERsbvNyHmG7LBthWChezxfohJ88/O3nIvCsyv1IX9MNgyeWcOxHnA==", "SigningCertUrl": "https: //sns.us-east-1.amazonaws.com/SimpleNotificationService-56e67fcb41f6fec09b0196692625d385.pem", "UnsubscribeUrl": "https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:3595xxx02761:codestar-notifications-test:3648ad36-735e-4ce7-83ad-f5340d503c5d", "MessageAttributes": {}}}]}
Other option using EventBridge Rule, using an event pattern like
{
"source": ["aws.codebuild"],
"detail-type": ["CodeBuild Build State Change"],
"detail": {
"build-status": ["SUCCEEDED"]
}
}
your answer on the event field event.detail.additional-information.logs.deep-link is link for log stream.
{
"version": "0",
"id": "1bdc2d34-2b5a-4de9-8738-bd38ef2de265",
"detail-type": "CodeBuild Build State Change",
"source": "aws.codebuild",
"account": "3595xxx02761",
"time": "2023-01-11T11:42:06Z",
"region": "us-east-1",
"resources": ["arn:aws:codebuild:us-east-1:3595xxx02761:build/test-build:88cfdd6f-0028-4bd6-ae43-ce44fcf5bbc2"],
"detail": {
"build-status": "SUCCEEDED",
"project-name": "test-build",
"build-id": "arn:aws:codebuild:us-east-1:3595xxx02761:build/test-build:88cfdd6f-0028-4bd6-ae43-ce44fcf5bbc2",
"additional-information": {
"cache": {"type": "NO_CACHE"},
"build-number": 1.0,
"timeout-in-minutes": 60.0,
"build-complete": True,
"initiator": "root",
"build-start-time": "Jan 11, 2023 11:41:22 AM",
"source": {"buildspec": "version: 0.2\n\nphases:\n build:\n commands:\n - echo "Hello world"\n", "git-clone-depth": 1.0, "type": "NO_SOURCE"},
"artifact": {"location": ""},
"environment": {"image": "aws/codebuild/amazonlinux2-x86_64-standard:4.0", "privileged-mode": False, "image-pull-credentials-type": "CODEBUILD", "compute-type": "BUILD_GENERAL1_SMALL", "type": "LINUX_CONTAINER", "environment-variables": []},
"logs": {"group-name": "/aws/codebuild/test-build", "stream-name": "88cfdd6f-0028-4bd6-ae43-ce44fcf5bbc2", "deep-link": "https://console.aws.amazon.com/cloudwatch/home?region=us-east-1#logEvent:group=/aws/codebuild/test-build;stream=88cfdd6f-0028-4bd6-ae43-ce44fcf5bbc2"},
"phases": [{"phase-context": [], "start-time": "Jan 11, 2023 11:41:22 AM", "end-time": "Jan 11, 2023 11:41:22 AM", "duration-in-seconds": 0.0, "phase-type": "SUBMITTED", "phase-status": "SUCCEEDED"}, {"phase-context": [], "start-time": "Jan 11, 2023 11:41:22 AM", "end-time": "Jan 11, 2023 11:41:24 AM", "duration-in-seconds": 1.0, "phase-type": "QUEUED", "phase-status": "SUCCEEDED"}, {"phase-context": [": "], "start-time": "Jan 11, 2023 11:41:24 AM", "end-time": "Jan 11, 2023 11:41:57 AM", "duration-in-seconds": 33.0, "phase-type": "PROVISIONING", "phase-status": "SUCCEEDED"}, {"phase-context": [": "], "start-time": "Jan 11, 2023 11:41:57 AM", "end-time": "Jan 11, 2023 11:42:00 AM", "duration-in-seconds": 2.0, "phase-type": "DOWNLOAD_SOURCE", "phase-status": "SUCCEEDED"}, {"phase-context": [": "], "start-time": "Jan 11, 2023 11:42:00 AM", "end-time": "Jan 11, 2023 11:42:00 AM", "duration-in-seconds": 0.0, "phase-type": "INSTALL", "phase-status": "SUCCEEDED"}, {"phase-context": [": "], "start-time": "Jan 11, 2023 11:42:00 AM", "end-time": "Jan 11, 2023 11:42:00 AM", "duration-in-seconds": 0.0, "phase-type": "PRE_BUILD", "phase-status": "SUCCEEDED"}, {"phase-context": [": "], "start-time": "Jan 11, 2023 11:42:00 AM", "end-time": "Jan 11, 2023 11:42:00 AM", "duration-in-seconds": 0.0, "phase-type": "BUILD", "phase-status": "SUCCEEDED"}, {"phase-context": [": "], "start-time": "Jan 11, 2023 11:42:00 AM", "end-time": "Jan 11, 2023 11:42:00 AM", "duration-in-seconds": 0.0, "phase-type": "POST_BUILD", "phase-status": "SUCCEEDED"}, {"phase-context": [": "], "start-time": "Jan 11, 2023 11:42:00 AM", "end-time": "Jan 11, 2023 11:42:00 AM", "duration-in-seconds": 0.0, "phase-type": "UPLOAD_ARTIFACTS", "phase-status": "SUCCEEDED"}, {"phase-context": [": "], "start-time": "Jan 11, 2023 11:42:00 AM", "end-time": "Jan 11, 2023 11:42:04 AM", "duration-in-seconds": 4.0, "phase-type": "FINALIZING", "phase-status": "SUCCEEDED"}, {"start-time": "Jan 11, 2023 11:42:04 AM", "phase-type": "COMPLETED"}],
"queued-timeout-in-minutes": 480.0
},
"current-phase": "COMPLETED",
"current-phase-context": "[: ]",
"version": "1"
}
}

AWS EventBridge: Get branch name and last commit author for AWS CodeBuild event rule

We're using CodeBuild and have implemented our own email build notification system (as seems to be required currently).
We use the same CodeBuild project to build all of our branches for the same Bitbucket repository and we use an EventBridge rule that triggers if that CodeBuild project fails. It then calls a Lambda function to issue a notification via a SNS topic.
We can get some basic information in the event passed to Lambda from EventBridge, such as the build number, project and status. But we need to include the branch the build was triggered for since it is confusing when these emails arrive and it's unclear what branch the error was on. Additionally, having the author of the commit triggering the build would be helpful to just email them rather than the more general notification we're currently doing.
Unfortunately neither the branch nor author seem to be in the event. Is there a way to include them or fetch them?
Our EventBridge event pattern is
{
"detail": {
"build-status": ["FAILED"],
"project-name": ["api"]
},
"detail-type": ["CodeBuild Build State Change"],
"source": ["aws.codebuild"]
}
Our Lambda is
const AWS = require("aws-sdk");
const snsTopicArn = process.env.TOPIC_ARN;
exports.handler = (event, context, callback) => {
const status = event.detail["build-status"];
const project = event.detail["project-name"];
const buildLabel = event.detail["additional-information"]["build-number"];
const account = event.account;
const region = event.region;
// determine the link to the build
const buildFullId = event.detail["build-id"];
const buildIdParts = buildFullId.split(":");
const buildId = buildIdParts[buildIdParts.length - 1];
const buildLink = `https://${region}.console.aws.amazon.com/codesuite/codebuild/${account}/projects/${project}/build/${project}%3A${buildId}/?region=${region}`;
const subject = `${project} build ${buildLabel} ${status}`;
const message = `${subject}\n\n${buildLink}`;
const sns = new AWS.SNS();
sns.publish(
{
TopicArn: snsTopicArn,
Message: message,
Subject: subject,
},
function (error, data) {
if (error) console.log(error, error.stack);
callback(error, data);
}
);
};
And we're getting events like this passed to our Lambda:
{
"version": "0",
"id": "...",
"detail-type": "CodeBuild Build State Change",
"source": "aws.codebuild",
"account": "...",
"time": "2022-08-24T09:01:14Z",
"region": "eu-west-2",
"resources": [
"arn:aws:codebuild:eu-west-2:...:build/demo-release-deployment:..."
],
"detail": {
"build-status": "FAILED",
"project-name": "demo-release-deployment",
"build-id": "arn:aws:codebuild:eu-west-2:...:build/demo-release-deployment:...",
"additional-information": {
"cache": {
"type": "NO_CACHE"
},
"build-number": 18,
"timeout-in-minutes": 10,
"build-complete": true,
"initiator": "...",
"build-start-time": "Aug 24, 2022 8:57:39 AM",
"source": {
"report-build-status": true,
"buildspec": "",
"location": "...#bitbucket.org/.../build-test.git>https://...#bitbucket.org/.../build-test.git",
"git-clone-depth": 1,
"type": "BITBUCKET",
"git-submodules-config": {
"fetch-submodules": true
}
},
"artifact": {
"location": ""
},
"environment": {
"image": "aws/codebuild/amazonlinux2-x86_64-standard:4.0",
"privileged-mode": false,
"image-pull-credentials-type": "CODEBUILD",
"compute-type": "BUILD_GENERAL1_SMALL",
"type": "LINUX_CONTAINER",
"environment-variables": []
},
"logs": {
"group-name": "demo-deployment",
"stream-name": "demo-deployment/...",
"deep-link": "https://console.aws.amazon.com/cloudwatch/home?region=eu-west-2#logEvent:group=demo-deployment;stream=demo-deployment/..."
},
"phases": [
{
"phase-context": [],
"start-time": "Aug 24, 2022 8:57:39 AM",
"end-time": "Aug 24, 2022 8:57:39 AM",
"duration-in-seconds": 0,
"phase-type": "SUBMITTED",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [],
"start-time": "Aug 24, 2022 8:57:39 AM",
"end-time": "Aug 24, 2022 8:58:51 AM",
"duration-in-seconds": 72,
"phase-type": "QUEUED",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [": "],
"start-time": "Aug 24, 2022 8:58:51 AM",
"end-time": "Aug 24, 2022 9:00:59 AM",
"duration-in-seconds": 127,
"phase-type": "PROVISIONING",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [": "],
"start-time": "Aug 24, 2022 9:00:59 AM",
"end-time": "Aug 24, 2022 9:01:06 AM",
"duration-in-seconds": 6,
"phase-type": "DOWNLOAD_SOURCE",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [": "],
"start-time": "Aug 24, 2022 9:01:06 AM",
"end-time": "Aug 24, 2022 9:01:06 AM",
"duration-in-seconds": 0,
"phase-type": "INSTALL",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [": "],
"start-time": "Aug 24, 2022 9:01:06 AM",
"end-time": "Aug 24, 2022 9:01:06 AM",
"duration-in-seconds": 0,
"phase-type": "PRE_BUILD",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [
"COMMAND_EXECUTION_ERROR: Error while executing command: npm publish. Reason: exit status 1"
],
"start-time": "Aug 24, 2022 9:01:06 AM",
"end-time": "Aug 24, 2022 9:01:11 AM",
"duration-in-seconds": 4,
"phase-type": "BUILD",
"phase-status": "FAILED"
},
{
"phase-context": [": "],
"start-time": "Aug 24, 2022 9:01:11 AM",
"end-time": "Aug 24, 2022 9:01:11 AM",
"duration-in-seconds": 0,
"phase-type": "POST_BUILD",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [": "],
"start-time": "Aug 24, 2022 9:01:11 AM",
"end-time": "Aug 24, 2022 9:01:11 AM",
"duration-in-seconds": 0,
"phase-type": "UPLOAD_ARTIFACTS",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [": "],
"start-time": "Aug 24, 2022 9:01:11 AM",
"end-time": "Aug 24, 2022 9:01:13 AM",
"duration-in-seconds": 2,
"phase-type": "FINALIZING",
"phase-status": "SUCCEEDED"
},
{
"start-time": "Aug 24, 2022 9:01:13 AM",
"phase-type": "COMPLETED"
}
],
"queued-timeout-in-minutes": 480
},
"current-phase": "COMPLETED",
"current-phase-context": "[: ]",
"version": "1"
}
}
I had the same task. I tried to find any solution. I tried to implement the following script: https://github.com/thii/aws-codebuild-extras. But there is a problem with moving environment variables to lambda.
So finally I solved it using the following steps:
I print author email in buildSpec.yml file:
pre_build:
commands:
- AUTHOR_EMAIL="$(git log -1 --pretty=%ae)"
- echo "CODEBUILD_GIT_AUTHOR_EMAIL=$AUTHOR_EMAIL"
I get all logs from CloudWatch in my lambda handler and find string by pattern ^CODEBUILD_GIT_AUTHOR_EMAIL=(.*)$
I send message to this email using AWS SES inside my lambda handler
Very bad solution, but it works. I can't cost more time for this task

Can we send multiple data in a single metric using aws cloudwatch cli?

Is there a way to send multiple data in a single metric using aws cloudwatch cli.
Have you tried specifying a json object and putting multiple items within that list?
i.e.
aws cloudwatch put-metric-data --namespace "Usage Metrics" --metric-data file://metric.json
Where metric.json is something like:
[
{
"MetricName": "Metric1",
"Timestamp": "Wednesday, November 2, 2013 8:28:20 AM",
"Value": 0.50,
"Unit": "Count"
},
{
"MetricName": Metric2,
"Timestamp": "Wednesday, November 2, 2013 8:28:20 AM",
"Value": 1,
"Unit": "Count"
},
{
"MetricName": "Metric 2",
"Timestamp": "Wednesday, November 2, 2013 8:29:20 AM",
"Value": 5,
"Unit": "Count"
}
]
if you need to send multiple values for one metric, something like:
[
{
"MetricName": "Metric1",
"Timestamp": "Wednesday, November 2, 2013 8:28:20 AM",
"Values": [0.50,2,3,5.1],
"Unit": "Count"
}
]
or
[
{
"MetricName": "Metric1",
"Timestamp": "Wednesday, November 2, 2013 8:28:20 AM",
"StatisticValues": {
"SampleCount": 8,
"Sum": 18,
"Minimum": 2.3,
"Maximum": 2.7
},
"Unit": "None"
}
]

How do I get the cloud foundry services usage

I have deployed some services in cloud foundry. I need to get the usage of all my services. I referred to this document.
I have tried to explore the above documentation in python . I tried the below program:
import sys
import json
import requests
import cf_api
from flask import Flask
from flask_cors import CORS, cross_origin
from getpass import getpass
def apps():
cloud_controller_url = 'https://app-usage.run.pivotal.io/system_report/service_usages'
username='user'
password='pass'
response=''
print('-----apps-----')
print('Authenticating with UAA...')
cc = cf_api.new_cloud_controller(
cloud_controller_url,
client_id='cf', # the ``cf`` command uses this client and the secret below
client_secret='',
username=username,
password=password,
)
print('Login OK!')
print('----------')
print('Searching for applications...')
req = cc.request('/system_report/service_usages')
resources_list = cc.get_all_resources(req)
f = open ("cfServices.json", "w")
print(json.dumps(resources_list))
f.write(json.dumps(resources_list))
apps()
I also tried to explore it in postman. I got 404 error.
I am expecting results. this is the sample expecting output which is provided in cloud foundry documentation:
{
"report_time": "2017-05-11 18:29:14 UTC",
"monthly_service_reports": [
{
"service_name": "fake-service-0507f1fd-2340-49a6-9d43-a347a5f5f6be",
"service_guid": "177dcfde-cd51-4058-bd86-b98015c295f5",
"usages": [
{
"month": 1,
"year": 2017,
"duration_in_hours": 0,
"average_instances": 0,
"maximum_instances": 0
},
{
"month": 2,
"year": 2017,
"duration_in_hours": 0,
"average_instances": 0,
"maximum_instances": 0
},
{
"month": 3,
"year": 2017,
"duration_in_hours": 4.182222222222227,
"average_instances": 0,
"maximum_instances": 2
},
{
"month": 4,
"year": 2017,
"duration_in_hours": 2176.962222222186,
"average_instances": 3,
"maximum_instances": 7
},
{
"month": 5,
"year": 2017,
"duration_in_hours": 385.61388888888854,
"average_instances": 1.5,
"maximum_instances": 3
}
],
"plans": [
{
"usages": [
{
"month": 1,
"year": 2017,
"duration_in_hours": 0,
"average_instances": 0,
"maximum_instances": 0
},
{
"month": 2,
"year": 2017,
"duration_in_hours": 0,
"average_instances": 0,
"maximum_instances": 0
},
{
"month": 3,
"year": 2017,
"duration_in_hours": 4.182222222222227,
"average_instances": 0,
"maximum_instances": 2
},
{
"month": 4,
"year": 2017,
"duration_in_hours": 1465.6388888888941,
"average_instances": 2,
"maximum_instances": 5
},
{
"month": 5,
"year": 2017,
"duration_in_hours": 385.61388888888854,
"average_instances": 1.5,
"maximum_instances": 3
}
],
"service_plan_name": "fake-plan",
"service_plan_guid": "ac09f607-f4e5-4807-af16-e95856061bd7"
}
i believe this api is for PAS owner who deploy PCF dev, and the PCF web services might not allow you to view that kind of information as it doesnot asking for ORG information
But i have something for you ,
you can use this api for each service you are using for fetching some of the information you want
REQUEST: [2019-01-28T22:58:19+05:30]
GET /v2/services/c72eba27-c3f6-4ccb-b2fb-0a7e8ce0a25 HTTP/1.1
Host: api.run.pivotal.io
Accept: application/json
Authorization: [PRIVATE DATA HIDDEN]
User-Agent: cf/6.37.0+a40009753.2018-05-25 (go1.9.6; amd64 windows)
RESPONSE: [2019-01-28T22:58:19+05:30]
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 1619
Content-Type: application/json;charset=utf-8
Date: Mon, 28 Jan 2019 17:28:18 GMT
Server: nginx
X-Content-Type-Options: nosniff
X-Ratelimit-Limit: 20000
X-Ratelimit-Remaining: 19941
X-Ratelimit-Reset: 1548699889
X-Vcap-Request-Id: 87b81068-f777-45a8-46ff-a116d449bd49::d4a02e66-208b-40c4-a9b7-c4afdcc9ffce
{
"entity": {
"active": true,
"bindable": true,
"bindings_retrievable": false,
"description": "Fully managed MongoDB-as-a-Service",
"documentation_url": null,
"extra": "{\n \"displayName\": \"mLab\",\n \"imageUrl\": \"https://d3bql97l1ytoxn.cloudfront.net/app_resources/5819/thumbs_64/img2243331358186927838.png\",\n \"longDescription\": \"mLab is a fully managed MongoDB Database-as-a-Service (DBaaS)\\nplatform that automates the operational aspects of running MongoDB in\\nthe cloud.\",\n \"providerDisplayName\": \"mLab\",\n \"documentationUrl\": \"http://docs.run.pivotal.io/marketplace/services/mlab.html\",\n \"supportUrl\": \"https://support.mlab.com/\"\n}",
"info_url": null,
"instances_retrievable": false,
"label": "mlab",
"long_description": null,
"plan_updateable": false,
"provider": null,
"requires": null,
"service_broker_guid": "bb6733db-3bdc-4d6d-c5a-63e9bbc1f4de",
"service_broker_name": "appdirect",
"service_plans_url": "/v2/services/c72eba27-c3f6-4cb-b2fb-0a74e8ce0a25/service_plans",
"tags": [
"Cloud Databases",
"Developer Tools",
"Web-based",
"Data Store",
"document",
"Windows",
"Security",
"IT Management",
"mongodb",
"Mac"
],
"unique_id": "6213a86a-ae52-11e3-ac06-22000a1dd446",
"url": null,
"version": null
},
"metadata": {
"created_at": "2015-01-19T21:16:29Z",
"guid": "c72eba27-c3f6-4ccb-b2fb-0a74e8ce0a25",
"updated_at": "2018-11-26T21:50:43Z",
"url": "/v2/services/c72eba27-c3f6-4ccb-b2fb-0a7e8ce0a25"
}
}
Hope this helps you

Google Line Chart set value for no data to 0

I want integrate a google line chart on my raspberry pi terminal to show some statistics about my coffee consumption. If my json have got no value for a date, the line chart should set the value to 0. At the moment, dates with no values have got the value of the day before. Any ideas?
I have used this configuration:
let options = {
hAxis: {
format: 'd.M.yy',
gridlines: {count: 15},
},
vAxis: {
title: 'Cups of Coffee',
},
colors: ['#34495e'],
interpolateNulls : true
};
Dates with no value are not displayed in my json. For example: no entry for the date 3.6.2017 Here is the json:
[{
"_id": {
"year": 2017,
"month": 6,
"day": 9,
"action": "Coffee made"
},
"createdAt": "2017-06-09T06:41:50.904Z",
"count": 1
},
{
"_id": {
"year": 2017,
"month": 6,
"day": 8,
"action": "Coffee made"
},
"createdAt": "2017-06-08T05:44:04.081Z",
"count": 1
},
{
"_id": {
"year": 2017,
"month": 6,
"day": 7,
"action": "Coffee made"
},
"createdAt": "2017-06-07T06:10:01.713Z",
"count": 4
},
{
"_id": {
"year": 2017,
"month": 6,
"day": 6,
"action": "Coffee made"
},
"createdAt": "2017-06-06T05:52:09.775Z",
"count": 2
},
{
"_id": {
"year": 2017,
"month": 6,
"day": 2,
"action": "Coffee made"
},
"createdAt": "2017-06-02T06:03:47.243Z",
"count": 1
},
{
"_id": {
"year": 2017,
"month": 6,
"day": 1,
"action": "Coffee made"
},
"createdAt": "2017-06-01T05:37:31.399Z",
"count": 1
},
{
"_id": {
"year": 2017,
"month": 5,
"day": 31,
"action": "Coffee made"
},
"createdAt": "2017-05-31T05:18:49.220Z",
"count": 1
}
]
Current line chart output (The values of date 2. Jun to 5. June should be 0)
just need to add a row for the missing dates...
use data table method getFilteredRows to check data for a certain day
see following working snippet...
the json is loaded, then starting with the min date in the data,
and ending with the current date, each day is checked for data
if now rows are found, one is added with value of 0
google.charts.load('current', {
callback: function () {
drawChart();
window.addEventListener('resize', drawChart, false);
},
packages:['corechart', 'table']
});
function drawChart() {
var jsonData = [{
"_id": {
"year": 2017,
"month": 6,
"day": 9,
"action": "Coffee made"
},
"createdAt": "2017-06-09T06:41:50.904Z",
"count": 1
},
{
"_id": {
"year": 2017,
"month": 6,
"day": 8,
"action": "Coffee made"
},
"createdAt": "2017-06-08T05:44:04.081Z",
"count": 1
},
{
"_id": {
"year": 2017,
"month": 6,
"day": 7,
"action": "Coffee made"
},
"createdAt": "2017-06-07T06:10:01.713Z",
"count": 4
},
{
"_id": {
"year": 2017,
"month": 6,
"day": 6,
"action": "Coffee made"
},
"createdAt": "2017-06-06T05:52:09.775Z",
"count": 2
},
{
"_id": {
"year": 2017,
"month": 6,
"day": 2,
"action": "Coffee made"
},
"createdAt": "2017-06-02T06:03:47.243Z",
"count": 1
},
{
"_id": {
"year": 2017,
"month": 6,
"day": 1,
"action": "Coffee made"
},
"createdAt": "2017-06-01T05:37:31.399Z",
"count": 1
},
{
"_id": {
"year": 2017,
"month": 5,
"day": 31,
"action": "Coffee made"
},
"createdAt": "2017-05-31T05:18:49.220Z",
"count": 1
}
];
var datePattern = 'd.M.yy';
var formatDate = new google.visualization.DateFormat({
pattern: datePattern
});
var dataTable = new google.visualization.DataTable({
"cols": [
{"label": "Date", "type": "date"},
{"label": "Cups of Coffee", "type":"number"}
]
});
jsonData.forEach(function (row) {
dataTable.addRow([
new Date(row.createdAt),
row.count
]);
});
var startDate = dataTable.getColumnRange(0).min;
var endDate = new Date();
var oneDay = (1000 * 60 * 60 * 24);
for (var i = startDate.getTime(); i < endDate.getTime(); i = i + oneDay) {
var coffeeData = dataTable.getFilteredRows([{
column: 0,
test: function (value, row, column, table) {
var coffeeDate = formatDate.formatValue(table.getValue(row, column));
var testDate = formatDate.formatValue(new Date(i));
return (coffeeDate === testDate);
}
}]);
if (coffeeData.length === 0) {
dataTable.addRow([
new Date(i),
0
]);
}
}
dataTable.sort({column: 0});
var chartLine = new google.visualization.ChartWrapper({
chartType: 'LineChart',
containerId: 'chart',
dataTable: dataTable,
options: {
theme: 'material',
legend: {
position: 'none',
},
chartArea: {
top: 12,
right: 12,
bottom: 48,
left: 48,
height: '100%',
width: '100%'
},
colors: ['#34495e'],
hAxis: {
format: datePattern,
gridlines: {
count: 15
},
},
pointSize: 4,
vAxis: {
title: 'Cups of Coffee',
}
}
});
chartLine.draw();
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>