Skip to main content

2 posts tagged with "pagerduty"

View All Tags

AWS Lambda → PagerDuty: Show who's on‑call

See who’s on‑call for this Lambda’s service right now
🔒
https://eu-central-1.console.aws.amazon.com/lambda/home?region=eu-central-1#/functions/shop-order-processing?tab=code
preload

How does this help you?

  • See who is currently on‑call for the service associated with your Lambda function
  • Jump directly to PagerDuty's On‑Call view filtered to the service

Preview

How it looks in the extension

ToolJumppagerdutyOn-call: John ... Other items ...

High level approach

We read the service tag from the Lambda function using the AWS Lambda API, then search for a PagerDuty service with a matching name. We resolve the service's escalation policy, fetch the current L1 on‑call user, and show their first name.

Prerequisites

For the code below to work, please ensure you have:

  1. AWS credentials configured in your secrets (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
  2. PagerDuty API token configured in your secrets (PAGERDUTY_API_TOKEN)
  3. Lambda functions tagged with:
    • service tag containing the PagerDuty service name (exact match required)
  4. PagerDuty service with a name that exactly matches the Lambda function's service tag value

Code

aws-lambda-pagerduty-oncall.integration.example.js
module.exports = {
metadata: {
name: 'aws-lambda-pagerduty-oncall',
description: 'Show current L1 on-call for the PagerDuty service associated with this Lambda function',
match: {
contextType: 'aws',
context: {
'service.name': { equals: 'lambda' },
'service.arn': { exists: true },
'scope.region': { exists: true },
'service.resourceName': { exists: true }
}
},
requiredSecrets: ['AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY', 'PAGERDUTY_API_TOKEN'],
cache: 900
},
run: async function (context, secrets = {}) {
// Get AWS region from context
const awsRegion = context.scope.region;
const functionName = context.service.resourceName;

try {
// Query AWS Lambda to get function tags
const { LambdaClient, ListTagsCommand } = require('@aws-sdk/client-lambda');

const lambdaClient = new LambdaClient({
credentials: {
accessKeyId: secrets.AWS_ACCESS_KEY_ID,
secretAccessKey: secrets.AWS_SECRET_ACCESS_KEY,
},
region: awsRegion
});

const command = new ListTagsCommand({ Resource: context.service.arn });
const tagsResponse = await lambdaClient.send(command);

// Look for service tag
const serviceName = tagsResponse.Tags?.service;

if (!serviceName) {
logger.warn({
operation: 'aws-lambda-pagerduty-oncall',
step: 'no-service-tag',
functionName: functionName
}, 'No service tag found on Lambda function');
return [];
}

// Query PagerDuty for the service
const PD_HOST = 'https://api.pagerduty.com'; // US API
const headers = {
'Authorization': `Token token=${secrets.PAGERDUTY_API_TOKEN}`,
'Accept': 'application/vnd.pagerduty+json;version=2',
'Content-Type': 'application/json'
};

// Search for services by name
const servicesResp = await fetch(`${PD_HOST}/services?query=${encodeURIComponent(serviceName)}&limit=25`, { headers });
if (!servicesResp.ok) {
throw new Error(`PagerDuty API error (services): ${servicesResp.status} ${servicesResp.statusText}`);
}
const servicesJson = await servicesResp.json();
const service = (servicesJson.services || []).find(s => s.name === serviceName);

if (!service) {
logger.warn({
operation: 'aws-lambda-pagerduty-oncall',
step: 'service-not-found',
functionName: functionName,
serviceName: serviceName
}, 'PagerDuty service not found');
return [];
}

// Fetch service to get escalation policy
const serviceResp = await fetch(`${PD_HOST}/services/${encodeURIComponent(service.id)}?include[]=escalation_policy`, { headers });
if (!serviceResp.ok) {
throw new Error(`PagerDuty API error (service): ${serviceResp.status} ${serviceResp.statusText}`);
}
const serviceJson = await serviceResp.json();
const ep = serviceJson.service && serviceJson.service.escalation_policy;
if (!ep || !ep.id) {
logger.warn({
operation: 'aws-lambda-pagerduty-oncall',
step: 'no-escalation-policy',
functionName: functionName,
serviceName: serviceName,
serviceId: service.id
}, 'No escalation policy found for service');
return [];
}

// Query on-calls for this escalation policy and pick L1
const oncallsResp = await fetch(`${PD_HOST}/oncalls?limit=25&escalation_policy_ids[]=${encodeURIComponent(ep.id)}`, { headers });
if (!oncallsResp.ok) {
throw new Error(`PagerDuty API error (oncalls): ${oncallsResp.status} ${oncallsResp.statusText}`);
}
const oncallsJson = await oncallsResp.json();
const l1 = (oncallsJson.oncalls || []).find(o => o.escalation_level === 1);
if (!l1 || !l1.user) {
logger.warn({
operation: 'aws-lambda-pagerduty-oncall',
step: 'no-oncall-user',
functionName: functionName,
serviceName: serviceName,
serviceId: service.id
}, 'No L1 on-call user found');
return [];
}

// Extract first name
const fullName = l1.user.name || l1.user.summary || '';
const firstName = fullName.split(' ')[0] || fullName;

// Build deep link to On-Call view filtered by service
const oncallUrl = `https://app.pagerduty.com/oncalls?service_ids[]=${encodeURIComponent(service.id)}`;

const results = [
{
type: 'link',
content: `On-call: ${firstName}`,
href: oncallUrl,
icon: 'pagerduty'
}
];

return results;

} catch (error) {
logger.error({
operation: 'aws-lambda-pagerduty-oncall',
step: 'error',
functionName: functionName,
error: error.message
}, 'Error retrieving on-call information');
return [];
}
}
};

GitHub → PagerDuty: Show who's on‑call

See who’s on‑call for this repo’s service right now
🔒
https://github.com/mycompany/webshop
preload

How does this help you?

  • See who is currently on‑call for this repository
  • Jump directly to PagerDuty’s On‑Call view filtered to the service

Preview

How it looks in the extension

ToolJumppagerdutyOncall: John ... Other items ...

High level approach

We assume PagerDuty services are tagged with the repository name (tag key repository, value matching the GitHub repo, e.g. company/webshop). We locate the first matching service, resolve its escalation policy, fetch the current L1 on‑call user, and show their first name.

Prerequisites

For the code below to work, please follow the guide on Connecting to PagerDuty. Provide PAGERDUTY_API_TOKEN as a secret.

Code

github.pagerduty.oncall.integration.example.js
module.exports = {
metadata: {
name: 'github-pagerduty-oncall',
description: 'Show current L1 on-call for the first PagerDuty service tagged with this repository, with link to the On-Call view',
match: {
contextType: 'github',
context: {
'page.repository': { startsWith: 'my-org/' }
}
},
requiredSecrets: ['PAGERDUTY_API_TOKEN'],
cache: 900
},
run: async function (context, secrets = {}) {
const PD_HOST = 'https://api.pagerduty.com'; // US API

const repoTag = `repository:${context.page.repository}`;
const headers = {
'Authorization': `Token token=${secrets.PAGERDUTY_API_TOKEN}`,
'Accept': 'application/vnd.pagerduty+json;version=2',
'Content-Type': 'application/json'
};

// 1) Resolve the tag ID by label (exact match)
const tagsResp = await fetch(`${PD_HOST}/tags?query=${encodeURIComponent(repoTag)}&limit=25`, { headers });
if (!tagsResp.ok) {
throw new Error(`PagerDuty API error (tags): ${tagsResp.status} ${tagsResp.statusText}`);
}
const tagsJson = await tagsResp.json();
const tag = (tagsJson.tags || []).find(t => t.label === repoTag);
if (!tag) {
return [];
}

// 2) Find first service entity with this tag
const entitiesResp = await fetch(`${PD_HOST}/tags/${encodeURIComponent(tag.id)}/entities?type=service&limit=25`, { headers });
if (!entitiesResp.ok) {
throw new Error(`PagerDuty API error (tag entities): ${entitiesResp.status} ${entitiesResp.statusText}`);
}
const entitiesJson = await entitiesResp.json();
const service = (entitiesJson.entities || []).find(e => e.type === 'service');
if (!service) {
return [];
}

// 3) Fetch service to get escalation policy
const serviceResp = await fetch(`${PD_HOST}/services/${encodeURIComponent(service.id)}?include[]=escalation_policy`, { headers });
if (!serviceResp.ok) {
throw new Error(`PagerDuty API error (service): ${serviceResp.status} ${serviceResp.statusText}`);
}
const serviceJson = await serviceResp.json();
const ep = serviceJson.service && serviceJson.service.escalation_policy;
if (!ep || !ep.id) {
return [];
}

// 4) Query on-calls for this escalation policy and pick L1
const oncallsResp = await fetch(`${PD_HOST}/oncalls?limit=25&escalation_policy_ids[]=${encodeURIComponent(ep.id)}`, { headers });
if (!oncallsResp.ok) {
throw new Error(`PagerDuty API error (oncalls): ${oncallsResp.status} ${oncallsResp.statusText}`);
}
const oncallsJson = await oncallsResp.json();
const l1 = (oncallsJson.oncalls || []).find(o => o.escalation_level === 1);
if (!l1 || !l1.user) {
return [];
}

// Extract first name
const fullName = l1.user.name || l1.user.summary || '';
const firstName = fullName.split(' ')[0] || fullName;

// 5) Build deep link to On-Call view filtered by service
const oncallUrl = `https://app.pagerduty.com/oncalls?service_ids[]=${encodeURIComponent(service.id)}`;

const results = [
{ type: 'link', content: `On-call: ${firstName}`, icon: 'pagerduty', href: oncallUrl },
];

return results;
}
};