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.
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": {}}}]}
{
"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"
}
}